Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
coanor committed Sep 2, 2024
1 parent 778318a commit ff7be84
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 45 deletions.
104 changes: 59 additions & 45 deletions filter/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,13 @@ func binEval(op ItemType, lhs, rhs interface{}) bool {
}

case *NilLiteral:
log.Debugf("lv: %+#v", lv)
return lv.String() == Nil

default: // NOTE: interface{} EQ/NEQ, see: https://stackoverflow.com/a/34246225/342348

log.Debugf("lv type: %s, rhs: %s", lv, rhs)

switch rv := rhs.(type) {
case *Regex:
ok, err := regexp.MatchString(rv.Regex, lhs.(string))
Expand All @@ -196,6 +200,13 @@ func binEval(op ItemType, lhs, rhs interface{}) bool {
return !rhs.(*Regex).Re.MatchString(lhs.(string))

case NEQ:
log.Debugf("lhs: %s, rhs: %s", lhs, rhs)
_, lok := lhs.(*NilLiteral)
_, rok := rhs.(*NilLiteral)
if lok && rok {
return false
}

return lhs != rhs

case GTE, GT, LT, LTE: // rhs/lhs should be number or string
Expand Down Expand Up @@ -331,67 +342,70 @@ func (e *BinaryExpr) singleEval(data KVs) bool {
}

// first: fetch left-handle-symbol and OP on right-handle-symbol
switch lhs := e.LHS.(type) {
case *Identifier:
name := lhs.Name

switch e.Op {
case MATCH, NOT_MATCH:
for _, item := range e.RHS.(NodeList) {
if v, ok := data.Get(name); ok {
switch x := v.(type) {
case string:
if binEval(e.Op, x, item) {
return true
}
default:
continue
}
}
}
return false
lhs, ok := e.LHS.(*Identifier)
if !ok {
log.Errorf("unknown LHS type, expect Identifier, got `%s'", reflect.TypeOf(e.LHS).String())
return false
}

name := lhs.Name

case IN:
for _, item := range arr {
if v, ok := data.Get(name); ok {
if binEval(EQ, v, item) {
switch e.Op {
case MATCH, NOT_MATCH:
for _, item := range e.RHS.(NodeList) {
if v, ok := data.Get(name); ok {
switch x := v.(type) {
case string:
if binEval(e.Op, x, item) {
return true
}
} else {
return binEval(EQ, item, nilVal)
default:
continue
}
}
return false
}
return false

case NOT_IN:
for _, item := range arr {
if v, ok := data.Get(name); ok {
if binEval(EQ, v, item) {
return false
}
} else {
if binEval(EQ, nilVal, item) {
return false
}
case IN:
for _, item := range arr {
if v, ok := data.Get(name); ok {
if binEval(EQ, v, item) {
return true
}
} else {
return binEval(EQ, item, nilVal)
}
}
return false

return true

case GTE, GT, LT, LTE, NEQ, EQ:

case NOT_IN:
for _, item := range arr {
if v, ok := data.Get(name); ok {
if binEval(e.Op, v, lit) {
return true
if binEval(EQ, v, item) {
return false
}
} else {
if binEval(EQ, item, nilVal) {
return false
}
} else { // not exist in data
return binEval(EQ, lit, nilVal)
}
}

return true

case GTE, GT, LT, LTE, NEQ, EQ:
if v, ok := data.Get(name); ok {
if binEval(e.Op, v, lit) {
return true
}
} else { // not exist in data
log.Debugf("op %s on %s %s", e.Op, lit, nilVal)
return binEval(e.Op, lit, nilVal)
}
default:
log.Errorf("unknown LHS type, expect Identifier, got `%s'", reflect.TypeOf(e.LHS).String())
log.Warnf("unsupported operation %s on single-eval expr", e.Op)
}

return false
}

Expand Down
9 changes: 9 additions & 0 deletions filter/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ func TestExprConditions(t *testing.T) {
fields: map[string]any{"xyz": int64(123)},
pass: true,
},

{
in: "{ xyz = nil }",
pass: true,
},
{
in: "{ xyz != nil }",
pass: false,
},
}

for _, tc := range cases {
Expand Down

0 comments on commit ff7be84

Please sign in to comment.