fix(equals): compare integers by value, not by raw text
The == and != operators matched the raw text of two scalars, so the same integer written in a different base never matched: `0x10 == 16`, `0o644 == 420` and `1_000 == 1000` were all false. This contradicted yq's own comparison operators, which parse those formats and agree the values are equal - for `0o644`, both `> 419` and `< 421` are true, yet `== 420` was false. It also contradicted the documented "Match number" behaviour of `==`. Give ==/!= the same !!int/!!int branch the comparison operators already have: parse both sides with the existing parseInt64 helper and compare the resulting int64s. If either side does not fit in an int64, fall back to the previous raw match rather than raising a new error. String globbing, floats and null handling are unchanged. Fixes #1977
This commit is contained in:
1 parent
b9be9a6184
commit
699d0e35c0
3 files changed
+86
-1
No files matched your search
@@ -146,6 +146,55 @@ var equalsOperatorScenarios = []expressionScenario{
|
||||
"D0, P[2], (!!bool)::true\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Match number written in a different base",
|
||||
subdescription: "Integers are compared by value, so the hexadecimal and octal forms of 16 match the decimal form.",
|
||||
document: `[0x10, 0o20, 16]`,
|
||||
expression: `.[] | (. == 16)`,
|
||||
expected: []string{
|
||||
"D0, P[0], (!!bool)::true\n",
|
||||
"D0, P[1], (!!bool)::true\n",
|
||||
"D0, P[2], (!!bool)::true\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `[0x10, 0o20, 16]`,
|
||||
expression: `.[] | (. != 16)`,
|
||||
expected: []string{
|
||||
"D0, P[0], (!!bool)::false\n",
|
||||
"D0, P[1], (!!bool)::false\n",
|
||||
"D0, P[2], (!!bool)::false\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `[1_000, 1000]`,
|
||||
expression: `.[] | (. == 1000)`,
|
||||
expected: []string{
|
||||
"D0, P[0], (!!bool)::true\n",
|
||||
"D0, P[1], (!!bool)::true\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `[0x10, 17]`,
|
||||
expression: `.[] | (. == 16)`,
|
||||
expected: []string{
|
||||
"D0, P[0], (!!bool)::true\n",
|
||||
"D0, P[1], (!!bool)::false\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
// integers that don't fit in an int64 can't be compared numerically,
|
||||
// fall back to matching the raw values.
|
||||
skipDoc: true,
|
||||
document: `[9223372036854775808, 9223372036854775808]`,
|
||||
expression: `.[0] == .[1]`,
|
||||
expected: []string{
|
||||
"D0, P[0], (!!bool)::true\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `a: { cat: {b: apple, c: whatever}, pat: {b: banana} }`,
|
||||
|
||||
Reference in new issue
Block a user