From 8f7070ffac7007ec22edcda10f8c7f3acc31e7e1 Mon Sep 17 00:00:00 2001 From: Keepers Date: Mon, 18 Sep 2023 14:57:15 -0600 Subject: [PATCH] add humanstring to path category type (#4282) #### Does this PR need a docs update or release note? - [x] :no_entry: No #### Type of change - [x] :broom: Tech Debt/Cleanup #### Issue(s) * #3988 #### Test Plan - [x] :muscle: Manual - [x] :zap: Unit test --- src/cmd/factory/impl/common.go | 2 +- .../m365/collection/drive/collections.go | 4 +- .../m365/collection/exchange/backup.go | 2 +- .../m365/collection/exchange/collection.go | 2 +- .../m365/collection/exchange/restore.go | 2 +- .../m365/collection/groups/collection.go | 2 +- .../m365/collection/site/collection.go | 2 +- src/internal/m365/service/groups/backup.go | 7 +- .../m365/service/sharepoint/backup.go | 2 +- src/pkg/path/category_type.go | 62 ++++++++++------ src/pkg/path/category_type_test.go | 72 +++++++++++++++++++ 11 files changed, 122 insertions(+), 37 deletions(-) create mode 100644 src/pkg/path/category_type_test.go diff --git a/src/cmd/factory/impl/common.go b/src/cmd/factory/impl/common.go index 9f9eee19d..7ce382072 100644 --- a/src/cmd/factory/impl/common.go +++ b/src/cmd/factory/impl/common.go @@ -73,7 +73,7 @@ func generateAndRestoreItems( nowLegacy = dttm.FormatToLegacy(time.Now()) id = uuid.NewString() subject = "automated " + now[:16] + " - " + id[:8] - body = "automated " + cat.String() + " generation for " + userID + " at " + now + " - " + id + body = "automated " + cat.HumanString() + " generation for " + userID + " at " + now + " - " + id ) items = append(items, item{ diff --git a/src/internal/m365/collection/drive/collections.go b/src/internal/m365/collection/drive/collections.go index 4d6925db6..40d4d7cd6 100644 --- a/src/internal/m365/collection/drive/collections.go +++ b/src/internal/m365/collection/drive/collections.go @@ -243,7 +243,9 @@ func (c *Collections) Get( driveTombstones[driveID] = struct{}{} } - progressBar := observe.MessageWithCompletion(ctx, observe.Bulletf("files")) + progressBar := observe.MessageWithCompletion( + ctx, + observe.Bulletf(path.FilesCategory.HumanString())) defer close(progressBar) // Enumerate drives for the specified resourceOwner diff --git a/src/internal/m365/collection/exchange/backup.go b/src/internal/m365/collection/exchange/backup.go index ae9354648..d0b565ad2 100644 --- a/src/internal/m365/collection/exchange/backup.go +++ b/src/internal/m365/collection/exchange/backup.go @@ -53,7 +53,7 @@ func CreateCollections( foldersComplete := observe.MessageWithCompletion( ctx, - observe.Bulletf("%s", qp.Category)) + observe.Bulletf("%s", qp.Category.HumanString())) defer close(foldersComplete) rootFolder, cc := handler.NewContainerCache(bpc.ProtectedResource.ID()) diff --git a/src/internal/m365/collection/exchange/collection.go b/src/internal/m365/collection/exchange/collection.go index 98cfb3e00..cd9587802 100644 --- a/src/internal/m365/collection/exchange/collection.go +++ b/src/internal/m365/collection/exchange/collection.go @@ -164,7 +164,7 @@ func (col *Collection) streamItems(ctx context.Context, errs *fault.Bus) { if len(col.added)+len(col.removed) > 0 { colProgress = observe.CollectionProgress( ctx, - col.FullPath().Category().String(), + col.FullPath().Category().HumanString(), col.LocationPath().Elements()) defer close(colProgress) } diff --git a/src/internal/m365/collection/exchange/restore.go b/src/internal/m365/collection/exchange/restore.go index 6d6b6721c..487ce0d4d 100644 --- a/src/internal/m365/collection/exchange/restore.go +++ b/src/internal/m365/collection/exchange/restore.go @@ -47,7 +47,7 @@ func RestoreCollection( colProgress := observe.CollectionProgress( ctx, - category.String(), + category.HumanString(), fullPath.Folder(false)) defer close(colProgress) diff --git a/src/internal/m365/collection/groups/collection.go b/src/internal/m365/collection/groups/collection.go index 190c3e688..02f672c6c 100644 --- a/src/internal/m365/collection/groups/collection.go +++ b/src/internal/m365/collection/groups/collection.go @@ -209,7 +209,7 @@ func (col *Collection) streamItems(ctx context.Context, errs *fault.Bus) { if len(col.added)+len(col.removed) > 0 { colProgress = observe.CollectionProgress( ctx, - col.FullPath().Category().String(), + col.FullPath().Category().HumanString(), col.LocationPath().Elements()) defer close(colProgress) } diff --git a/src/internal/m365/collection/site/collection.go b/src/internal/m365/collection/site/collection.go index e63e11388..c0bceafea 100644 --- a/src/internal/m365/collection/site/collection.go +++ b/src/internal/m365/collection/site/collection.go @@ -196,7 +196,7 @@ func (sc *Collection) runPopulate( // TODO: Insert correct ID for CollectionProgress colProgress := observe.CollectionProgress( ctx, - sc.fullPath.Category().String(), + sc.fullPath.Category().HumanString(), sc.fullPath.Folders()) defer close(colProgress) diff --git a/src/internal/m365/service/groups/backup.go b/src/internal/m365/service/groups/backup.go index d2bc44a3e..27f34f7b3 100644 --- a/src/internal/m365/service/groups/backup.go +++ b/src/internal/m365/service/groups/backup.go @@ -67,14 +67,9 @@ func ProduceBackupCollections( break } - catStr := scope.Category().PathType().String() - if scope.Category().PathType() == path.ChannelMessagesCategory { - catStr = "messages" - } - progressBar := observe.MessageWithCompletion( ctx, - observe.Bulletf("%s", catStr)) + observe.Bulletf("%s", scope.Category().PathType().HumanString())) defer close(progressBar) var dbcs []data.BackupCollection diff --git a/src/internal/m365/service/sharepoint/backup.go b/src/internal/m365/service/sharepoint/backup.go index e64f91a4c..c46dc6ef2 100644 --- a/src/internal/m365/service/sharepoint/backup.go +++ b/src/internal/m365/service/sharepoint/backup.go @@ -52,7 +52,7 @@ func ProduceBackupCollections( progressBar := observe.MessageWithCompletion( ctx, - observe.Bulletf("%s", scope.Category().PathType())) + observe.Bulletf("%s", scope.Category().PathType().HumanString())) defer close(progressBar) var spcs []data.BackupCollection diff --git a/src/pkg/path/category_type.go b/src/pkg/path/category_type.go index 85a5de6f4..b8c20020f 100644 --- a/src/pkg/path/category_type.go +++ b/src/pkg/path/category_type.go @@ -29,31 +29,47 @@ const ( ChannelMessagesCategory CategoryType = 9 // channelMessages ) -func ToCategoryType(category string) CategoryType { - cat := strings.ToLower(category) +var strToCat = map[string]CategoryType{ + strings.ToLower(EmailCategory.String()): EmailCategory, + strings.ToLower(ContactsCategory.String()): ContactsCategory, + strings.ToLower(EventsCategory.String()): EventsCategory, + strings.ToLower(FilesCategory.String()): FilesCategory, + strings.ToLower(LibrariesCategory.String()): LibrariesCategory, + strings.ToLower(ListsCategory.String()): ListsCategory, + strings.ToLower(PagesCategory.String()): PagesCategory, + strings.ToLower(DetailsCategory.String()): DetailsCategory, + strings.ToLower(ChannelMessagesCategory.String()): ChannelMessagesCategory, +} - switch cat { - case strings.ToLower(EmailCategory.String()): - return EmailCategory - case strings.ToLower(ContactsCategory.String()): - return ContactsCategory - case strings.ToLower(EventsCategory.String()): - return EventsCategory - case strings.ToLower(FilesCategory.String()): - return FilesCategory - case strings.ToLower(LibrariesCategory.String()): - return LibrariesCategory - case strings.ToLower(ListsCategory.String()): - return ListsCategory - case strings.ToLower(PagesCategory.String()): - return PagesCategory - case strings.ToLower(DetailsCategory.String()): - return DetailsCategory - case strings.ToLower(ChannelMessagesCategory.String()): - return ChannelMessagesCategory - default: - return UnknownCategory +func ToCategoryType(s string) CategoryType { + cat, ok := strToCat[strings.ToLower(s)] + if ok { + return cat } + + return UnknownCategory +} + +var catToHuman = map[CategoryType]string{ + EmailCategory: "Emails", + ContactsCategory: "Contacts", + EventsCategory: "Events", + FilesCategory: "Files", + LibrariesCategory: "Libraries", + ListsCategory: "Lists", + PagesCategory: "Pages", + DetailsCategory: "Details", + ChannelMessagesCategory: "Messages", +} + +// HumanString produces a more human-readable string version of the category. +func (cat CategoryType) HumanString() string { + hs, ok := catToHuman[cat] + if ok { + return hs + } + + return "Unknown Category" } // --------------------------------------------------------------------------- diff --git a/src/pkg/path/category_type_test.go b/src/pkg/path/category_type_test.go new file mode 100644 index 000000000..209f69924 --- /dev/null +++ b/src/pkg/path/category_type_test.go @@ -0,0 +1,72 @@ +package path + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/src/internal/tester" +) + +type CategoryTypeUnitSuite struct { + tester.Suite +} + +func TestCategoryTypeUnitSuite(t *testing.T) { + suite.Run(t, &CategoryTypeUnitSuite{Suite: tester.NewUnitSuite(t)}) +} + +func (suite *CategoryTypeUnitSuite) TestToCategoryType() { + table := []struct { + input string + expect CategoryType + }{ + {input: "unknown", expect: 0}, + {input: "EMAIL", expect: 1}, + {input: "Email", expect: 1}, + {input: "email", expect: 1}, + {input: "contacts", expect: 2}, + {input: "events", expect: 3}, + {input: "files", expect: 4}, + {input: "lists", expect: 5}, + {input: "libraries", expect: 6}, + {input: "pages", expect: 7}, + {input: "details", expect: 8}, + {input: "channelmessages", expect: 9}, + } + for _, test := range table { + suite.Run(test.input, func() { + assert.Equal( + suite.T(), + test.expect, + ToCategoryType(test.input)) + }) + } +} + +func (suite *CategoryTypeUnitSuite) TestHumanString() { + table := []struct { + input CategoryType + expect string + }{ + {input: 0, expect: "Unknown Category"}, + {input: 1, expect: "Emails"}, + {input: 2, expect: "Contacts"}, + {input: 3, expect: "Events"}, + {input: 4, expect: "Files"}, + {input: 5, expect: "Lists"}, + {input: 6, expect: "Libraries"}, + {input: 7, expect: "Pages"}, + {input: 8, expect: "Details"}, + {input: 9, expect: "Messages"}, + } + for _, test := range table { + suite.Run(test.input.String(), func() { + assert.Equal( + suite.T(), + test.expect, + test.input.HumanString()) + }) + } +}