use scope type instead of map (#597)

This commit is contained in:
Keepers 2022-08-18 12:06:22 -06:00 committed by GitHub
parent e198aa9ae7
commit c13ef798eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 51 deletions

View File

@ -156,8 +156,17 @@ func extendExchangeScopeValues(es []ExchangeScope) []ExchangeScope {
return es return es
} }
// Scopes retrieves the list of exchangeScopes in the selector.
func (s *exchange) Scopes() []ExchangeScope {
scopes := []ExchangeScope{}
for _, v := range s.Includes {
scopes = append(scopes, ExchangeScope(v))
}
return scopes
}
// ------------------- // -------------------
// Scope Factory // Scope Factories
func makeExchangeScope(granularity string, cat exchangeCategory, vs []string) ExchangeScope { func makeExchangeScope(granularity string, cat exchangeCategory, vs []string) ExchangeScope {
return ExchangeScope{ return ExchangeScope{
@ -380,21 +389,12 @@ func (d ExchangeDestination) Set(cat exchangeCategory, dest string) error {
type ( type (
// ExchangeScope specifies the data available // ExchangeScope specifies the data available
// when interfacing with the Exchange service. // when interfacing with the Exchange service.
ExchangeScope map[string]string ExchangeScope scope
// exchangeCategory enumerates the type of the lowest level // exchangeCategory enumerates the type of the lowest level
// of data () in a scope. // of data () in a scope.
exchangeCategory int exchangeCategory int
) )
// Scopes retrieves the list of exchangeScopes in the selector.
func (s *exchange) Scopes() []ExchangeScope {
scopes := []ExchangeScope{}
for _, v := range s.Includes {
scopes = append(scopes, ExchangeScope(v))
}
return scopes
}
//go:generate stringer -type=exchangeCategory //go:generate stringer -type=exchangeCategory
const ( const (
ExchangeCategoryUnknown exchangeCategory = iota ExchangeCategoryUnknown exchangeCategory = iota
@ -499,7 +499,7 @@ func (s ExchangeScope) Contains(cat exchangeCategory, target string) bool {
if !s.IncludesCategory(cat) { if !s.IncludesCategory(cat) {
return false return false
} }
return contains(s, cat.String(), target) return contains(scope(s), cat.String(), target)
} }
// returns true if the category is included in the scope's data type, // returns true if the category is included in the scope's data type,
@ -706,7 +706,7 @@ func (sr *ExchangeRestore) Reduce(deets *details.Details) *details.Details {
// groups each scope by its category of data (contact, event, or mail). // groups each scope by its category of data (contact, event, or mail).
// user-level scopes will duplicate to all three categories. // user-level scopes will duplicate to all three categories.
func exchangeScopesByCategory(scopes []map[string]string) map[string][]ExchangeScope { func exchangeScopesByCategory(scopes []scope) map[string][]ExchangeScope {
m := map[string][]ExchangeScope{ m := map[string][]ExchangeScope{
ExchangeContact.String(): {}, ExchangeContact.String(): {},
ExchangeEvent.String(): {}, ExchangeEvent.String(): {},

View File

@ -373,7 +373,7 @@ func (suite *ExchangeSourceSuite) TestExchangeDestination_GetOrDefault() {
} }
} }
var allScopesExceptUnknown = map[string]string{ var allScopesExceptUnknown = scope{
ExchangeContact.String(): AnyTgt, ExchangeContact.String(): AnyTgt,
ExchangeContactFolder.String(): AnyTgt, ExchangeContactFolder.String(): AnyTgt,
ExchangeEvent.String(): AnyTgt, ExchangeEvent.String(): AnyTgt,
@ -384,7 +384,7 @@ var allScopesExceptUnknown = map[string]string{
func (suite *ExchangeSourceSuite) TestExchangeBackup_Scopes() { func (suite *ExchangeSourceSuite) TestExchangeBackup_Scopes() {
eb := NewExchangeBackup() eb := NewExchangeBackup()
eb.Includes = []map[string]string{allScopesExceptUnknown} eb.Includes = []scope{allScopesExceptUnknown}
// todo: swap the above for this // todo: swap the above for this
// eb := NewExchangeBackup().IncludeUsers(AnyTgt) // eb := NewExchangeBackup().IncludeUsers(AnyTgt)
@ -393,7 +393,7 @@ func (suite *ExchangeSourceSuite) TestExchangeBackup_Scopes() {
assert.Equal( assert.Equal(
suite.T(), suite.T(),
allScopesExceptUnknown, allScopesExceptUnknown,
map[string]string(scopes[0])) scope(scopes[0]))
} }
func (suite *ExchangeSourceSuite) TestExchangeScope_Category() { func (suite *ExchangeSourceSuite) TestExchangeScope_Category() {
@ -420,7 +420,7 @@ func (suite *ExchangeSourceSuite) TestExchangeScope_Category() {
for _, test := range table { for _, test := range table {
suite.T().Run(test.is.String()+test.expect.String(), func(t *testing.T) { suite.T().Run(test.is.String()+test.expect.String(), func(t *testing.T) {
eb := NewExchangeBackup() eb := NewExchangeBackup()
eb.Includes = []map[string]string{{scopeKeyCategory: test.is.String()}} eb.Includes = []scope{{scopeKeyCategory: test.is.String()}}
scope := eb.Scopes()[0] scope := eb.Scopes()[0]
test.check(t, test.expect, scope.Category()) test.check(t, test.expect, scope.Category())
}) })
@ -452,7 +452,7 @@ func (suite *ExchangeSourceSuite) TestExchangeScope_IncludesCategory() {
for _, test := range table { for _, test := range table {
suite.T().Run(test.is.String()+test.expect.String(), func(t *testing.T) { suite.T().Run(test.is.String()+test.expect.String(), func(t *testing.T) {
eb := NewExchangeBackup() eb := NewExchangeBackup()
eb.Includes = []map[string]string{{scopeKeyCategory: test.is.String()}} eb.Includes = []scope{{scopeKeyCategory: test.is.String()}}
scope := eb.Scopes()[0] scope := eb.Scopes()[0]
test.check(t, scope.IncludesCategory(test.expect)) test.check(t, scope.IncludesCategory(test.expect))
}) })
@ -461,7 +461,7 @@ func (suite *ExchangeSourceSuite) TestExchangeScope_IncludesCategory() {
func (suite *ExchangeSourceSuite) TestExchangeScope_Get() { func (suite *ExchangeSourceSuite) TestExchangeScope_Get() {
eb := NewExchangeBackup() eb := NewExchangeBackup()
eb.Includes = []map[string]string{allScopesExceptUnknown} eb.Includes = []scope{allScopesExceptUnknown}
// todo: swap the above for this // todo: swap the above for this
// eb := NewExchangeBackup().IncludeUsers(AnyTgt) // eb := NewExchangeBackup().IncludeUsers(AnyTgt)
@ -794,12 +794,12 @@ func (suite *ExchangeSourceSuite) TestExchangeScopesByCategory() {
event int event int
mail int mail int
} }
type input []map[string]string type input []scope
makeInput := func(es ...[]ExchangeScope) []map[string]string { makeInput := func(es ...[]ExchangeScope) []scope {
mss := []map[string]string{} mss := []scope{}
for _, sl := range es { for _, sl := range es {
for _, s := range sl { for _, s := range sl {
mss = append(mss, map[string]string(s)) mss = append(mss, scope(s))
} }
} }
return mss return mss

View File

@ -67,20 +67,20 @@ type Selector struct {
Service service `json:"service,omitempty"` Service service `json:"service,omitempty"`
// A slice of exclusion scopes. Exclusions apply globally to all // A slice of exclusion scopes. Exclusions apply globally to all
// inclusions/filters, with any-match behavior. // inclusions/filters, with any-match behavior.
Excludes []map[string]string `json:"exclusions,omitempty"` Excludes []scope `json:"exclusions,omitempty"`
// A slice of filter scopes. All inclusions must also match ALL filters. // A slice of filter scopes. All inclusions must also match ALL filters.
Filters []map[string]string `json:"filters,omitempty"` Filters []scope `json:"filters,omitempty"`
// A slice of inclusion scopes. Comparators must match either one of these, // A slice of inclusion scopes. Comparators must match either one of these,
// or all filters, to be included. // or all filters, to be included.
Includes []map[string]string `json:"includes,omitempty"` Includes []scope `json:"includes,omitempty"`
} }
// helper for specific selector instance constructors. // helper for specific selector instance constructors.
func newSelector(s service) Selector { func newSelector(s service) Selector {
return Selector{ return Selector{
Service: s, Service: s,
Excludes: []map[string]string{}, Excludes: []scope{},
Includes: []map[string]string{}, Includes: []scope{},
} }
} }
@ -115,14 +115,14 @@ func appendExcludes[T baseScope](
scopes ...[]T, scopes ...[]T,
) { ) {
if s.Excludes == nil { if s.Excludes == nil {
s.Excludes = []map[string]string{} s.Excludes = []scope{}
} }
concat := []T{} concat := []T{}
for _, scopeSl := range scopes { for _, scopeSl := range scopes {
concat = append(concat, tform(scopeSl)...) concat = append(concat, tform(scopeSl)...)
} }
for _, sc := range concat { for _, sc := range concat {
s.Excludes = append(s.Excludes, map[string]string(sc)) s.Excludes = append(s.Excludes, scope(sc))
} }
} }
@ -132,14 +132,14 @@ func appendFilters[T baseScope](
scopes ...[]T, scopes ...[]T,
) { ) {
if s.Filters == nil { if s.Filters == nil {
s.Filters = []map[string]string{} s.Filters = []scope{}
} }
concat := []T{} concat := []T{}
for _, scopeSl := range scopes { for _, scopeSl := range scopes {
concat = append(concat, tform(scopeSl)...) concat = append(concat, tform(scopeSl)...)
} }
for _, sc := range concat { for _, sc := range concat {
s.Filters = append(s.Filters, map[string]string(sc)) s.Filters = append(s.Filters, scope(sc))
} }
} }
@ -149,21 +149,21 @@ func appendIncludes[T baseScope](
scopes ...[]T, scopes ...[]T,
) { ) {
if s.Includes == nil { if s.Includes == nil {
s.Includes = []map[string]string{} s.Includes = []scope{}
} }
concat := []T{} concat := []T{}
for _, scopeSl := range scopes { for _, scopeSl := range scopes {
concat = append(concat, tform(scopeSl)...) concat = append(concat, tform(scopeSl)...)
} }
for _, sc := range concat { for _, sc := range concat {
s.Includes = append(s.Includes, map[string]string(sc)) s.Includes = append(s.Includes, scope(sc))
} }
} }
// contains returns true if the provided scope is Any(), or contains the // contains returns true if the provided scope is Any(), or contains the
// target string. // target string.
func contains(scope map[string]string, key, target string) bool { func contains(sc scope, key, target string) bool {
compare := scope[key] compare := sc[key]
if len(compare) == 0 { if len(compare) == 0 {
return false return false
} }
@ -234,7 +234,7 @@ func resourcesShortFormat(m map[string][]string) string {
// Transforms the slice to a single map. // Transforms the slice to a single map.
// Keys are each map's scopeKeyResource value. // Keys are each map's scopeKeyResource value.
// Values are the set of all scopeKeyDataTypes for a given resource. // Values are the set of all scopeKeyDataTypes for a given resource.
func toResourceTypeMap(ms []map[string]string) map[string][]string { func toResourceTypeMap(ms []scope) map[string][]string {
if len(ms) == 0 { if len(ms) == 0 {
return nil return nil
} }
@ -268,7 +268,7 @@ func addToSet(set []string, v string) []string {
// Destination // Destination
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
type Destination map[string]string type Destination scope
var ErrorDestinationAlreadySet = errors.New("destination is already declared") var ErrorDestinationAlreadySet = errors.New("destination is already declared")

View File

@ -11,9 +11,9 @@ import (
func stubSelector() Selector { func stubSelector() Selector {
return Selector{ return Selector{
Service: ServiceExchange, Service: ServiceExchange,
Excludes: []map[string]string{stubScope("")}, Excludes: []scope{scope(stubScope(""))},
Filters: []map[string]string{stubScope("")}, Filters: []scope{scope(stubScope(""))},
Includes: []map[string]string{stubScope("")}, Includes: []scope{scope(stubScope(""))},
} }
} }
@ -64,8 +64,8 @@ func (suite *SelectorSuite) TestPrintable_IncludedResources() {
assert.Equal(t, stubResource, res, "resource should state only the user") assert.Equal(t, stubResource, res, "resource should state only the user")
sel.Includes = []map[string]string{ sel.Includes = []scope{
stubScope(""), scope(stubScope("")),
{scopeKeyResource: "smarf", scopeKeyDataType: unknownCatStub.String()}, {scopeKeyResource: "smarf", scopeKeyDataType: unknownCatStub.String()},
{scopeKeyResource: "smurf", scopeKeyDataType: unknownCatStub.String()}, {scopeKeyResource: "smurf", scopeKeyDataType: unknownCatStub.String()},
} }
@ -88,20 +88,20 @@ func (suite *SelectorSuite) TestPrintable_IncludedResources() {
func (suite *SelectorSuite) TestToResourceTypeMap() { func (suite *SelectorSuite) TestToResourceTypeMap() {
table := []struct { table := []struct {
name string name string
input []map[string]string input []scope
expect map[string][]string expect map[string][]string
}{ }{
{ {
name: "single scope", name: "single scope",
input: []map[string]string{stubScope("")}, input: []scope{scope(stubScope(""))},
expect: map[string][]string{ expect: map[string][]string{
stubResource: {rootCatStub.String()}, stubResource: {rootCatStub.String()},
}, },
}, },
{ {
name: "disjoint resources", name: "disjoint resources",
input: []map[string]string{ input: []scope{
stubScope(""), scope(stubScope("")),
{ {
scopeKeyResource: "smarf", scopeKeyResource: "smarf",
scopeKeyDataType: unknownCatStub.String(), scopeKeyDataType: unknownCatStub.String(),
@ -114,8 +114,8 @@ func (suite *SelectorSuite) TestToResourceTypeMap() {
}, },
{ {
name: "disjoint types", name: "disjoint types",
input: []map[string]string{ input: []scope{
stubScope(""), scope(stubScope("")),
{ {
scopeKeyResource: stubResource, scopeKeyResource: stubResource,
scopeKeyDataType: "other", scopeKeyDataType: "other",
@ -138,8 +138,8 @@ func (suite *SelectorSuite) TestContains() {
t := suite.T() t := suite.T()
key := unknownCatStub.String() key := unknownCatStub.String()
target := "fnords" target := "fnords"
does := map[string]string{key: target} does := scope{key: target}
doesNot := map[string]string{key: "smarf"} doesNot := scope{key: "smarf"}
assert.True(t, contains(does, key, target)) assert.True(t, contains(does, key, target))
assert.False(t, contains(doesNot, key, target)) assert.False(t, contains(doesNot, key, target))
} }