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 

## Type of change

- [x] 🐹 Trivial/Minor

## Test Plan

- [x]  Unit test
This commit is contained in:
Keepers 2022-12-21 11:08:26 -07:00 committed by GitHub
parent e31267b579
commit a13f1cc3a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 30 additions and 100 deletions

View File

@ -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
)

View File

@ -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=

View File

@ -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

View File

@ -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
}

View File

@ -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)

View File

@ -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

View File

@ -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()

View File

@ -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(

View File

@ -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
}

View File

@ -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,

View File

@ -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

View File

@ -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) {

View File

@ -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

View File

@ -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,

View File

@ -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
}