From 23a3cd6e16b7db9dd8619009b3960b5d5f67c6f5 Mon Sep 17 00:00:00 2001 From: Keepers Date: Mon, 2 Jan 2023 14:18:52 -0700 Subject: [PATCH] remove optionIdentifier (#2001) ## Description the exchange optionIdentifier performs almost all the same functions as the path category. In many cases, it is actually a translation of the other value. This cleanup will cascade into the creation of an api layer for the graph client. ## Does this PR need a docs update or release note? - [x] :no_entry: No ## Type of change - [x] :hamster: Trivial/Minor ## Issue(s) * #1967 ## Test Plan - [x] :zap: Unit test - [x] :green_heart: E2E --- src/cmd/getM365/getItem.go | 2 +- .../exchange/exchange_data_collection.go | 24 +-- .../exchange/optionidentifier_string.go | 29 --- .../connector/exchange/query_options.go | 184 ++++++------------ .../connector/exchange/service_iterators.go | 5 +- .../connector/exchange/service_restore.go | 10 +- 6 files changed, 78 insertions(+), 176 deletions(-) delete mode 100644 src/internal/connector/exchange/optionidentifier_string.go diff --git a/src/cmd/getM365/getItem.go b/src/cmd/getM365/getItem.go index 1036faf49..b3e08ec00 100644 --- a/src/cmd/getM365/getItem.go +++ b/src/cmd/getM365/getItem.go @@ -101,7 +101,7 @@ func runDisplayM365JSON( switch cat { case path.EmailCategory, path.EventsCategory, path.ContactsCategory: - get, serializeFunc = exchange.GetQueryAndSerializeFunc(exchange.CategoryToOptionIdentifier(cat)) + get, serializeFunc = exchange.GetQueryAndSerializeFunc(cat) default: return fmt.Errorf("unable to process category: %s", cat) } diff --git a/src/internal/connector/exchange/exchange_data_collection.go b/src/internal/connector/exchange/exchange_data_collection.go index e965919a4..7ff76500c 100644 --- a/src/internal/connector/exchange/exchange_data_collection.go +++ b/src/internal/connector/exchange/exchange_data_collection.go @@ -59,9 +59,9 @@ type Collection struct { // service - client/adapter pair used to access M365 back store service graph.Servicer - collectionType optionIdentifier - statusUpdater support.StatusUpdater - ctrl control.Options + category path.CategoryType + statusUpdater support.StatusUpdater + ctrl control.Options // FullPath is the current hierarchical path used by this collection. fullPath path.Path @@ -86,14 +86,14 @@ type Collection struct { func NewCollection( user string, curr, prev path.Path, - collectionType optionIdentifier, + category path.CategoryType, service graph.Servicer, statusUpdater support.StatusUpdater, ctrlOpts control.Options, doNotMergeItems bool, ) Collection { collection := Collection{ - collectionType: collectionType, + category: category, ctrl: ctrlOpts, data: make(chan data.Stream, collectionChannelBufferSize), doNotMergeItems: doNotMergeItems, @@ -135,13 +135,13 @@ func (col *Collection) Items() <-chan data.Stream { // GetQueryAndSerializeFunc helper function that returns the two functions functions // required to convert M365 identifier into a byte array filled with the serialized data -func GetQueryAndSerializeFunc(optID optionIdentifier) (GraphRetrievalFunc, GraphSerializeFunc) { - switch optID { - case contacts: +func GetQueryAndSerializeFunc(category path.CategoryType) (GraphRetrievalFunc, GraphSerializeFunc) { + switch category { + case path.ContactsCategory: return RetrieveContactDataForUser, serializeAndStreamContact - case events: + case path.EventsCategory: return RetrieveEventDataForUser, serializeAndStreamEvent - case messages: + case path.EmailCategory: return RetrieveMessageDataForUser, serializeAndStreamMessage // Unsupported options returns nil, nil default: @@ -195,9 +195,9 @@ func (col *Collection) streamItems(ctx context.Context) { // get QueryBasedonIdentifier // verify that it is the correct type in called function // serializationFunction - query, serializeFunc := GetQueryAndSerializeFunc(col.collectionType) + query, serializeFunc := GetQueryAndSerializeFunc(col.category) if query == nil { - errs = fmt.Errorf("unrecognized collection type: %s", col.collectionType.String()) + errs = fmt.Errorf("unrecognized collection type: %s", col.category) return } diff --git a/src/internal/connector/exchange/optionidentifier_string.go b/src/internal/connector/exchange/optionidentifier_string.go deleted file mode 100644 index 7e93a06e3..000000000 --- a/src/internal/connector/exchange/optionidentifier_string.go +++ /dev/null @@ -1,29 +0,0 @@ -// Code generated by "stringer -type=optionIdentifier"; DO NOT EDIT. - -package exchange - -import "strconv" - -func _() { - // An "invalid array index" compiler error signifies that the constant values have changed. - // Re-run the stringer command to generate them again. - var x [1]struct{} - _ = x[unknown-0] - _ = x[folders-1] - _ = x[calendars-2] - _ = x[events-3] - _ = x[messages-4] - _ = x[users-5] - _ = x[contacts-6] -} - -const _optionIdentifier_name = "unknownfolderscalendarseventsmessagesuserscontacts" - -var _optionIdentifier_index = [...]uint8{0, 7, 14, 23, 29, 37, 42, 50} - -func (i optionIdentifier) String() string { - if i < 0 || i >= optionIdentifier(len(_optionIdentifier_index)-1) { - return "optionIdentifier(" + strconv.FormatInt(int64(i), 10) + ")" - } - return _optionIdentifier_name[_optionIdentifier_index[i]:_optionIdentifier_index[i+1]] -} diff --git a/src/internal/connector/exchange/query_options.go b/src/internal/connector/exchange/query_options.go index e42c4c6b4..c4c206518 100644 --- a/src/internal/connector/exchange/query_options.go +++ b/src/internal/connector/exchange/query_options.go @@ -4,9 +4,6 @@ import ( "fmt" msuser "github.com/microsoftgraph/msgraph-sdk-go/users" - "github.com/pkg/errors" - - "github.com/alcionai/corso/src/pkg/path" ) // ----------------------------------------------------------------------- @@ -15,98 +12,59 @@ import ( // selectors for M365 objects // ----------------------------------------------------------------------- var ( - fieldsForCalendars = map[string]int{ - "changeKey": 1, - "events": 2, - "id": 3, - "isDefaultCalendar": 4, - "name": 5, - "owner": 6, + fieldsForCalendars = map[string]struct{}{ + "changeKey": {}, + "events": {}, + "id": {}, + "isDefaultCalendar": {}, + "name": {}, + "owner": {}, } - fieldsForEvents = map[string]int{ - "calendar": 1, - "end": 2, - "id": 3, - "isOnlineMeeting": 4, - "isReminderOn": 5, - "responseStatus": 6, - "responseRequested": 7, - "showAs": 8, - "subject": 9, + fieldsForEvents = map[string]struct{}{ + "calendar": {}, + "end": {}, + "id": {}, + "isOnlineMeeting": {}, + "isReminderOn": {}, + "responseStatus": {}, + "responseRequested": {}, + "showAs": {}, + "subject": {}, } - fieldsForFolders = map[string]int{ - "childFolderCount": 1, - "displayName": 2, - "id": 3, - "isHidden": 4, - "parentFolderId": 5, - "totalItemCount": 6, - "unreadItemCount": 7, + fieldsForFolders = map[string]struct{}{ + "childFolderCount": {}, + "displayName": {}, + "id": {}, + "isHidden": {}, + "parentFolderId": {}, + "totalItemCount": {}, + "unreadItemCount": {}, } - fieldsForUsers = map[string]int{ - "birthday": 1, - "businessPhones": 2, - "city": 3, - "companyName": 4, - "department": 5, - "displayName": 6, - "employeeId": 7, - "id": 8, - "mail": 9, - "userPrincipalName": 10, + fieldsForMessages = map[string]struct{}{ + "conservationId": {}, + "conversationIndex": {}, + "parentFolderId": {}, + "subject": {}, + "webLink": {}, + "id": {}, + "isRead": {}, } - fieldsForMessages = map[string]int{ - "conservationId": 1, - "conversationIndex": 2, - "parentFolderId": 3, - "subject": 4, - "webLink": 5, - "id": 6, - "isRead": 7, - } - - fieldsForContacts = map[string]int{ - "id": 1, - "companyName": 2, - "department": 3, - "displayName": 4, - "fileAs": 5, - "givenName": 6, - "manager": 7, - "parentFolderId": 8, + fieldsForContacts = map[string]struct{}{ + "id": {}, + "companyName": {}, + "department": {}, + "displayName": {}, + "fileAs": {}, + "givenName": {}, + "manager": {}, + "parentFolderId": {}, } ) -type optionIdentifier int - -//go:generate stringer -type=optionIdentifier -const ( - unknown optionIdentifier = iota - folders - calendars - events - messages - users - contacts -) - -func CategoryToOptionIdentifier(category path.CategoryType) optionIdentifier { - switch category { - case path.EmailCategory: - return messages - case path.ContactsCategory: - return contacts - case path.EventsCategory: - return events - default: - return unknown - } -} - // ----------------------------------------------------------------------- // exchange.Query Option Section // These functions can be used to filter a response on M365 @@ -117,7 +75,7 @@ func CategoryToOptionIdentifier(category path.CategoryType) optionIdentifier { func optionsForFolderMessagesDelta( moreOps []string, ) (*msuser.ItemMailFoldersItemMessagesDeltaRequestBuilderGetRequestConfiguration, error) { - selecting, err := buildOptions(moreOps, messages) + selecting, err := buildOptions(moreOps, fieldsForMessages) if err != nil { return nil, err } @@ -139,7 +97,7 @@ func optionsForCalendars(moreOps []string) ( *msuser.ItemCalendarsRequestBuilderGetRequestConfiguration, error, ) { - selecting, err := buildOptions(moreOps, calendars) + selecting, err := buildOptions(moreOps, fieldsForCalendars) if err != nil { return nil, err } @@ -160,7 +118,7 @@ func optionsForContactFolders(moreOps []string) ( *msuser.ItemContactFoldersRequestBuilderGetRequestConfiguration, error, ) { - selecting, err := buildOptions(moreOps, folders) + selecting, err := buildOptions(moreOps, fieldsForFolders) if err != nil { return nil, err } @@ -179,7 +137,7 @@ func optionsForContactFolderByID(moreOps []string) ( *msuser.ItemContactFoldersContactFolderItemRequestBuilderGetRequestConfiguration, error, ) { - selecting, err := buildOptions(moreOps, folders) + selecting, err := buildOptions(moreOps, fieldsForFolders) if err != nil { return nil, err } @@ -200,7 +158,7 @@ func optionsForContactFolderByID(moreOps []string) ( func optionsForMailFolders( moreOps []string, ) (*msuser.ItemMailFoldersRequestBuilderGetRequestConfiguration, error) { - selecting, err := buildOptions(moreOps, folders) + selecting, err := buildOptions(moreOps, fieldsForFolders) if err != nil { return nil, err } @@ -221,7 +179,7 @@ func optionsForMailFolders( func optionsForMailFoldersItem( moreOps []string, ) (*msuser.ItemMailFoldersMailFolderItemRequestBuilderGetRequestConfiguration, error) { - selecting, err := buildOptions(moreOps, folders) + selecting, err := buildOptions(moreOps, fieldsForFolders) if err != nil { return nil, err } @@ -239,7 +197,7 @@ func optionsForMailFoldersItem( func optionsForContactFoldersItemDelta( moreOps []string, ) (*msuser.ItemContactFoldersItemContactsDeltaRequestBuilderGetRequestConfiguration, error) { - selecting, err := buildOptions(moreOps, contacts) + selecting, err := buildOptions(moreOps, fieldsForContacts) if err != nil { return nil, err } @@ -258,7 +216,7 @@ func optionsForContactFoldersItemDelta( // optionsForEvents ensures valid option inputs for exchange.Events // @return is first call in Events().GetWithRequestConfigurationAndResponseHandler(options, handler) func optionsForEvents(moreOps []string) (*msuser.ItemEventsRequestBuilderGetRequestConfiguration, error) { - selecting, err := buildOptions(moreOps, events) + selecting, err := buildOptions(moreOps, fieldsForEvents) if err != nil { return nil, err } @@ -277,7 +235,7 @@ func optionsForEvents(moreOps []string) (*msuser.ItemEventsRequestBuilderGetRequ func optionsForEventsByCalendar( moreOps []string, ) (*msuser.ItemCalendarsItemEventsRequestBuilderGetRequestConfiguration, error) { - selecting, err := buildOptions(moreOps, events) + selecting, err := buildOptions(moreOps, fieldsForEvents) if err != nil { return nil, err } @@ -297,7 +255,7 @@ func optionsForEventsByCalendar( func optionsForContactChildFolders( moreOps []string, ) (*msuser.ItemContactFoldersItemChildFoldersRequestBuilderGetRequestConfiguration, error) { - selecting, err := buildOptions(moreOps, contacts) + selecting, err := buildOptions(moreOps, fieldsForContacts) if err != nil { return nil, err } @@ -315,7 +273,7 @@ func optionsForContactChildFolders( // optionsForContacts transforms options into select query for MailContacts // @return is the first call in Contacts().GetWithRequestConfigurationAndResponseHandler(options, handler) func optionsForContacts(moreOps []string) (*msuser.ItemContactsRequestBuilderGetRequestConfiguration, error) { - selecting, err := buildOptions(moreOps, contacts) + selecting, err := buildOptions(moreOps, fieldsForContacts) if err != nil { return nil, err } @@ -333,39 +291,15 @@ func optionsForContacts(moreOps []string) (*msuser.ItemContactsRequestBuilderGet // buildOptions - Utility Method for verifying if select options are valid for the m365 object type // @return is a pair. The first is a string literal of allowable options based on the object type, // the second is an error. An error is returned if an unsupported option or optionIdentifier was used -func buildOptions(options []string, optID optionIdentifier) ([]string, error) { - var ( - allowedOptions map[string]int - returnedOptions = []string{"id"} - ) +func buildOptions(fields []string, allowed map[string]struct{}) ([]string, error) { + returnedOptions := []string{"id"} - switch optID { - case calendars: - allowedOptions = fieldsForCalendars - case contacts: - allowedOptions = fieldsForContacts - case events: - allowedOptions = fieldsForEvents - case folders: - allowedOptions = fieldsForFolders - case users: - allowedOptions = fieldsForUsers - case messages: - allowedOptions = fieldsForMessages - case unknown: - fallthrough - default: - return nil, errors.New("unsupported option") - } - - for _, entry := range options { - _, ok := allowedOptions[entry] + for _, entry := range fields { + _, ok := allowed[entry] if !ok { - return nil, fmt.Errorf("unsupported element passed to buildOptions: %v", entry) + return nil, fmt.Errorf("unsupported field: %v", entry) } - - returnedOptions = append(returnedOptions, entry) } - return returnedOptions, nil + return append(returnedOptions, fields...), nil } diff --git a/src/internal/connector/exchange/service_iterators.go b/src/internal/connector/exchange/service_iterators.go index ae416c997..19308b473 100644 --- a/src/internal/connector/exchange/service_iterators.go +++ b/src/internal/connector/exchange/service_iterators.go @@ -44,7 +44,6 @@ func filterContainersAndFillCollections( ) error { var ( errs error - oi = CategoryToOptionIdentifier(qp.Category) // folder ID -> delta url or folder path lookups deltaURLs = map[string]string{} currPaths = map[string]string{} @@ -119,7 +118,7 @@ func filterContainersAndFillCollections( qp.ResourceOwner, currPath, prevPath, - oi, + scope.Category().PathType(), service, statusUpdater, ctrlOpts, @@ -169,7 +168,7 @@ func filterContainersAndFillCollections( qp.ResourceOwner, nil, // marks the collection as deleted prevPath, - oi, + scope.Category().PathType(), service, statusUpdater, ctrlOpts, diff --git a/src/internal/connector/exchange/service_restore.go b/src/internal/connector/exchange/service_restore.go index e5efb2fe7..d2056c95c 100644 --- a/src/internal/connector/exchange/service_restore.go +++ b/src/internal/connector/exchange/service_restore.go @@ -37,14 +37,12 @@ func RestoreExchangeObject( return nil, fmt.Errorf("restore policy: %s not supported for RestoreExchangeObject", policy) } - setting := CategoryToOptionIdentifier(category) - - switch setting { - case messages: + switch category { + case path.EmailCategory: return RestoreMailMessage(ctx, bits, service, control.Copy, destination, user) - case contacts: + case path.ContactsCategory: return RestoreExchangeContact(ctx, bits, service, control.Copy, destination, user) - case events: + case path.EventsCategory: return RestoreExchangeEvent(ctx, bits, service, control.Copy, destination, user) default: return nil, fmt.Errorf("type: %s not supported for RestoreExchangeObject", category)