From dc0c7e49c4731e47ecdb47d9ee42d2275fb0b3f0 Mon Sep 17 00:00:00 2001 From: ashmrtn Date: Mon, 12 Sep 2022 13:16:08 -0700 Subject: [PATCH] Remove selector pathType const in favor of path package const (#757) * Remove pathType const The path package const only provides information on the category, not the service for the path. * Fix var name/package name clashes --- src/pkg/selectors/exchange.go | 31 ++++++++++++------------ src/pkg/selectors/exchange_test.go | 8 +++---- src/pkg/selectors/scopes.go | 38 ++++++++---------------------- src/pkg/selectors/scopes_test.go | 20 ++++++++-------- 4 files changed, 40 insertions(+), 57 deletions(-) diff --git a/src/pkg/selectors/exchange.go b/src/pkg/selectors/exchange.go index 35f717974..45914cb94 100644 --- a/src/pkg/selectors/exchange.go +++ b/src/pkg/selectors/exchange.go @@ -4,6 +4,7 @@ import ( "strconv" "github.com/alcionai/corso/src/internal/common" + "github.com/alcionai/corso/src/internal/path" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/filters" ) @@ -558,27 +559,27 @@ func (ec exchangeCategory) unknownCat() categorizer { // Example: // [tenantID, userPN, "mail", mailFolder, mailID] // => {exchUser: userPN, exchMailFolder: mailFolder, exchMail: mailID} -func (ec exchangeCategory) pathValues(path []string) map[categorizer]string { +func (ec exchangeCategory) pathValues(p []string) map[categorizer]string { m := map[categorizer]string{} - if len(path) < 5 { + if len(p) < 5 { return m } switch ec { case ExchangeContact: - m[ExchangeUser] = path[1] - m[ExchangeContactFolder] = path[3] - m[ExchangeContact] = path[4] + m[ExchangeUser] = p[1] + m[ExchangeContactFolder] = p[3] + m[ExchangeContact] = p[4] case ExchangeEvent: - m[ExchangeUser] = path[1] - m[ExchangeEventCalendar] = path[3] - m[ExchangeEvent] = path[4] + m[ExchangeUser] = p[1] + m[ExchangeEventCalendar] = p[3] + m[ExchangeEvent] = p[4] case ExchangeMail: - m[ExchangeUser] = path[2] - m[ExchangeMailFolder] = path[4] - m[ExchangeMail] = path[5] + m[ExchangeUser] = p[2] + m[ExchangeMailFolder] = p[4] + m[ExchangeMail] = p[5] } return m @@ -680,10 +681,10 @@ func (s exchange) Reduce(deets *details.Details) *details.Details { return reduce[ExchangeScope]( deets, s.Selector, - map[pathType]exchangeCategory{ - exchangeContactPath: ExchangeContact, - exchangeEventPath: ExchangeEvent, - exchangeMailPath: ExchangeMail, + map[path.CategoryType]exchangeCategory{ + path.ContactsCategory: ExchangeContact, + path.EventsCategory: ExchangeEvent, + path.EmailCategory: ExchangeMail, }, ) } diff --git a/src/pkg/selectors/exchange_test.go b/src/pkg/selectors/exchange_test.go index aa5528b4e..5fecc8cd4 100644 --- a/src/pkg/selectors/exchange_test.go +++ b/src/pkg/selectors/exchange_test.go @@ -1054,10 +1054,10 @@ func (suite *ExchangeSelectorSuite) TestScopesByCategory() { return mss } - cats := map[pathType]exchangeCategory{ - exchangeContactPath: ExchangeContact, - exchangeEventPath: ExchangeEvent, - exchangeMailPath: ExchangeMail, + cats := map[path.CategoryType]exchangeCategory{ + path.ContactsCategory: ExchangeContact, + path.EventsCategory: ExchangeEvent, + path.EmailCategory: ExchangeMail, } table := []struct { diff --git a/src/pkg/selectors/scopes.go b/src/pkg/selectors/scopes.go index 7cdd5adb1..87355636c 100644 --- a/src/pkg/selectors/scopes.go +++ b/src/pkg/selectors/scopes.go @@ -209,7 +209,7 @@ func isAnyTarget[T scopeT, C categoryT](s T, cat C) bool { func reduce[T scopeT, C categoryT]( deets *details.Details, s Selector, - dataCategories map[pathType]C, + dataCategories map[path.CategoryType]C, ) *details.Details { if deets == nil { return nil @@ -251,50 +251,32 @@ func reduce[T scopeT, C categoryT]( return reduced } -// TODO: this is a hack. We don't want these values declared here- it will get -// unwieldy to have all of them for all services. They should be declared in -// paths, since that's where service- and data-type-specific assertions are owned. -type pathType int - -const ( - unknownPathType pathType = iota - exchangeEventPath - exchangeContactPath - exchangeMailPath -) - // return the service data type of the path. // TODO: this is a hack. We don't want this identification to occur in this // package. It should get handled in paths, since that's where service- and // data-type-specific assertions are owned. // Ideally, we'd use something like path.DataType() instead of this func. -func pathTypeIn(p []string) pathType { +func pathTypeIn(p []string) path.CategoryType { // not all paths will be len=3. Most should be longer. // This just protects us from panicing below. if len(p) < 4 { - return unknownPathType + return path.UnknownCategory } - switch p[3] { - case path.EmailCategory.String(): - return exchangeMailPath - case path.ContactsCategory.String(): - return exchangeContactPath - case path.EventsCategory.String(): - return exchangeEventPath + if c := path.ToCategoryType(p[3]); c != path.UnknownCategory { + return c } - // fallback for unmigrated events and contacts paths switch p[2] { case path.EmailCategory.String(): - return exchangeMailPath + return path.EmailCategory case path.ContactsCategory.String(): - return exchangeContactPath + return path.ContactsCategory case path.EventsCategory.String(): - return exchangeEventPath + return path.EventsCategory } - return unknownPathType + return path.UnknownCategory } // groups each scope by its category of data (specified by the service-selector). @@ -303,7 +285,7 @@ func pathTypeIn(p []string) pathType { // so long as "mail" and "event" are contained in cats. func scopesByCategory[T scopeT, C categoryT]( scopes []scope, - cats map[pathType]C, + cats map[path.CategoryType]C, ) map[C][]T { m := map[C][]T{} for _, cat := range cats { diff --git a/src/pkg/selectors/scopes_test.go b/src/pkg/selectors/scopes_test.go index 8cfedf0da..3af959d0c 100644 --- a/src/pkg/selectors/scopes_test.go +++ b/src/pkg/selectors/scopes_test.go @@ -221,8 +221,8 @@ func (suite *SelectorScopesSuite) TestReduce() { }, } } - dataCats := map[pathType]mockCategorizer{ - unknownPathType: rootCatStub, + dataCats := map[path.CategoryType]mockCategorizer{ + path.UnknownCategory: rootCatStub, } for _, test := range reduceTestTable { @@ -238,32 +238,32 @@ func (suite *SelectorScopesSuite) TestReduce() { func (suite *SelectorScopesSuite) TestPathTypeIn() { table := []struct { name string - pathType pathType + pathType path.CategoryType pth []string }{ { name: "empty", - pathType: unknownPathType, + pathType: path.UnknownCategory, pth: []string{}, }, { name: "email", - pathType: exchangeMailPath, + pathType: path.EmailCategory, pth: stubPath(path.ExchangeService, path.EmailCategory, "", "", ""), }, { name: "contact", - pathType: exchangeContactPath, + pathType: path.ContactsCategory, pth: stubPath(path.ExchangeService, path.ContactsCategory, "", "", ""), }, { name: "event", - pathType: exchangeEventPath, + pathType: path.EventsCategory, pth: stubPath(path.ExchangeService, path.EventsCategory, "", "", ""), }, { name: "bogus", - pathType: unknownPathType, + pathType: path.UnknownCategory, pth: []string{"", "", "", "fnords", "", ""}, }, } @@ -282,8 +282,8 @@ func (suite *SelectorScopesSuite) TestScopesByCategory() { s2[scopeKeyCategory] = filterize(unknownCatStub.String()) result := scopesByCategory[mockScope]( []scope{scope(s1), scope(s2)}, - map[pathType]mockCategorizer{ - unknownPathType: rootCatStub, + map[path.CategoryType]mockCategorizer{ + path.UnknownCategory: rootCatStub, }) assert.Len(t, result, 1) assert.Len(t, result[rootCatStub], 1)