github.com/netdata/go.d.plugin@v0.58.1/pkg/matcher/expr.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package matcher 4 5 import ( 6 "errors" 7 "fmt" 8 ) 9 10 type ( 11 Expr interface { 12 Parse() (Matcher, error) 13 } 14 15 // SimpleExpr is a simple expression to describe the condition: 16 // (includes[0].Match(v) || includes[1].Match(v) || ...) && !(excludes[0].Match(v) || excludes[1].Match(v) || ...) 17 SimpleExpr struct { 18 Includes []string `yaml:"includes" json:"includes"` 19 Excludes []string `yaml:"excludes" json:"excludes"` 20 } 21 ) 22 23 var ( 24 ErrEmptyExpr = errors.New("empty expression") 25 ) 26 27 // Empty returns true if both Includes and Excludes are empty. You can't 28 func (s *SimpleExpr) Empty() bool { 29 return len(s.Includes) == 0 && len(s.Excludes) == 0 30 } 31 32 // Parse parses the given matchers in Includes and Excludes 33 func (s *SimpleExpr) Parse() (Matcher, error) { 34 if len(s.Includes) == 0 && len(s.Excludes) == 0 { 35 return nil, ErrEmptyExpr 36 } 37 var ( 38 includes = FALSE() 39 excludes = FALSE() 40 ) 41 if len(s.Includes) > 0 { 42 for _, item := range s.Includes { 43 m, err := Parse(item) 44 if err != nil { 45 return nil, fmt.Errorf("parse matcher %q error: %v", item, err) 46 } 47 includes = Or(includes, m) 48 } 49 } else { 50 includes = TRUE() 51 } 52 53 for _, item := range s.Excludes { 54 m, err := Parse(item) 55 if err != nil { 56 return nil, fmt.Errorf("parse matcher %q error: %v", item, err) 57 } 58 excludes = Or(excludes, m) 59 } 60 61 return And(includes, Not(excludes)), nil 62 }