]> go.fuhry.dev Git - runtime.git/commitdiff
stringmatch: add "any" and "never" matchers
authorDan Fuhry <dan@fuhry.com>
Sun, 30 Mar 2025 02:05:50 +0000 (22:05 -0400)
committerDan Fuhry <dan@fuhry.com>
Sun, 30 Mar 2025 02:05:50 +0000 (22:05 -0400)
utils/stringmatch/matchers.go
utils/stringmatch/serialization.go

index 20ebe25688fb587f9bf8089df5ca04b5ec1aa023..a46892c888e1545f607ea7db2bed9b73da646e87 100644 (file)
@@ -18,6 +18,8 @@ type Suffix string
 type Exact string
 type Contains string
 type Regexp string
+type Any struct{}
+type Never struct{}
 
 func (s Prefix) Match(input string) bool {
        return strings.HasPrefix(input, string(s))
@@ -60,6 +62,22 @@ func (s Regexp) String() string {
        return fmt.Sprintf("%T(%s)", s, string(s))
 }
 
+func (s Any) String() string {
+       return fmt.Sprintf("%T", s)
+}
+
+func (s Any) Match(string) bool {
+       return true
+}
+
+func (s Never) Match(string) bool {
+       return false
+}
+
+func (s Never) String() string {
+       return fmt.Sprintf("%T", s)
+}
+
 type andMatcher struct {
        matchers []StringMatcher
 }
index 83935a1e2f5281ec4732be6dbbc67e3d7658a86d..170b8159bf795f0bfe0482cd8b250d0ac49b6b7d 100644 (file)
@@ -54,6 +54,16 @@ func (m *MatchRule) Matcher() (StringMatcher, error) {
                        return nil, errors.New("regexp matcher may not have sub-rules")
                }
                return Regexp(m.Value), nil
+       case "any":
+               if m.Value != "" || 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 {
+                       return nil, errors.New("never matcher does not accept a value or sub-rules")
+               }
+               return Never{}, nil
        case "and":
                if m.Value != "" {
                        return nil, errors.New("\"and\" matcher may not have a unary value")