From aab2af09194a77a6b93d81088baf981cd5e27c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Brauer?= Date: Mon, 14 Mar 2022 17:38:42 +0100 Subject: [PATCH] fix: coerce booleans to numbers for comparison in exprparser (#1030) Co-Authored-By: Markus Wolf Co-authored-by: Markus Wolf Co-authored-by: Casey Lee --- pkg/exprparser/interpreter.go | 4 +++- pkg/exprparser/interpreter_test.go | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/exprparser/interpreter.go b/pkg/exprparser/interpreter.go index df73fb4..d313862 100644 --- a/pkg/exprparser/interpreter.go +++ b/pkg/exprparser/interpreter.go @@ -305,6 +305,8 @@ func (impl *interperterImpl) compareValues(leftValue reflect.Value, rightValue r } switch leftValue.Kind() { + case reflect.Bool: + return impl.compareNumber(float64(impl.coerceToNumber(leftValue).Int()), float64(impl.coerceToNumber(rightValue).Int()), kind) case reflect.String: return impl.compareString(strings.ToLower(leftValue.String()), strings.ToLower(rightValue.String()), kind) @@ -323,7 +325,7 @@ func (impl *interperterImpl) compareValues(leftValue reflect.Value, rightValue r return impl.compareNumber(leftValue.Float(), rightValue.Float(), kind) default: - return nil, fmt.Errorf("TODO: evaluateCompare not implemented %+v", reflect.TypeOf(leftValue)) + return nil, fmt.Errorf("TODO: evaluateCompare not implemented! left: %+v, right: %+v", leftValue.Kind(), rightValue.Kind()) } } diff --git a/pkg/exprparser/interpreter_test.go b/pkg/exprparser/interpreter_test.go index a23d045..32a94e4 100644 --- a/pkg/exprparser/interpreter_test.go +++ b/pkg/exprparser/interpreter_test.go @@ -121,6 +121,10 @@ func TestOperatorsCompare(t *testing.T) { {`0 == '' }}`, true, "string-0-coercion-alt"}, {`3 == '3' }}`, true, "string-3-coercion-alt"}, {`'TEST' == 'test' }}`, true, "string-casing"}, + {"true > false }}", true, "bool-greater-than"}, + {"true >= false }}", true, "bool-greater-than-eq"}, + {"true >= true }}", true, "bool-greater-than-1"}, + {"true != false }}", true, "bool-not-equal"}, {`fromJSON('{}') < 2 }}`, false, "object-with-less"}, {`fromJSON('{}') < fromJSON('[]') }}`, false, "object/arr-with-lt"}, {`fromJSON('{}') > fromJSON('[]') }}`, false, "object/arr-with-gt"},