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:
parent
e31267b579
commit
a13f1cc3a6
@ -25,6 +25,7 @@ require (
|
|||||||
github.com/tomlazar/table v0.1.2
|
github.com/tomlazar/table v0.1.2
|
||||||
github.com/vbauerster/mpb/v8 v8.1.4
|
github.com/vbauerster/mpb/v8 v8.1.4
|
||||||
go.uber.org/zap v1.24.0
|
go.uber.org/zap v1.24.0
|
||||||
|
golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15
|
||||||
golang.org/x/tools v0.4.0
|
golang.org/x/tools v0.4.0
|
||||||
gopkg.in/resty.v1 v1.12.0
|
gopkg.in/resty.v1 v1.12.0
|
||||||
)
|
)
|
||||||
|
|||||||
@ -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-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-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-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-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/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=
|
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
|
import "golang.org/x/exp/maps"
|
||||||
|
|
||||||
type StringConfigurer interface {
|
type StringConfigurer interface {
|
||||||
StringConfig() (map[string]string, error)
|
StringConfig() (map[string]string, error)
|
||||||
}
|
}
|
||||||
@ -15,9 +17,7 @@ func UnionStringConfigs(cfgs ...StringConfigurer) (map[string]string, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range c {
|
maps.Copy(union, c)
|
||||||
union[k] = v
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return union, nil
|
return union, nil
|
||||||
|
|||||||
@ -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
|
|
||||||
}
|
|
||||||
@ -11,7 +11,6 @@ import (
|
|||||||
msuser "github.com/microsoftgraph/msgraph-sdk-go/users"
|
msuser "github.com/microsoftgraph/msgraph-sdk-go/users"
|
||||||
"github.com/pkg/errors"
|
"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/graph"
|
||||||
"github.com/alcionai/corso/src/internal/connector/support"
|
"github.com/alcionai/corso/src/internal/connector/support"
|
||||||
"github.com/alcionai/corso/src/internal/data"
|
"github.com/alcionai/corso/src/internal/data"
|
||||||
@ -62,7 +61,7 @@ func filterContainersAndFillCollections(
|
|||||||
currPaths = map[string]string{}
|
currPaths = map[string]string{}
|
||||||
// copy of previousPaths. any folder found in the resolver get
|
// copy of previousPaths. any folder found in the resolver get
|
||||||
// deleted from this map, leaving only the deleted maps behind
|
// deleted from this map, leaving only the deleted maps behind
|
||||||
deletedPaths = common.CopyMap(dps)
|
deletedPaths = map[string]DeltaPath{}
|
||||||
)
|
)
|
||||||
|
|
||||||
getJobs, err := getFetchIDFunc(qp.Category)
|
getJobs, err := getFetchIDFunc(qp.Category)
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import (
|
|||||||
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
|
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
|
||||||
"github.com/microsoftgraph/msgraph-sdk-go/models"
|
"github.com/microsoftgraph/msgraph-sdk-go/models"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/internal/connector/discovery"
|
"github.com/alcionai/corso/src/internal/connector/discovery"
|
||||||
"github.com/alcionai/corso/src/internal/connector/exchange"
|
"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.
|
// GetUsers returns the email address of users within the tenant.
|
||||||
func (gc *GraphConnector) GetUsers() []string {
|
func (gc *GraphConnector) GetUsers() []string {
|
||||||
return buildFromMap(true, gc.Users)
|
return maps.Keys(gc.Users)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsersIds returns the M365 id for the user
|
// GetUsersIds returns the M365 id for the user
|
||||||
func (gc *GraphConnector) GetUsersIds() []string {
|
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
|
// 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.
|
// GetSiteWebURLs returns the WebURLs of sharepoint sites within the tenant.
|
||||||
func (gc *GraphConnector) GetSiteWebURLs() []string {
|
func (gc *GraphConnector) GetSiteWebURLs() []string {
|
||||||
return buildFromMap(true, gc.Sites)
|
return maps.Keys(gc.Sites)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSiteIds returns the canonical site IDs in the tenant
|
// GetSiteIds returns the canonical site IDs in the tenant
|
||||||
func (gc *GraphConnector) GetSiteIDs() []string {
|
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.
|
// 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
|
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
|
// RestoreDataCollections restores data from the specified collections
|
||||||
// into M365 using the GraphAPI.
|
// into M365 using the GraphAPI.
|
||||||
// SideEffect: gc.status is updated at the completion of operation
|
// SideEffect: gc.status is updated at the completion of operation
|
||||||
|
|||||||
@ -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) {
|
func statusTestTask(gc *GraphConnector, objects, success, folder int) {
|
||||||
ctx, flush := tester.NewContext()
|
ctx, flush := tester.NewContext()
|
||||||
defer flush()
|
defer flush()
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/stretchr/testify/suite"
|
"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/graph"
|
||||||
"github.com/alcionai/corso/src/internal/connector/mockconnector"
|
"github.com/alcionai/corso/src/internal/connector/mockconnector"
|
||||||
@ -336,9 +337,7 @@ func runRestoreBackupTest(
|
|||||||
collections = append(collections, userCollections...)
|
collections = append(collections, userCollections...)
|
||||||
totalItems += numItems
|
totalItems += numItems
|
||||||
|
|
||||||
for k, v := range userExpectedData {
|
maps.Copy(expectedData, userExpectedData)
|
||||||
expectedData[k] = v
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Logf(
|
t.Logf(
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/kopia/kopia/repo"
|
"github.com/kopia/kopia/repo"
|
||||||
"github.com/kopia/kopia/repo/manifest"
|
"github.com/kopia/kopia/repo/manifest"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/internal/model"
|
"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 := make(map[string]string, len(tags)+1)
|
||||||
res[manifest.TypeLabelKey] = s.String()
|
res[manifest.TypeLabelKey] = s.String()
|
||||||
|
|
||||||
for k, v := range tags {
|
maps.Copy(res, tags)
|
||||||
res[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/kopia/kopia/repo/manifest"
|
"github.com/kopia/kopia/repo/manifest"
|
||||||
"github.com/kopia/kopia/snapshot"
|
"github.com/kopia/kopia/snapshot"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/pkg/logger"
|
"github.com/alcionai/corso/src/pkg/logger"
|
||||||
"github.com/alcionai/corso/src/pkg/path"
|
"github.com/alcionai/corso/src/pkg/path"
|
||||||
@ -198,9 +199,7 @@ func fetchPrevManifests(
|
|||||||
resourceOwner: "",
|
resourceOwner: "",
|
||||||
})
|
})
|
||||||
|
|
||||||
for k, v := range tags {
|
maps.Copy(allTags, tags)
|
||||||
allTags[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
reason := Reason{
|
reason := Reason{
|
||||||
ResourceOwner: resourceOwner,
|
ResourceOwner: resourceOwner,
|
||||||
|
|||||||
@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/internal/connector/mockconnector"
|
"github.com/alcionai/corso/src/internal/connector/mockconnector"
|
||||||
"github.com/alcionai/corso/src/internal/data"
|
"github.com/alcionai/corso/src/internal/data"
|
||||||
@ -241,9 +242,7 @@ func (suite *KopiaIntegrationSuite) TestBackupCollections() {
|
|||||||
expectedTags[tk] = tv
|
expectedTags[tk] = tv
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range normalizeTagKVs(customTags) {
|
maps.Copy(expectedTags, normalizeTagKVs(customTags))
|
||||||
expectedTags[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
table := []struct {
|
table := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/pkg/account"
|
"github.com/alcionai/corso/src/pkg/account"
|
||||||
)
|
)
|
||||||
@ -48,12 +49,7 @@ func cloneTestConfig() map[string]string {
|
|||||||
return map[string]string{}
|
return map[string]string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
clone := map[string]string{}
|
return maps.Clone(testConfig)
|
||||||
for k, v := range testConfig {
|
|
||||||
clone[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
return clone
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTestViper() (*viper.Viper, error) {
|
func NewTestViper() (*viper.Viper, error) {
|
||||||
|
|||||||
@ -3,6 +3,8 @@ package tester
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetRequiredEnvVars retrieves the provided env vars from the os.
|
// GetRequiredEnvVars retrieves the provided env vars from the os.
|
||||||
@ -35,9 +37,7 @@ func GetRequiredEnvSls(evs ...[]string) (map[string]string, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range r {
|
maps.Copy(vals, r)
|
||||||
vals[k] = v
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return vals, nil
|
return vals, nil
|
||||||
|
|||||||
@ -3,6 +3,8 @@ package selectors
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
D "github.com/alcionai/corso/src/internal/diagnostics"
|
D "github.com/alcionai/corso/src/internal/diagnostics"
|
||||||
"github.com/alcionai/corso/src/pkg/backup/details"
|
"github.com/alcionai/corso/src/pkg/backup/details"
|
||||||
"github.com/alcionai/corso/src/pkg/filters"
|
"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
|
// discreteCopy makes a shallow clone of the scocpe, and sets the resource
|
||||||
// owner filter target in the clone to the provided string.
|
// owner filter target in the clone to the provided string.
|
||||||
func discreteCopy[T scopeT](s T, resourceOwner string) T {
|
func discreteCopy[T scopeT](s T, resourceOwner string) T {
|
||||||
clone := T{}
|
clone := maps.Clone(s)
|
||||||
|
|
||||||
for k, v := range s {
|
|
||||||
clone[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
return set(
|
return set(
|
||||||
clone,
|
clone,
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/pkg/backup/details"
|
"github.com/alcionai/corso/src/pkg/backup/details"
|
||||||
"github.com/alcionai/corso/src/pkg/filters"
|
"github.com/alcionai/corso/src/pkg/filters"
|
||||||
@ -185,11 +186,7 @@ func discreteScopes[T scopeT, C categoryT](
|
|||||||
t := T(v)
|
t := T(v)
|
||||||
|
|
||||||
if isAnyTarget(t, rootCat) {
|
if isAnyTarget(t, rootCat) {
|
||||||
w := T{}
|
w := maps.Clone(t)
|
||||||
for k, v := range t {
|
|
||||||
w[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
set(w, rootCat, discreteIDs)
|
set(w, rootCat, discreteIDs)
|
||||||
t = w
|
t = w
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user