From: Dan Fuhry Date: Sun, 30 Mar 2025 02:05:50 +0000 (-0400) Subject: stringmatch: add "any" and "never" matchers X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=714a12909fd23b9b95d501ba8cf52a3e8c066e6c;p=runtime.git stringmatch: add "any" and "never" matchers --- diff --git a/utils/stringmatch/matchers.go b/utils/stringmatch/matchers.go index 20ebe25..a46892c 100644 --- a/utils/stringmatch/matchers.go +++ b/utils/stringmatch/matchers.go @@ -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 } diff --git a/utils/stringmatch/serialization.go b/utils/stringmatch/serialization.go index 83935a1..170b815 100644 --- a/utils/stringmatch/serialization.go +++ b/utils/stringmatch/serialization.go @@ -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")