Skip to content

Commit aab2af0

Browse files
ZauberNerdKnisterPetercplee
authored
fix: coerce booleans to numbers for comparison in exprparser (#1030)
Co-Authored-By: Markus Wolf <[email protected]> Co-authored-by: Markus Wolf <[email protected]> Co-authored-by: Casey Lee <[email protected]>
1 parent 14c9801 commit aab2af0

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

pkg/exprparser/interpreter.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ func (impl *interperterImpl) compareValues(leftValue reflect.Value, rightValue r
305305
}
306306

307307
switch leftValue.Kind() {
308+
case reflect.Bool:
309+
return impl.compareNumber(float64(impl.coerceToNumber(leftValue).Int()), float64(impl.coerceToNumber(rightValue).Int()), kind)
308310
case reflect.String:
309311
return impl.compareString(strings.ToLower(leftValue.String()), strings.ToLower(rightValue.String()), kind)
310312

@@ -323,7 +325,7 @@ func (impl *interperterImpl) compareValues(leftValue reflect.Value, rightValue r
323325
return impl.compareNumber(leftValue.Float(), rightValue.Float(), kind)
324326

325327
default:
326-
return nil, fmt.Errorf("TODO: evaluateCompare not implemented %+v", reflect.TypeOf(leftValue))
328+
return nil, fmt.Errorf("TODO: evaluateCompare not implemented! left: %+v, right: %+v", leftValue.Kind(), rightValue.Kind())
327329
}
328330
}
329331

pkg/exprparser/interpreter_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ func TestOperatorsCompare(t *testing.T) {
121121
{`0 == '' }}`, true, "string-0-coercion-alt"},
122122
{`3 == '3' }}`, true, "string-3-coercion-alt"},
123123
{`'TEST' == 'test' }}`, true, "string-casing"},
124+
{"true > false }}", true, "bool-greater-than"},
125+
{"true >= false }}", true, "bool-greater-than-eq"},
126+
{"true >= true }}", true, "bool-greater-than-1"},
127+
{"true != false }}", true, "bool-not-equal"},
124128
{`fromJSON('{}') < 2 }}`, false, "object-with-less"},
125129
{`fromJSON('{}') < fromJSON('[]') }}`, false, "object/arr-with-lt"},
126130
{`fromJSON('{}') > fromJSON('[]') }}`, false, "object/arr-with-gt"},

0 commit comments

Comments
 (0)