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
}
// 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 {
return ExchangeScope{
@ -380,21 +389,12 @@ func (d ExchangeDestination) Set(cat exchangeCategory, dest string) error {
type (
// ExchangeScope specifies the data available
// when interfacing with the Exchange service.
ExchangeScope map[string]string
ExchangeScope scope
// exchangeCategory enumerates the type of the lowest level
// of data () in a scope.
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
const (
ExchangeCategoryUnknown exchangeCategory = iota
@ -499,7 +499,7 @@ func (s ExchangeScope) Contains(cat exchangeCategory, target string) bool {
if !s.IncludesCategory(cat) {
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,
@ -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).
// 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{
ExchangeContact.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,
ExchangeContactFolder.String(): AnyTgt,
ExchangeEvent.String(): AnyTgt,
@ -384,7 +384,7 @@ var allScopesExceptUnknown = map[string]string{
func (suite *ExchangeSourceSuite) TestExchangeBackup_Scopes() {
eb := NewExchangeBackup()
eb.Includes = []map[string]string{allScopesExceptUnknown}
eb.Includes = []scope{allScopesExceptUnknown}
// todo: swap the above for this
// eb := NewExchangeBackup().IncludeUsers(AnyTgt)
@ -393,7 +393,7 @@ func (suite *ExchangeSourceSuite) TestExchangeBackup_Scopes() {
assert.Equal(
suite.T(),
allScopesExceptUnknown,
map[string]string(scopes[0]))
scope(scopes[0]))
}
func (suite *ExchangeSourceSuite) TestExchangeScope_Category() {
@ -420,7 +420,7 @@ func (suite *ExchangeSourceSuite) TestExchangeScope_Category() {
for _, test := range table {
suite.T().Run(test.is.String()+test.expect.String(), func(t *testing.T) {
eb := NewExchangeBackup()
eb.Includes = []map[string]string{{scopeKeyCategory: test.is.String()}}
eb.Includes = []scope{{scopeKeyCategory: test.is.String()}}
scope := eb.Scopes()[0]
test.check(t, test.expect, scope.Category())
})
@ -452,7 +452,7 @@ func (suite *ExchangeSourceSuite) TestExchangeScope_IncludesCategory() {
for _, test := range table {
suite.T().Run(test.is.String()+test.expect.String(), func(t *testing.T) {
eb := NewExchangeBackup()
eb.Includes = []map[string]string{{scopeKeyCategory: test.is.String()}}
eb.Includes = []scope{{scopeKeyCategory: test.is.String()}}
scope := eb.Scopes()[0]
test.check(t, scope.IncludesCategory(test.expect))
})
@ -461,7 +461,7 @@ func (suite *ExchangeSourceSuite) TestExchangeScope_IncludesCategory() {
func (suite *ExchangeSourceSuite) TestExchangeScope_Get() {
eb := NewExchangeBackup()
eb.Includes = []map[string]string{allScopesExceptUnknown}
eb.Includes = []scope{allScopesExceptUnknown}
// todo: swap the above for this
// eb := NewExchangeBackup().IncludeUsers(AnyTgt)
@ -794,12 +794,12 @@ func (suite *ExchangeSourceSuite) TestExchangeScopesByCategory() {
event int
mail int
}
type input []map[string]string
makeInput := func(es ...[]ExchangeScope) []map[string]string {
mss := []map[string]string{}
type input []scope
makeInput := func(es ...[]ExchangeScope) []scope {
mss := []scope{}
for _, sl := range es {
for _, s := range sl {
mss = append(mss, map[string]string(s))
mss = append(mss, scope(s))
}
}
return mss

View File

@ -67,20 +67,20 @@ type Selector struct {
Service service `json:"service,omitempty"`
// A slice of exclusion scopes. Exclusions apply globally to all
// 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.
Filters []map[string]string `json:"filters,omitempty"`
Filters []scope `json:"filters,omitempty"`
// A slice of inclusion scopes. Comparators must match either one of these,
// or all filters, to be included.
Includes []map[string]string `json:"includes,omitempty"`
Includes []scope `json:"includes,omitempty"`
}
// helper for specific selector instance constructors.
func newSelector(s service) Selector {
return Selector{
Service: s,
Excludes: []map[string]string{},
Includes: []map[string]string{},
Excludes: []scope{},
Includes: []scope{},
}
}
@ -115,14 +115,14 @@ func appendExcludes[T baseScope](
scopes ...[]T,
) {
if s.Excludes == nil {
s.Excludes = []map[string]string{}
s.Excludes = []scope{}
}
concat := []T{}
for _, scopeSl := range scopes {
concat = append(concat, tform(scopeSl)...)
}
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,
) {
if s.Filters == nil {
s.Filters = []map[string]string{}
s.Filters = []scope{}
}
concat := []T{}
for _, scopeSl := range scopes {
concat = append(concat, tform(scopeSl)...)
}
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,
) {
if s.Includes == nil {
s.Includes = []map[string]string{}
s.Includes = []scope{}
}
concat := []T{}
for _, scopeSl := range scopes {
concat = append(concat, tform(scopeSl)...)
}
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
// target string.
func contains(scope map[string]string, key, target string) bool {
compare := scope[key]
func contains(sc scope, key, target string) bool {
compare := sc[key]
if len(compare) == 0 {
return false
}
@ -234,7 +234,7 @@ func resourcesShortFormat(m map[string][]string) string {
// Transforms the slice to a single map.
// Keys are each map's scopeKeyResource value.
// 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 {
return nil
}
@ -268,7 +268,7 @@ func addToSet(set []string, v string) []string {
// Destination
// ---------------------------------------------------------------------------
type Destination map[string]string
type Destination scope
var ErrorDestinationAlreadySet = errors.New("destination is already declared")

View File

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