diff --git a/src/internal/common/slices.go b/src/internal/common/slices.go new file mode 100644 index 000000000..2d8522981 --- /dev/null +++ b/src/internal/common/slices.go @@ -0,0 +1,10 @@ +package common + +func ContainsString(super []string, sub string) bool { + for _, s := range super { + if s == sub { + return true + } + } + return false +} diff --git a/src/internal/common/slices_test.go b/src/internal/common/slices_test.go new file mode 100644 index 000000000..63e4fc986 --- /dev/null +++ b/src/internal/common/slices_test.go @@ -0,0 +1,27 @@ +package common_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/internal/common" +) + +type CommonSlicesSuite struct { + suite.Suite +} + +func TestCommonSlicesSuite(t *testing.T) { + suite.Run(t, new(CommonSlicesSuite)) +} + +func (suite *CommonSlicesSuite) TestContainsString() { + t := suite.T() + target := "fnords" + good := []string{"fnords"} + bad := []string{"foo", "bar"} + assert.True(t, common.ContainsString(good, target)) + assert.False(t, common.ContainsString(bad, target)) +} diff --git a/src/pkg/selectors/exchange.go b/src/pkg/selectors/exchange.go index 6c9ac93c0..ac2e616c7 100644 --- a/src/pkg/selectors/exchange.go +++ b/src/pkg/selectors/exchange.go @@ -493,6 +493,24 @@ func (s ExchangeScope) IncludesCategory(cat exchangeCategory) bool { return false } +// Contains returns true if the category is included in the scope's +// data type, and the target string is included in the scope. +func (s ExchangeScope) Contains(cat exchangeCategory, target string) bool { + if !s.IncludesCategory(cat) { + return false + } + return contains(s, cat.String(), target) +} + +// returns true if the category is included in the scope's data type, +// and the value is set to Any(). +func (s ExchangeScope) IsAny(cat exchangeCategory) bool { + if !s.IncludesCategory(cat) { + return false + } + return s[cat.String()] == AnyTgt +} + // Get returns the data category in the scope. If the scope // contains all data types for a user, it'll return the // ExchangeUser category. @@ -587,7 +605,7 @@ func (s ExchangeScope) matchesPath(cat exchangeCategory, path []string) bool { // all parts of the scope must match isAny := target[0] == AnyTgt if !isAny { - if !contains(target, id) { + if !common.ContainsString(target, id) { return false } } @@ -595,16 +613,6 @@ func (s ExchangeScope) matchesPath(cat exchangeCategory, path []string) bool { return true } -// temporary helper until filters replace string values for scopes. -func contains(super []string, sub string) bool { - for _, s := range super { - if s == sub { - return true - } - } - return false -} - // --------------------------------------------------------------------------- // Restore Point Filtering // --------------------------------------------------------------------------- diff --git a/src/pkg/selectors/exchange_test.go b/src/pkg/selectors/exchange_test.go index a1f235b4a..6d06539b7 100644 --- a/src/pkg/selectors/exchange_test.go +++ b/src/pkg/selectors/exchange_test.go @@ -871,3 +871,74 @@ func (suite *ExchangeSourceSuite) TestMatchExchangeEntry() { }) } } + +func (suite *ExchangeSourceSuite) TestContains() { + target := "fnords" + var ( + es = NewExchangeRestore() + anyUser = extendExchangeScopeValues(es.Users(Any())) + noMail = extendExchangeScopeValues(es.Mails(None(), None(), None())) + does = extendExchangeScopeValues(es.Mails(Any(), Any(), []string{target})) + doesNot = extendExchangeScopeValues(es.Mails(Any(), Any(), []string{"smarf"})) + wrongType = extendExchangeScopeValues(es.Contacts(Any(), Any(), Any())) + wrongTypeGoodTarget = extendExchangeScopeValues(es.Contacts(Any(), Any(), Any())) + ) + table := []struct { + name string + scopes []ExchangeScope + expect assert.BoolAssertionFunc + }{ + {"any user", anyUser, assert.True}, + {"no mail", noMail, assert.False}, + {"does contain", does, assert.True}, + {"does not contain", doesNot, assert.False}, + {"wrong type", wrongType, assert.False}, + {"wrong type but right target", wrongTypeGoodTarget, assert.False}, + } + for _, test := range table { + suite.T().Run(test.name, func(t *testing.T) { + var result bool + for _, scope := range test.scopes { + if scope.Contains(ExchangeMail, target) { + result = true + break + } + } + test.expect(t, result) + }) + } +} + +func (suite *ExchangeSourceSuite) TestIsAny() { + var ( + es = NewExchangeRestore() + anyUser = extendExchangeScopeValues(es.Users(Any())) + noUser = extendExchangeScopeValues(es.Users(None())) + specificMail = extendExchangeScopeValues(es.Mails(Any(), Any(), []string{"mail"})) + anyMail = extendExchangeScopeValues(es.Mails(Any(), Any(), Any())) + ) + table := []struct { + name string + scopes []ExchangeScope + cat exchangeCategory + expect assert.BoolAssertionFunc + }{ + {"any user", anyUser, ExchangeUser, assert.True}, + {"no user", noUser, ExchangeUser, assert.False}, + {"specific mail", specificMail, ExchangeMail, assert.False}, + {"any mail", anyMail, ExchangeMail, assert.True}, + {"wrong category", anyMail, ExchangeEvent, assert.False}, + } + for _, test := range table { + suite.T().Run(test.name, func(t *testing.T) { + var result bool + for _, scope := range test.scopes { + if scope.IsAny(test.cat) { + result = true + break + } + } + test.expect(t, result) + }) + } +} diff --git a/src/pkg/selectors/selectors.go b/src/pkg/selectors/selectors.go index 45206c610..57ac17878 100644 --- a/src/pkg/selectors/selectors.go +++ b/src/pkg/selectors/selectors.go @@ -154,6 +154,22 @@ func appendIncludes[T baseScope]( } } +// 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] + if len(compare) == 0 { + return false + } + if compare == NoneTgt { + return false + } + if compare == AnyTgt { + return true + } + return strings.Contains(compare, target) +} + // --------------------------------------------------------------------------- // Printing Selectors for Human Reading // --------------------------------------------------------------------------- diff --git a/src/pkg/selectors/selectors_test.go b/src/pkg/selectors/selectors_test.go index 284c9782b..74f6a89d7 100644 --- a/src/pkg/selectors/selectors_test.go +++ b/src/pkg/selectors/selectors_test.go @@ -151,3 +151,13 @@ func (suite *SelectorSuite) TestToResourceTypeMap() { }) } } + +func (suite *SelectorSuite) TestContains() { + t := suite.T() + key := "key" + target := "fnords" + does := map[string]string{key: target} + doesNot := map[string]string{key: "smarf"} + assert.True(t, contains(does, key, target)) + assert.False(t, contains(doesNot, key, target)) +}