Sub(vars map[string]string) StringMatcher
}
-type Prefix string
-type Suffix string
-type Exact string
-type Contains string
-type Regexp string
-type Any struct{}
-type Never struct{}
+type (
+ Prefix string
+ Suffix string
+ Exact string
+ Contains string
+ Regexp string
+ Any struct{}
+ Never struct{}
+)
type MatchableString interface {
~string
return s
}
+func Not(matcher StringMatcher) StringMatcher {
+ return ¬Matcher{matcher: matcher}
+}
+
+type notMatcher struct {
+ matcher StringMatcher
+}
+
+func (s *notMatcher) Match(input string) bool {
+ return !s.matcher.Match(input)
+}
+
+func (s *notMatcher) String() string {
+ return fmt.Sprintf("%T(%s)", s, s.matcher.String())
+}
+
+func (s *notMatcher) Sub(vars map[string]string) StringMatcher {
+ return Not(s.matcher.Sub(vars))
+}
+
type andMatcher struct {
matchers []StringMatcher
}
}
}
+func TestNot(t *testing.T) {
+ cases := []*testCase{
+ {Not(Prefix("/foo")), "/foo/bar", false},
+ {Not(Prefix("/foo")), "/bar", true},
+ {Not(Suffix("/foo")), "/foo/bar", true},
+ {Not(Suffix("/bar")), "/bar", false},
+ {Not(Contains("o")), "/foo", false},
+ {Not(Contains("a")), "/foo", true},
+ }
+
+ for _, tc := range cases {
+ tc.Run(t)
+ }
+}
+
func TestAnd(t *testing.T) {
cases := []*testCase{
{And(Prefix("/foo"), Suffix("/bar")), "/foo/bar", true},
type MatchRule struct {
Mode string `yaml:"mode" json:"mode"`
Value string `yaml:"value" json:"value"`
+ Rule *MatchRule `yaml:"rule" json:"rule"`
Rules []*MatchRule `yaml:"rules" json:"rules"`
}
if m.Value == "" {
return nil, errors.New("prefix matcher is missing value")
}
+ if m.Rule != nil {
+ return nil, errors.New("prefix matcher may not have child rule")
+ }
if len(m.Rules) != 0 {
return nil, errors.New("prefix matcher may not have sub-rules")
}
if m.Value == "" {
return nil, errors.New("suffix matcher is missing value")
}
+ if m.Rule != nil {
+ return nil, errors.New("suffix matcher may not have child rule")
+ }
if len(m.Rules) != 0 {
return nil, errors.New("suffix matcher may not have sub-rules")
}
if m.Value == "" {
return nil, errors.New("exact matcher is missing value")
}
+ if m.Rule != nil {
+ return nil, errors.New("exact matcher may not have child rule")
+ }
if len(m.Rules) != 0 {
return nil, errors.New("exact matcher may not have sub-rules")
}
if m.Value == "" {
return nil, errors.New("contains matcher is missing value")
}
+ if m.Rule != nil {
+ return nil, errors.New("contains matcher may not have child rule")
+ }
if len(m.Rules) != 0 {
return nil, errors.New("contains matcher may not have sub-rules")
}
if m.Value == "" {
return nil, errors.New("regexp matcher is missing value")
}
+ if m.Rule != nil {
+ return nil, errors.New("regexp matcher may not have child rule")
+ }
if len(m.Rules) != 0 {
return nil, errors.New("regexp matcher may not have sub-rules")
}
return Regexp(m.Value), nil
+ case "not", "invert", "inverse":
+ if m.Value != "" {
+ return nil, errors.New("not matcher may not have a unary value")
+ }
+ if len(m.Rules) != 0 {
+ return nil, errors.New("not matcher may not have sub-rules")
+ }
+ if m.Rule == nil {
+ return nil, errors.New("not matcher must have a singular child rule in `rule'")
+ }
+ matcher, err := m.Rule.Matcher()
+ if err != nil {
+ return nil, fmt.Errorf("while processing 'not' rule: %v", err)
+ }
+ return Not(matcher), nil
case "any":
- if m.Value != "" || len(m.Rules) != 0 {
+ if m.Value != "" || m.Rule != nil || len(m.Rules) != 0 {
return nil, errors.New("any matcher does not accept a value or sub-rules")
}
return Any{}, nil
case "never":
- if m.Value != "" || len(m.Rules) != 0 {
+ if m.Value != "" || m.Rule != nil || len(m.Rules) != 0 {
return nil, errors.New("never matcher does not accept a value or sub-rules")
}
return Never{}, nil
if m.Value != "" {
return nil, errors.New("\"and\" matcher may not have a unary value")
}
+ if m.Rule != nil {
+ return nil, errors.New("\"and\" matcher may not have a singular child rule")
+ }
if len(m.Rules) < 1 {
return nil, errors.New("no rules present in \"and\" matcher")
}
if m.Value != "" {
return nil, errors.New("\"or\" matcher may not have a unary value")
}
+ if m.Rule != nil {
+ return nil, errors.New("\"or\" matcher may not have a singular child rule")
+ }
if len(m.Rules) < 1 {
return nil, errors.New("no rules present in \"or\" matcher")
}