From a13f1cc3a6ea03f6a35d92ca447049417ab12cf8 Mon Sep 17 00:00:00 2001 From: Keepers Date: Wed, 21 Dec 2022 11:08:26 -0700 Subject: [PATCH] remove common maps, add golang maps (#1885) ## Description replaces the common/maps.go code with the golang experimental maps package. The maps package provides standard api such as Keys, Values, Copy, and Clone. https://pkg.go.dev/golang.org/x/exp/maps ## Does this PR need a docs update or release note? - [x] :no_entry: No ## Type of change - [x] :hamster: Trivial/Minor ## Test Plan - [x] :zap: Unit test --- src/go.mod | 1 + src/go.sum | 2 ++ src/internal/common/configs.go | 6 ++--- src/internal/common/maps.go | 26 ------------------ .../connector/exchange/service_iterators.go | 3 +-- src/internal/connector/graph_connector.go | 27 ++++--------------- .../graph_connector_disconnected_test.go | 16 ----------- .../connector/graph_connector_test.go | 5 ++-- src/internal/kopia/model_store.go | 5 ++-- src/internal/kopia/snapshot_manager.go | 5 ++-- src/internal/kopia/wrapper_test.go | 5 ++-- src/internal/tester/config.go | 8 ++---- src/internal/tester/envvars.go | 6 ++--- src/pkg/selectors/scopes.go | 8 +++--- src/pkg/selectors/selectors.go | 7 ++--- 15 files changed, 30 insertions(+), 100 deletions(-) delete mode 100644 src/internal/common/maps.go diff --git a/src/go.mod b/src/go.mod index 6c843ed37..15e70a62f 100644 --- a/src/go.mod +++ b/src/go.mod @@ -25,6 +25,7 @@ require ( github.com/tomlazar/table v0.1.2 github.com/vbauerster/mpb/v8 v8.1.4 go.uber.org/zap v1.24.0 + golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15 golang.org/x/tools v0.4.0 gopkg.in/resty.v1 v1.12.0 ) diff --git a/src/go.sum b/src/go.sum index 4783ac2ae..2910b8dfd 100644 --- a/src/go.sum +++ b/src/go.sum @@ -444,6 +444,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15 h1:5oN1Pz/eDhCpbMbLstvIPa0b/BEQo6g6nwV3pLjfM6w= +golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/src/internal/common/configs.go b/src/internal/common/configs.go index 73e6c2630..57bdf527b 100644 --- a/src/internal/common/configs.go +++ b/src/internal/common/configs.go @@ -1,5 +1,7 @@ package common +import "golang.org/x/exp/maps" + type StringConfigurer interface { StringConfig() (map[string]string, error) } @@ -15,9 +17,7 @@ func UnionStringConfigs(cfgs ...StringConfigurer) (map[string]string, error) { return nil, err } - for k, v := range c { - union[k] = v - } + maps.Copy(union, c) } return union, nil diff --git a/src/internal/common/maps.go b/src/internal/common/maps.go deleted file mode 100644 index 5a8b873b2..000000000 --- a/src/internal/common/maps.go +++ /dev/null @@ -1,26 +0,0 @@ -package common - -// UnionMaps produces a new map containing all the values of the other -// maps. The last maps have the highes priority. Key collisions with -// earlier maps will favor the last map with that key. -func UnionMaps[K comparable, V any](ms ...map[K]V) map[K]V { - r := map[K]V{} - - for _, m := range ms { - for k, v := range m { - r[k] = v - } - } - - return r -} - -func CopyMap[K comparable, V any](m map[K]V) map[K]V { - r := map[K]V{} - - for k, v := range m { - r[k] = v - } - - return r -} diff --git a/src/internal/connector/exchange/service_iterators.go b/src/internal/connector/exchange/service_iterators.go index 25607d574..f97263054 100644 --- a/src/internal/connector/exchange/service_iterators.go +++ b/src/internal/connector/exchange/service_iterators.go @@ -11,7 +11,6 @@ import ( msuser "github.com/microsoftgraph/msgraph-sdk-go/users" "github.com/pkg/errors" - "github.com/alcionai/corso/src/internal/common" "github.com/alcionai/corso/src/internal/connector/graph" "github.com/alcionai/corso/src/internal/connector/support" "github.com/alcionai/corso/src/internal/data" @@ -62,7 +61,7 @@ func filterContainersAndFillCollections( currPaths = map[string]string{} // copy of previousPaths. any folder found in the resolver get // deleted from this map, leaving only the deleted maps behind - deletedPaths = common.CopyMap(dps) + deletedPaths = map[string]DeltaPath{} ) getJobs, err := getFetchIDFunc(qp.Category) diff --git a/src/internal/connector/graph_connector.go b/src/internal/connector/graph_connector.go index a75bcde7b..226be17cc 100644 --- a/src/internal/connector/graph_connector.go +++ b/src/internal/connector/graph_connector.go @@ -12,6 +12,7 @@ import ( msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core" "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/pkg/errors" + "golang.org/x/exp/maps" "github.com/alcionai/corso/src/internal/connector/discovery" "github.com/alcionai/corso/src/internal/connector/exchange" @@ -136,12 +137,12 @@ func (gc *GraphConnector) setTenantUsers(ctx context.Context) error { // GetUsers returns the email address of users within the tenant. func (gc *GraphConnector) GetUsers() []string { - return buildFromMap(true, gc.Users) + return maps.Keys(gc.Users) } // GetUsersIds returns the M365 id for the user func (gc *GraphConnector) GetUsersIds() []string { - return buildFromMap(false, gc.Users) + return maps.Values(gc.Users) } // setTenantSites queries the M365 to identify the sites in the @@ -202,12 +203,12 @@ func identifySite(item any) (string, string, error) { // GetSiteWebURLs returns the WebURLs of sharepoint sites within the tenant. func (gc *GraphConnector) GetSiteWebURLs() []string { - return buildFromMap(true, gc.Sites) + return maps.Keys(gc.Sites) } // GetSiteIds returns the canonical site IDs in the tenant func (gc *GraphConnector) GetSiteIDs() []string { - return buildFromMap(false, gc.Sites) + return maps.Values(gc.Sites) } // UnionSiteIDsAndWebURLs reduces the id and url slices into a single slice of site IDs. @@ -246,24 +247,6 @@ func (gc *GraphConnector) UnionSiteIDsAndWebURLs(ctx context.Context, ids, urls return idsl, nil } -// buildFromMap helper function for returning []string from map. -// Returns list of keys iff true; otherwise returns a list of values -func buildFromMap(isKey bool, mapping map[string]string) []string { - returnString := make([]string, 0) - - if isKey { - for k := range mapping { - returnString = append(returnString, k) - } - } else { - for _, v := range mapping { - returnString = append(returnString, v) - } - } - - return returnString -} - // RestoreDataCollections restores data from the specified collections // into M365 using the GraphAPI. // SideEffect: gc.status is updated at the completion of operation diff --git a/src/internal/connector/graph_connector_disconnected_test.go b/src/internal/connector/graph_connector_disconnected_test.go index 53cce1012..caaeffa28 100644 --- a/src/internal/connector/graph_connector_disconnected_test.go +++ b/src/internal/connector/graph_connector_disconnected_test.go @@ -72,22 +72,6 @@ func (suite *DisconnectedGraphConnectorSuite) TestBadConnection() { } } -func (suite *DisconnectedGraphConnectorSuite) TestBuild() { - names := make(map[string]string) - names["Al"] = "Bundy" - names["Ellen"] = "Ripley" - names["Axel"] = "Foley" - first := buildFromMap(true, names) - last := buildFromMap(false, names) - - suite.Contains(first, "Al") - suite.Contains(first, "Ellen") - suite.Contains(first, "Axel") - suite.Contains(last, "Bundy") - suite.Contains(last, "Ripley") - suite.Contains(last, "Foley") -} - func statusTestTask(gc *GraphConnector, objects, success, folder int) { ctx, flush := tester.NewContext() defer flush() diff --git a/src/internal/connector/graph_connector_test.go b/src/internal/connector/graph_connector_test.go index 6b9db24e4..47993dc37 100644 --- a/src/internal/connector/graph_connector_test.go +++ b/src/internal/connector/graph_connector_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "golang.org/x/exp/maps" "github.com/alcionai/corso/src/internal/connector/graph" "github.com/alcionai/corso/src/internal/connector/mockconnector" @@ -336,9 +337,7 @@ func runRestoreBackupTest( collections = append(collections, userCollections...) totalItems += numItems - for k, v := range userExpectedData { - expectedData[k] = v - } + maps.Copy(expectedData, userExpectedData) } t.Logf( diff --git a/src/internal/kopia/model_store.go b/src/internal/kopia/model_store.go index 7efe687f2..495e94cff 100644 --- a/src/internal/kopia/model_store.go +++ b/src/internal/kopia/model_store.go @@ -8,6 +8,7 @@ import ( "github.com/kopia/kopia/repo" "github.com/kopia/kopia/repo/manifest" "github.com/pkg/errors" + "golang.org/x/exp/maps" "github.com/alcionai/corso/src/internal/model" ) @@ -64,9 +65,7 @@ func tagsForModel(s model.Schema, tags map[string]string) (map[string]string, er res := make(map[string]string, len(tags)+1) res[manifest.TypeLabelKey] = s.String() - for k, v := range tags { - res[k] = v - } + maps.Copy(res, tags) return res, nil } diff --git a/src/internal/kopia/snapshot_manager.go b/src/internal/kopia/snapshot_manager.go index d09d59eba..ef8e82e01 100644 --- a/src/internal/kopia/snapshot_manager.go +++ b/src/internal/kopia/snapshot_manager.go @@ -7,6 +7,7 @@ import ( "github.com/kopia/kopia/repo/manifest" "github.com/kopia/kopia/snapshot" "github.com/pkg/errors" + "golang.org/x/exp/maps" "github.com/alcionai/corso/src/pkg/logger" "github.com/alcionai/corso/src/pkg/path" @@ -198,9 +199,7 @@ func fetchPrevManifests( resourceOwner: "", }) - for k, v := range tags { - allTags[k] = v - } + maps.Copy(allTags, tags) reason := Reason{ ResourceOwner: resourceOwner, diff --git a/src/internal/kopia/wrapper_test.go b/src/internal/kopia/wrapper_test.go index 440536437..9221e97ab 100644 --- a/src/internal/kopia/wrapper_test.go +++ b/src/internal/kopia/wrapper_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "golang.org/x/exp/maps" "github.com/alcionai/corso/src/internal/connector/mockconnector" "github.com/alcionai/corso/src/internal/data" @@ -241,9 +242,7 @@ func (suite *KopiaIntegrationSuite) TestBackupCollections() { expectedTags[tk] = tv } - for k, v := range normalizeTagKVs(customTags) { - expectedTags[k] = v - } + maps.Copy(expectedTags, normalizeTagKVs(customTags)) table := []struct { name string diff --git a/src/internal/tester/config.go b/src/internal/tester/config.go index 078fcfadc..4c2970f15 100644 --- a/src/internal/tester/config.go +++ b/src/internal/tester/config.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/viper" + "golang.org/x/exp/maps" "github.com/alcionai/corso/src/pkg/account" ) @@ -48,12 +49,7 @@ func cloneTestConfig() map[string]string { return map[string]string{} } - clone := map[string]string{} - for k, v := range testConfig { - clone[k] = v - } - - return clone + return maps.Clone(testConfig) } func NewTestViper() (*viper.Viper, error) { diff --git a/src/internal/tester/envvars.go b/src/internal/tester/envvars.go index 12a11cb2b..9cf7b7fa1 100644 --- a/src/internal/tester/envvars.go +++ b/src/internal/tester/envvars.go @@ -3,6 +3,8 @@ package tester import ( "errors" "os" + + "golang.org/x/exp/maps" ) // GetRequiredEnvVars retrieves the provided env vars from the os. @@ -35,9 +37,7 @@ func GetRequiredEnvSls(evs ...[]string) (map[string]string, error) { return nil, err } - for k, v := range r { - vals[k] = v - } + maps.Copy(vals, r) } return vals, nil diff --git a/src/pkg/selectors/scopes.go b/src/pkg/selectors/scopes.go index b0c1d4e96..924db2ef8 100644 --- a/src/pkg/selectors/scopes.go +++ b/src/pkg/selectors/scopes.go @@ -3,6 +3,8 @@ package selectors import ( "context" + "golang.org/x/exp/maps" + D "github.com/alcionai/corso/src/internal/diagnostics" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/filters" @@ -250,11 +252,7 @@ func set[T scopeT](s T, cat categorizer, v []string, opts ...option) T { // discreteCopy makes a shallow clone of the scocpe, and sets the resource // owner filter target in the clone to the provided string. func discreteCopy[T scopeT](s T, resourceOwner string) T { - clone := T{} - - for k, v := range s { - clone[k] = v - } + clone := maps.Clone(s) return set( clone, diff --git a/src/pkg/selectors/selectors.go b/src/pkg/selectors/selectors.go index e25f66350..051398eb4 100644 --- a/src/pkg/selectors/selectors.go +++ b/src/pkg/selectors/selectors.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/pkg/errors" + "golang.org/x/exp/maps" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/filters" @@ -185,11 +186,7 @@ func discreteScopes[T scopeT, C categoryT]( t := T(v) if isAnyTarget(t, rootCat) { - w := T{} - for k, v := range t { - w[k] = v - } - + w := maps.Clone(t) set(w, rootCat, discreteIDs) t = w }