fix: coerce booleans to numbers for comparison in exprparser (#1030)

Co-Authored-By: Markus Wolf <markus.wolf@new-work.se>

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>
Co-authored-by: Casey Lee <cplee@nektos.com>
This commit is contained in:
Björn Brauer 2022-03-14 17:38:42 +01:00 committed by GitHub
parent 14c98015c5
commit aab2af0919
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View file

@ -305,6 +305,8 @@ func (impl *interperterImpl) compareValues(leftValue reflect.Value, rightValue r
} }
switch leftValue.Kind() { switch leftValue.Kind() {
case reflect.Bool:
return impl.compareNumber(float64(impl.coerceToNumber(leftValue).Int()), float64(impl.coerceToNumber(rightValue).Int()), kind)
case reflect.String: case reflect.String:
return impl.compareString(strings.ToLower(leftValue.String()), strings.ToLower(rightValue.String()), kind) 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) return impl.compareNumber(leftValue.Float(), rightValue.Float(), kind)
default: 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())
} }
} }

View file

@ -121,6 +121,10 @@ func TestOperatorsCompare(t *testing.T) {
{`0 == '' }}`, true, "string-0-coercion-alt"}, {`0 == '' }}`, true, "string-0-coercion-alt"},
{`3 == '3' }}`, true, "string-3-coercion-alt"}, {`3 == '3' }}`, true, "string-3-coercion-alt"},
{`'TEST' == 'test' }}`, true, "string-casing"}, {`'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('{}') < 2 }}`, false, "object-with-less"},
{`fromJSON('{}') < fromJSON('[]') }}`, false, "object/arr-with-lt"}, {`fromJSON('{}') < fromJSON('[]') }}`, false, "object/arr-with-lt"},
{`fromJSON('{}') > fromJSON('[]') }}`, false, "object/arr-with-gt"}, {`fromJSON('{}') > fromJSON('[]') }}`, false, "object/arr-with-gt"},