Skip to content

Commit

Permalink
add constant compare
Browse files Browse the repository at this point in the history
  • Loading branch information
coanor committed Sep 2, 2024
1 parent eae6b15 commit 99b5a0e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
28 changes: 25 additions & 3 deletions filter/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,37 @@ func (e *BinaryExpr) singleEval(data KVs) bool {
case *NilLiteral:
lit = rhs

case *BoolLiteral:
lit = rhs.Val

default:

log.Errorf("invalid RHS, got type `%s'", reflect.TypeOf(e.RHS).String())
return false
}

// first: fetch left-handle-symbol and OP on right-handle-symbol
lhs, ok := e.LHS.(*Identifier)
if !ok {
var lhs *Identifier
switch left := e.LHS.(type) {
case *NilLiteral:
return binEval(e.Op, nilVal, lit)

case *NumberLiteral:
if left.IsInt {
return binEval(e.Op, left.Int, lit)
} else {
return binEval(e.Op, left.Float, lit)
}

case *BoolLiteral:
return binEval(e.Op, left.Val, lit)

case *StringLiteral:
return binEval(e.Op, left.Val, lit)

case *Identifier:
lhs = left // we get detailed lhs value later...

default:
log.Errorf("unknown LHS type, expect Identifier, got `%s'", reflect.TypeOf(e.LHS).String())
return false
}
Expand Down
29 changes: 29 additions & 0 deletions filter/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,35 @@ func TestExprConditions(t *testing.T) {
in: "{ xyz != nil }",
pass: false,
},

{
in: "{ nil = nil }", // nil literal
pass: true,
},

{
in: "{ 1 = 1 }", // int literal
pass: true,
},

{
in: "{ true = true }", // boolean literal
pass: true,
},

{
in: "{ 'hello' = 'hello'}", // string literal
pass: true,
},

{
in: "{ 1.0 = 1.0 }", // float literal
pass: true,
},
{
in: "{ 'abc' = 'ABC' }",
pass: false,
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 99b5a0e

Please sign in to comment.