Skip to content

Commit

Permalink
fix evaluating nested filter conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Jul 29, 2024
1 parent 412f711 commit c779e6a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
This file documents the revision history for the SNClient+ agent.

next:
- fix evaluating nested filter conditions
- fix check_http/check_tcp/check_dns help (#135)

0.26 Wed Jul 17 15:23:37 CEST 2024
Expand Down
67 changes: 65 additions & 2 deletions pkg/snclient/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,45 @@ func OperatorParse(str string) (Operator, error) {
return 0, fmt.Errorf("unknown operator: %s", str)
}

func (o *Operator) String() string {
switch *o {
case Equal:
return ("=")
case Unequal:
return ("!=")
case Contains:
return ("like")
case ContainsNot:
return ("unlike")
case ContainsNoCase:
return ("ilike")
case ContainsNotNoCase:
return ("not ilike")
case RegexMatch:
return ("~")
case RegexMatchNot:
return ("!~")
case RegexMatchNoCase:
return ("~~")
case RegexMatchNotNoCase:
return ("!~~")
case Lower:
return ("<")
case LowerEqual:
return ("<=")
case Greater:
return (">")
case GreaterEqual:
return (">=")
case InList:
return ("in")
case NotInList:
return ("not in")
}

return ("unknown")
}

// GroupOperator is the operator used to combine multiple filter conditions.
type GroupOperator uint8

Expand All @@ -119,6 +158,17 @@ const (
GroupOr
)

func (g *GroupOperator) String() string {
switch *g {
case GroupAnd:
return ("and")
case GroupOr:
return ("or")
}

return ("unknown")
}

// GroupOperatorParse parses group operator
func GroupOperatorParse(str string) (GroupOperator, error) {
switch strings.ToLower(str) {
Expand Down Expand Up @@ -173,7 +223,20 @@ func NewCondition(input string) (*Condition, error) {
}

func (c *Condition) String() string {
return c.original
if c.original != "" {
return c.original
}

if len(c.group) > 0 {
groups := []string{}
for _, g := range c.group {
groups = append(groups, g.String())
}

return " (" + strings.Join(groups, " "+c.groupOperator.String()+" ") + ") "
}

return fmt.Sprintf("%s %s %v", c.keyword, c.operator.String(), c.value)
}

// Match checks if given map matches current condition
Expand All @@ -185,7 +248,7 @@ func (c *Condition) Match(data map[string]string) (res, ok bool) {
if len(c.group) > 0 {
finalOK := true
for i := range c.group {
res, ok := c.group[i].matchSingle(data)
res, ok := c.group[i].Match(data)
if !ok {
finalOK = false

Expand Down

0 comments on commit c779e6a

Please sign in to comment.