clean up/simplify filter packagee (#695)
This commit is contained in:
parent
068f0560e4
commit
7a8c6eaf78
@ -9,35 +9,23 @@ type comparator int
|
|||||||
const (
|
const (
|
||||||
UnknownComparator comparator = iota
|
UnknownComparator comparator = iota
|
||||||
// a == b
|
// a == b
|
||||||
Equal
|
EqualTo
|
||||||
// a > b
|
// a > b
|
||||||
Greater
|
GreaterThan
|
||||||
// a < b
|
// a < b
|
||||||
Less
|
LessThan
|
||||||
// a < b < c
|
|
||||||
Between
|
|
||||||
// "foo,bar,baz" contains "foo"
|
// "foo,bar,baz" contains "foo"
|
||||||
Contains
|
TargetContains
|
||||||
// "foo" is found in "foo,bar,baz"
|
// "foo" is found in "foo,bar,baz"
|
||||||
In
|
TargetIn
|
||||||
// always passes
|
// always passes
|
||||||
Pass
|
Passes
|
||||||
// always fails
|
// always fails
|
||||||
Fail
|
Fails
|
||||||
// passthrough for the target
|
// passthrough for the target
|
||||||
Identity
|
IdentityValue
|
||||||
)
|
)
|
||||||
|
|
||||||
const delimiter = ","
|
|
||||||
|
|
||||||
func join(s ...string) string {
|
|
||||||
return strings.Join(s, delimiter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func split(s string) []string {
|
|
||||||
return strings.Split(s, delimiter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func norm(s string) string {
|
func norm(s string) string {
|
||||||
return strings.ToLower(s)
|
return strings.ToLower(s)
|
||||||
}
|
}
|
||||||
@ -47,91 +35,117 @@ func norm(s string) string {
|
|||||||
// true if Filter.Comparer(filter.target, v) is true.
|
// true if Filter.Comparer(filter.target, v) is true.
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
Comparator comparator `json:"comparator"`
|
Comparator comparator `json:"comparator"`
|
||||||
Category any `json:"category"` // a caller-provided identifier. Probably an iota or string const.
|
Target string `json:"target"` // the value to compare against
|
||||||
Target string `json:"target"` // the value to compare against
|
Negate bool `json:"negate"` // when true, negate the comparator result
|
||||||
Negate bool `json:"negate"` // when true, negate the comparator result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------------------
|
||||||
// Constructors
|
// Constructors
|
||||||
// ----------------------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// NewEquals creates a filter which Matches(v) is true if
|
// Equal creates a filter where Compare(v) is true if
|
||||||
// target == v
|
// target == v
|
||||||
func NewEquals(negate bool, category any, target string) Filter {
|
func Equal(target string) Filter {
|
||||||
return Filter{Equal, category, target, negate}
|
return newFilter(EqualTo, target, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGreater creates a filter which Matches(v) is true if
|
// NotEqual creates a filter where Compare(v) is true if
|
||||||
|
// target != v
|
||||||
|
func NotEqual(target string) Filter {
|
||||||
|
return newFilter(EqualTo, target, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Greater creates a filter where Compare(v) is true if
|
||||||
// target > v
|
// target > v
|
||||||
func NewGreater(negate bool, category any, target string) Filter {
|
func Greater(target string) Filter {
|
||||||
return Filter{Greater, category, target, negate}
|
return newFilter(GreaterThan, target, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLess creates a filter which Matches(v) is true if
|
// NotGreater creates a filter where Compare(v) is true if
|
||||||
|
// target <= v
|
||||||
|
func NotGreater(target string) Filter {
|
||||||
|
return newFilter(GreaterThan, target, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Less creates a filter where Compare(v) is true if
|
||||||
// target < v
|
// target < v
|
||||||
func NewLess(negate bool, category any, target string) Filter {
|
func Less(target string) Filter {
|
||||||
return Filter{Less, category, target, negate}
|
return newFilter(LessThan, target, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBetween creates a filter which Matches(v) is true if
|
// NotLess creates a filter where Compare(v) is true if
|
||||||
// lesser < v && v < greater
|
// target >= v
|
||||||
func NewBetween(negate bool, category any, lesser, greater string) Filter {
|
func NotLess(target string) Filter {
|
||||||
return Filter{Between, category, join(lesser, greater), negate}
|
return newFilter(LessThan, target, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContains creates a filter which Matches(v) is true if
|
// Contains creates a filter where Compare(v) is true if
|
||||||
// super.Contains(v)
|
// target.Contains(v)
|
||||||
func NewContains(negate bool, category any, super string) Filter {
|
func Contains(target string) Filter {
|
||||||
return Filter{Contains, category, super, negate}
|
return newFilter(TargetContains, target, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewIn creates a filter which Matches(v) is true if
|
// NotContains creates a filter where Compare(v) is true if
|
||||||
// v.Contains(substr)
|
// !target.Contains(v)
|
||||||
func NewIn(negate bool, category any, substr string) Filter {
|
func NotContains(target string) Filter {
|
||||||
return Filter{In, category, substr, negate}
|
return newFilter(TargetContains, target, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPass creates a filter where Matches(v) always returns true
|
// In creates a filter where Compare(v) is true if
|
||||||
func NewPass() Filter {
|
// v.Contains(target)
|
||||||
return Filter{Pass, nil, "*", false}
|
func In(target string) Filter {
|
||||||
|
return newFilter(TargetIn, target, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFail creates a filter where Matches(v) always returns false
|
// NotIn creates a filter where Compare(v) is true if
|
||||||
func NewFail() Filter {
|
// !v.Contains(target)
|
||||||
return Filter{Fail, nil, "", false}
|
func NotIn(target string) Filter {
|
||||||
|
return newFilter(TargetIn, target, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewIdentity creates a filter intended to hold values, rather than
|
// Pass creates a filter where Compare(v) always returns true
|
||||||
// compare them. Functionally, it'll behave the same as Equals.
|
func Pass() Filter {
|
||||||
func NewIdentity(id string) Filter {
|
return newFilter(Passes, "*", false)
|
||||||
return Filter{Identity, nil, id, false}
|
}
|
||||||
|
|
||||||
|
// Fail creates a filter where Compare(v) always returns false
|
||||||
|
func Fail() Filter {
|
||||||
|
return newFilter(Fails, "", false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Identity creates a filter intended to hold values, rather than
|
||||||
|
// compare them. Comparatively, it'll behave the same as Equals.
|
||||||
|
func Identity(id string) Filter {
|
||||||
|
return newFilter(IdentityValue, id, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newFilter is the standard filter constructor.
|
||||||
|
func newFilter(c comparator, target string, negate bool) Filter {
|
||||||
|
return Filter{c, target, negate}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------------------
|
||||||
// Comparisons
|
// Comparisons
|
||||||
// ----------------------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Checks whether the filter matches the input
|
// Compare checks whether the input passes the filter.
|
||||||
func (f Filter) Matches(input string) bool {
|
func (f Filter) Compare(input string) bool {
|
||||||
var cmp func(string, string) bool
|
var cmp func(string, string) bool
|
||||||
|
|
||||||
switch f.Comparator {
|
switch f.Comparator {
|
||||||
case Equal, Identity:
|
case EqualTo, IdentityValue:
|
||||||
cmp = equals
|
cmp = equals
|
||||||
case Greater:
|
case GreaterThan:
|
||||||
cmp = greater
|
cmp = greater
|
||||||
case Less:
|
case LessThan:
|
||||||
cmp = less
|
cmp = less
|
||||||
case Between:
|
case TargetContains:
|
||||||
cmp = between
|
|
||||||
case Contains:
|
|
||||||
cmp = contains
|
cmp = contains
|
||||||
case In:
|
case TargetIn:
|
||||||
cmp = in
|
cmp = in
|
||||||
case Pass:
|
case Passes:
|
||||||
return true
|
return true
|
||||||
case Fail:
|
case Fails:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,15 +172,6 @@ func less(target, input string) bool {
|
|||||||
return target < input
|
return target < input
|
||||||
}
|
}
|
||||||
|
|
||||||
// assumes target is a delimited string.
|
|
||||||
// true if both:
|
|
||||||
// - less(target[0], input)
|
|
||||||
// - greater(target[1], input)
|
|
||||||
func between(target, input string) bool {
|
|
||||||
parts := split(target)
|
|
||||||
return less(parts[0], input) && greater(parts[1], input)
|
|
||||||
}
|
|
||||||
|
|
||||||
// true if target contains input as a substring.
|
// true if target contains input as a substring.
|
||||||
func contains(target, input string) bool {
|
func contains(target, input string) bool {
|
||||||
return strings.Contains(target, input)
|
return strings.Contains(target, input)
|
||||||
@ -181,34 +186,22 @@ func in(target, input string) bool {
|
|||||||
// Helpers
|
// Helpers
|
||||||
// ----------------------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Targets returns the Target value split into a slice.
|
// prefixString maps the comparators to string prefixes for printing.
|
||||||
func (f Filter) Targets() []string {
|
var prefixString = map[comparator]string{
|
||||||
return split(f.Target)
|
EqualTo: "eq:",
|
||||||
|
GreaterThan: "gt:",
|
||||||
|
LessThan: "lt:",
|
||||||
|
TargetContains: "cont:",
|
||||||
|
TargetIn: "in:",
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Filter) String() string {
|
func (f Filter) String() string {
|
||||||
var prefix string
|
|
||||||
|
|
||||||
switch f.Comparator {
|
switch f.Comparator {
|
||||||
case Equal:
|
case Passes:
|
||||||
prefix = "eq:"
|
|
||||||
case Greater:
|
|
||||||
prefix = "gt:"
|
|
||||||
case Less:
|
|
||||||
prefix = "lt:"
|
|
||||||
case Between:
|
|
||||||
prefix = "btwn:"
|
|
||||||
case Contains:
|
|
||||||
prefix = "cont:"
|
|
||||||
case In:
|
|
||||||
prefix = "in:"
|
|
||||||
case Pass:
|
|
||||||
return "pass"
|
return "pass"
|
||||||
case Fail:
|
case Fails:
|
||||||
return "fail"
|
return "fail"
|
||||||
case Identity:
|
|
||||||
default: // no prefix
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return prefix + f.Target
|
return prefixString[f.Comparator] + f.Target
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,9 +18,8 @@ func TestFiltersSuite(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (suite *FiltersSuite) TestEquals() {
|
func (suite *FiltersSuite) TestEquals() {
|
||||||
makeFilt := filters.NewEquals
|
f := filters.Equal("foo")
|
||||||
f := makeFilt(false, "", "foo")
|
nf := filters.NotEqual("foo")
|
||||||
nf := makeFilt(true, "", "foo")
|
|
||||||
|
|
||||||
table := []struct {
|
table := []struct {
|
||||||
input string
|
input string
|
||||||
@ -32,16 +31,15 @@ func (suite *FiltersSuite) TestEquals() {
|
|||||||
}
|
}
|
||||||
for _, test := range table {
|
for _, test := range table {
|
||||||
suite.T().Run(test.input, func(t *testing.T) {
|
suite.T().Run(test.input, func(t *testing.T) {
|
||||||
test.expectF(t, f.Matches(test.input), "filter")
|
test.expectF(t, f.Compare(test.input), "filter")
|
||||||
test.expectNF(t, nf.Matches(test.input), "negated filter")
|
test.expectNF(t, nf.Compare(test.input), "negated filter")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *FiltersSuite) TestGreater() {
|
func (suite *FiltersSuite) TestGreater() {
|
||||||
makeFilt := filters.NewGreater
|
f := filters.Greater("5")
|
||||||
f := makeFilt(false, "", "5")
|
nf := filters.NotGreater("5")
|
||||||
nf := makeFilt(true, "", "5")
|
|
||||||
|
|
||||||
table := []struct {
|
table := []struct {
|
||||||
input string
|
input string
|
||||||
@ -54,16 +52,15 @@ func (suite *FiltersSuite) TestGreater() {
|
|||||||
}
|
}
|
||||||
for _, test := range table {
|
for _, test := range table {
|
||||||
suite.T().Run(test.input, func(t *testing.T) {
|
suite.T().Run(test.input, func(t *testing.T) {
|
||||||
test.expectF(t, f.Matches(test.input), "filter")
|
test.expectF(t, f.Compare(test.input), "filter")
|
||||||
test.expectNF(t, nf.Matches(test.input), "negated filter")
|
test.expectNF(t, nf.Compare(test.input), "negated filter")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *FiltersSuite) TestLess() {
|
func (suite *FiltersSuite) TestLess() {
|
||||||
makeFilt := filters.NewLess
|
f := filters.Less("5")
|
||||||
f := makeFilt(false, "", "5")
|
nf := filters.NotLess("5")
|
||||||
nf := makeFilt(true, "", "5")
|
|
||||||
|
|
||||||
table := []struct {
|
table := []struct {
|
||||||
input string
|
input string
|
||||||
@ -76,39 +73,15 @@ func (suite *FiltersSuite) TestLess() {
|
|||||||
}
|
}
|
||||||
for _, test := range table {
|
for _, test := range table {
|
||||||
suite.T().Run(test.input, func(t *testing.T) {
|
suite.T().Run(test.input, func(t *testing.T) {
|
||||||
test.expectF(t, f.Matches(test.input), "filter")
|
test.expectF(t, f.Compare(test.input), "filter")
|
||||||
test.expectNF(t, nf.Matches(test.input), "negated filter")
|
test.expectNF(t, nf.Compare(test.input), "negated filter")
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *FiltersSuite) TestBetween() {
|
|
||||||
makeFilt := filters.NewBetween
|
|
||||||
f := makeFilt(false, "", "abc", "def")
|
|
||||||
nf := makeFilt(true, "", "abc", "def")
|
|
||||||
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
expectF assert.BoolAssertionFunc
|
|
||||||
expectNF assert.BoolAssertionFunc
|
|
||||||
}{
|
|
||||||
{"cd", assert.True, assert.False},
|
|
||||||
{"a", assert.False, assert.True},
|
|
||||||
{"1", assert.False, assert.True},
|
|
||||||
{"f", assert.False, assert.True},
|
|
||||||
}
|
|
||||||
for _, test := range table {
|
|
||||||
suite.T().Run(test.input, func(t *testing.T) {
|
|
||||||
test.expectF(t, f.Matches(test.input), "filter")
|
|
||||||
test.expectNF(t, nf.Matches(test.input), "negated filter")
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *FiltersSuite) TestContains() {
|
func (suite *FiltersSuite) TestContains() {
|
||||||
makeFilt := filters.NewContains
|
f := filters.Contains("smurfs")
|
||||||
f := makeFilt(false, "", "smurfs")
|
nf := filters.NotContains("smurfs")
|
||||||
nf := makeFilt(true, "", "smurfs")
|
|
||||||
|
|
||||||
table := []struct {
|
table := []struct {
|
||||||
input string
|
input string
|
||||||
@ -120,16 +93,15 @@ func (suite *FiltersSuite) TestContains() {
|
|||||||
}
|
}
|
||||||
for _, test := range table {
|
for _, test := range table {
|
||||||
suite.T().Run(test.input, func(t *testing.T) {
|
suite.T().Run(test.input, func(t *testing.T) {
|
||||||
test.expectF(t, f.Matches(test.input), "filter")
|
test.expectF(t, f.Compare(test.input), "filter")
|
||||||
test.expectNF(t, nf.Matches(test.input), "negated filter")
|
test.expectNF(t, nf.Compare(test.input), "negated filter")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *FiltersSuite) TestContains_Joined() {
|
func (suite *FiltersSuite) TestContains_Joined() {
|
||||||
makeFilt := filters.NewContains
|
f := filters.Contains("smarf,userid")
|
||||||
f := makeFilt(false, "", "smarf,userid")
|
nf := filters.NotContains("smarf,userid")
|
||||||
nf := makeFilt(true, "", "smarf,userid")
|
|
||||||
|
|
||||||
table := []struct {
|
table := []struct {
|
||||||
input string
|
input string
|
||||||
@ -142,16 +114,15 @@ func (suite *FiltersSuite) TestContains_Joined() {
|
|||||||
}
|
}
|
||||||
for _, test := range table {
|
for _, test := range table {
|
||||||
suite.T().Run(test.input, func(t *testing.T) {
|
suite.T().Run(test.input, func(t *testing.T) {
|
||||||
test.expectF(t, f.Matches(test.input), "filter")
|
test.expectF(t, f.Compare(test.input), "filter")
|
||||||
test.expectNF(t, nf.Matches(test.input), "negated filter")
|
test.expectNF(t, nf.Compare(test.input), "negated filter")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *FiltersSuite) TestIn() {
|
func (suite *FiltersSuite) TestIn() {
|
||||||
makeFilt := filters.NewIn
|
f := filters.In("murf")
|
||||||
f := makeFilt(false, "", "murf")
|
nf := filters.NotIn("murf")
|
||||||
nf := makeFilt(true, "", "murf")
|
|
||||||
|
|
||||||
table := []struct {
|
table := []struct {
|
||||||
input string
|
input string
|
||||||
@ -163,16 +134,15 @@ func (suite *FiltersSuite) TestIn() {
|
|||||||
}
|
}
|
||||||
for _, test := range table {
|
for _, test := range table {
|
||||||
suite.T().Run(test.input, func(t *testing.T) {
|
suite.T().Run(test.input, func(t *testing.T) {
|
||||||
test.expectF(t, f.Matches(test.input), "filter")
|
test.expectF(t, f.Compare(test.input), "filter")
|
||||||
test.expectNF(t, nf.Matches(test.input), "negated filter")
|
test.expectNF(t, nf.Compare(test.input), "negated filter")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *FiltersSuite) TestIn_Joined() {
|
func (suite *FiltersSuite) TestIn_Joined() {
|
||||||
makeFilt := filters.NewIn
|
f := filters.In("userid")
|
||||||
f := makeFilt(false, "", "userid")
|
nf := filters.NotIn("userid")
|
||||||
nf := makeFilt(true, "", "userid")
|
|
||||||
|
|
||||||
table := []struct {
|
table := []struct {
|
||||||
input string
|
input string
|
||||||
@ -184,8 +154,8 @@ func (suite *FiltersSuite) TestIn_Joined() {
|
|||||||
}
|
}
|
||||||
for _, test := range table {
|
for _, test := range table {
|
||||||
suite.T().Run(test.input, func(t *testing.T) {
|
suite.T().Run(test.input, func(t *testing.T) {
|
||||||
test.expectF(t, f.Matches(test.input), "filter")
|
test.expectF(t, f.Compare(test.input), "filter")
|
||||||
test.expectNF(t, nf.Matches(test.input), "negated filter")
|
test.expectNF(t, nf.Compare(test.input), "negated filter")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -270,7 +270,7 @@ func (sr *ExchangeRestore) MailReceivedAfter(timeStrings string) []ExchangeScope
|
|||||||
ExchangeMail,
|
ExchangeMail,
|
||||||
ExchangeFilterMailReceivedAfter,
|
ExchangeFilterMailReceivedAfter,
|
||||||
[]string{timeStrings},
|
[]string{timeStrings},
|
||||||
wrapFilter(filters.NewLess)),
|
wrapFilter(filters.Less)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,7 +284,7 @@ func (sr *ExchangeRestore) MailReceivedBefore(timeStrings string) []ExchangeScop
|
|||||||
ExchangeMail,
|
ExchangeMail,
|
||||||
ExchangeFilterMailReceivedBefore,
|
ExchangeFilterMailReceivedBefore,
|
||||||
[]string{timeStrings},
|
[]string{timeStrings},
|
||||||
wrapFilter(filters.NewGreater)),
|
wrapFilter(filters.Greater)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,7 +299,7 @@ func (sr *ExchangeRestore) MailSender(senderIDs []string) []ExchangeScope {
|
|||||||
ExchangeMail,
|
ExchangeMail,
|
||||||
ExchangeFilterMailSender,
|
ExchangeFilterMailSender,
|
||||||
senderIDs,
|
senderIDs,
|
||||||
wrapFilter(filters.NewIn)),
|
wrapFilter(filters.In)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,7 +314,7 @@ func (sr *ExchangeRestore) MailSubject(subjectSubstrings []string) []ExchangeSco
|
|||||||
ExchangeMail,
|
ExchangeMail,
|
||||||
ExchangeFilterMailSubject,
|
ExchangeFilterMailSubject,
|
||||||
subjectSubstrings,
|
subjectSubstrings,
|
||||||
wrapFilter(filters.NewIn)),
|
wrapFilter(filters.In)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -337,7 +337,7 @@ func (suite *ExchangeSelectorSuite) TestExchangeSelector_Exclude_Users() {
|
|||||||
map[categorizer]string{ExchangeUser: join(u1, u2)},
|
map[categorizer]string{ExchangeUser: join(u1, u2)},
|
||||||
)
|
)
|
||||||
|
|
||||||
if sc[scopeKeyCategory].Matches(ExchangeContactFolder.String()) {
|
if sc[scopeKeyCategory].Compare(ExchangeContactFolder.String()) {
|
||||||
scopeMustHave(
|
scopeMustHave(
|
||||||
t,
|
t,
|
||||||
ExchangeScope(sc),
|
ExchangeScope(sc),
|
||||||
@ -348,7 +348,7 @@ func (suite *ExchangeSelectorSuite) TestExchangeSelector_Exclude_Users() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sc[scopeKeyCategory].Matches(ExchangeEvent.String()) {
|
if sc[scopeKeyCategory].Compare(ExchangeEvent.String()) {
|
||||||
scopeMustHave(
|
scopeMustHave(
|
||||||
t,
|
t,
|
||||||
ExchangeScope(sc),
|
ExchangeScope(sc),
|
||||||
@ -358,7 +358,7 @@ func (suite *ExchangeSelectorSuite) TestExchangeSelector_Exclude_Users() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sc[scopeKeyCategory].Matches(ExchangeMailFolder.String()) {
|
if sc[scopeKeyCategory].Compare(ExchangeMailFolder.String()) {
|
||||||
scopeMustHave(
|
scopeMustHave(
|
||||||
t,
|
t,
|
||||||
ExchangeScope(sc),
|
ExchangeScope(sc),
|
||||||
@ -391,7 +391,7 @@ func (suite *ExchangeSelectorSuite) TestExchangeSelector_Include_Users() {
|
|||||||
map[categorizer]string{ExchangeUser: join(u1, u2)},
|
map[categorizer]string{ExchangeUser: join(u1, u2)},
|
||||||
)
|
)
|
||||||
|
|
||||||
if sc[scopeKeyCategory].Matches(ExchangeContactFolder.String()) {
|
if sc[scopeKeyCategory].Compare(ExchangeContactFolder.String()) {
|
||||||
scopeMustHave(
|
scopeMustHave(
|
||||||
t,
|
t,
|
||||||
ExchangeScope(sc),
|
ExchangeScope(sc),
|
||||||
@ -402,7 +402,7 @@ func (suite *ExchangeSelectorSuite) TestExchangeSelector_Include_Users() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sc[scopeKeyCategory].Matches(ExchangeEvent.String()) {
|
if sc[scopeKeyCategory].Compare(ExchangeEvent.String()) {
|
||||||
scopeMustHave(
|
scopeMustHave(
|
||||||
t,
|
t,
|
||||||
ExchangeScope(sc),
|
ExchangeScope(sc),
|
||||||
@ -412,7 +412,7 @@ func (suite *ExchangeSelectorSuite) TestExchangeSelector_Include_Users() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sc[scopeKeyCategory].Matches(ExchangeMailFolder.String()) {
|
if sc[scopeKeyCategory].Compare(ExchangeMailFolder.String()) {
|
||||||
scopeMustHave(
|
scopeMustHave(
|
||||||
t,
|
t,
|
||||||
ExchangeScope(sc),
|
ExchangeScope(sc),
|
||||||
@ -566,7 +566,7 @@ func (suite *ExchangeSelectorSuite) TestExchangeScope_Category() {
|
|||||||
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 = []scope{
|
eb.Includes = []scope{
|
||||||
{scopeKeyCategory: filters.NewIdentity(test.is.String())},
|
{scopeKeyCategory: filters.Identity(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())
|
||||||
@ -600,7 +600,7 @@ func (suite *ExchangeSelectorSuite) TestExchangeScope_IncludesCategory() {
|
|||||||
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 = []scope{
|
eb.Includes = []scope{
|
||||||
{scopeKeyCategory: filters.NewIdentity(test.is.String())},
|
{scopeKeyCategory: filters.Identity(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))
|
||||||
|
|||||||
@ -99,11 +99,11 @@ func stubScope(match string) mockScope {
|
|||||||
|
|
||||||
return mockScope{
|
return mockScope{
|
||||||
rootCatStub.String(): passAny,
|
rootCatStub.String(): passAny,
|
||||||
scopeKeyCategory: filters.NewIdentity(rootCatStub.String()),
|
scopeKeyCategory: filters.Identity(rootCatStub.String()),
|
||||||
scopeKeyGranularity: filters.NewIdentity(Item),
|
scopeKeyGranularity: filters.Identity(Item),
|
||||||
scopeKeyResource: filters.NewIdentity(stubResource),
|
scopeKeyResource: filters.Identity(stubResource),
|
||||||
scopeKeyDataType: filters.NewIdentity(rootCatStub.String()),
|
scopeKeyDataType: filters.Identity(rootCatStub.String()),
|
||||||
shouldMatch: filters.NewIdentity(sm),
|
shouldMatch: filters.Identity(sm),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -121,10 +121,10 @@ func makeScope[T scopeT](
|
|||||||
resources, vs []string,
|
resources, vs []string,
|
||||||
) T {
|
) T {
|
||||||
s := T{
|
s := T{
|
||||||
scopeKeyCategory: filters.NewIdentity(cat.String()),
|
scopeKeyCategory: filters.Identity(cat.String()),
|
||||||
scopeKeyDataType: filters.NewIdentity(cat.leafCat().String()),
|
scopeKeyDataType: filters.Identity(cat.leafCat().String()),
|
||||||
scopeKeyGranularity: filters.NewIdentity(granularity),
|
scopeKeyGranularity: filters.Identity(granularity),
|
||||||
scopeKeyResource: filters.NewIdentity(join(resources...)),
|
scopeKeyResource: filters.Identity(join(resources...)),
|
||||||
cat.String(): filterize(vs...),
|
cat.String(): filterize(vs...),
|
||||||
cat.rootCat().String(): filterize(resources...),
|
cat.rootCat().String(): filterize(resources...),
|
||||||
}
|
}
|
||||||
@ -140,11 +140,11 @@ func makeFilterScope[T scopeT](
|
|||||||
f func([]string) filters.Filter,
|
f func([]string) filters.Filter,
|
||||||
) T {
|
) T {
|
||||||
return T{
|
return T{
|
||||||
scopeKeyCategory: filters.NewIdentity(cat.String()),
|
scopeKeyCategory: filters.Identity(cat.String()),
|
||||||
scopeKeyDataType: filters.NewIdentity(cat.leafCat().String()),
|
scopeKeyDataType: filters.Identity(cat.leafCat().String()),
|
||||||
scopeKeyGranularity: filters.NewIdentity(Filter),
|
scopeKeyGranularity: filters.Identity(Filter),
|
||||||
scopeKeyInfoFilter: filters.NewIdentity(filterCat.String()),
|
scopeKeyInfoFilter: filters.Identity(filterCat.String()),
|
||||||
scopeKeyResource: filters.NewIdentity(Filter),
|
scopeKeyResource: filters.Identity(Filter),
|
||||||
filterCat.String(): f(clean(vs)),
|
filterCat.String(): f(clean(vs)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -164,7 +164,7 @@ func matches[T scopeT, C categoryT](s T, cat C, target string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return s[cat.String()].Matches(target)
|
return s[cat.String()].Compare(target)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getCategory returns the scope's category value.
|
// getCategory returns the scope's category value.
|
||||||
@ -409,8 +409,7 @@ func matchesPathValues[T scopeT, C categoryT](
|
|||||||
// all parts of the scope must match
|
// all parts of the scope must match
|
||||||
cc := c.(C)
|
cc := c.(C)
|
||||||
if !isAnyTarget(sc, cc) {
|
if !isAnyTarget(sc, cc) {
|
||||||
f := filters.NewContains(false, cc, join(scopeVals...))
|
if filters.NotContains(join(scopeVals...)).Compare(pathVal) {
|
||||||
if !f.Matches(pathVal) {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -53,7 +53,7 @@ func (suite *SelectorScopesSuite) TestContains() {
|
|||||||
name: "blank value",
|
name: "blank value",
|
||||||
scope: func() mockScope {
|
scope: func() mockScope {
|
||||||
stub := stubScope("")
|
stub := stubScope("")
|
||||||
stub[rootCatStub.String()] = filters.NewEquals(false, nil, "")
|
stub[rootCatStub.String()] = filters.Equal("")
|
||||||
return stub
|
return stub
|
||||||
},
|
},
|
||||||
check: rootCatStub.String(),
|
check: rootCatStub.String(),
|
||||||
@ -427,23 +427,23 @@ func (suite *SelectorScopesSuite) TestWrapFilter() {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "any",
|
name: "any",
|
||||||
filter: filters.NewContains,
|
filter: filters.Contains,
|
||||||
input: Any(),
|
input: Any(),
|
||||||
comparator: int(filters.Pass),
|
comparator: int(filters.Passes),
|
||||||
target: AnyTgt,
|
target: AnyTgt,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "none",
|
name: "none",
|
||||||
filter: filters.NewIn,
|
filter: filters.In,
|
||||||
input: None(),
|
input: None(),
|
||||||
comparator: int(filters.Fail),
|
comparator: int(filters.Fails),
|
||||||
target: NoneTgt,
|
target: NoneTgt,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "something",
|
name: "something",
|
||||||
filter: filters.NewEquals,
|
filter: filters.Equal,
|
||||||
input: []string{"userid"},
|
input: []string{"userid"},
|
||||||
comparator: int(filters.Equal),
|
comparator: int(filters.EqualTo),
|
||||||
target: "userid",
|
target: "userid",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,8 +56,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
passAny = filters.NewPass()
|
passAny = filters.Pass()
|
||||||
failAny = filters.NewFail()
|
failAny = filters.Fail()
|
||||||
)
|
)
|
||||||
|
|
||||||
// All is the resource name that gets output when the resource is AnyTgt.
|
// All is the resource name that gets output when the resource is AnyTgt.
|
||||||
@ -258,7 +258,7 @@ func toResourceTypeMap(ms []scope) map[string][]string {
|
|||||||
k = All
|
k = All
|
||||||
}
|
}
|
||||||
|
|
||||||
r[k] = addToSet(r[k], m[scopeKeyDataType].Targets())
|
r[k] = addToSet(r[k], split(m[scopeKeyDataType].Target))
|
||||||
}
|
}
|
||||||
|
|
||||||
return r
|
return r
|
||||||
@ -357,13 +357,13 @@ func filterize(s ...string) filters.Filter {
|
|||||||
return failAny
|
return failAny
|
||||||
}
|
}
|
||||||
|
|
||||||
return filters.NewEquals(false, "", s[0])
|
return filters.Equal(s[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
return filters.NewContains(false, "", join(s...))
|
return filters.Contains(join(s...))
|
||||||
}
|
}
|
||||||
|
|
||||||
type filterFunc func(bool, any, string) filters.Filter
|
type filterFunc func(string) filters.Filter
|
||||||
|
|
||||||
// wrapFilter produces a func that filterizes the input by:
|
// wrapFilter produces a func that filterizes the input by:
|
||||||
// - cleans the input string
|
// - cleans the input string
|
||||||
@ -386,6 +386,6 @@ func wrapFilter(ff filterFunc) func([]string) filters.Filter {
|
|||||||
|
|
||||||
ss := join(s...)
|
ss := join(s...)
|
||||||
|
|
||||||
return ff(false, nil, ss)
|
return ff(ss)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user