Files
dualfroz 699d0e35c0 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
2026-09-03 13:29:22 +02:00

65 lines
2.3 KiB
Go

package yqlib
func equalsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("equalsOperation")
return crossFunction(d, context, expressionNode, isEquals(false), true)
}
// scalarsMatch compares two scalar nodes. Integers are compared by value, so
// that the same number written in a different base (e.g. 0x10 and 16) matches,
// like the comparison operators already do. Anything else - including integers
// too big for an int64 - falls back to matching the raw values.
func scalarsMatch(lhs *CandidateNode, rhs *CandidateNode) bool {
if lhs.guessTagFromCustomType() == "!!int" && rhs.guessTagFromCustomType() == "!!int" {
_, lhsNum, lhsErr := parseInt64(lhs.Value)
_, rhsNum, rhsErr := parseInt64(rhs.Value)
if lhsErr == nil && rhsErr == nil {
return lhsNum == rhsNum
}
log.Debugf("could not parse %v and/or %v as int64, comparing the raw values", lhs.Value, rhs.Value)
}
return matchKey(lhs.Value, rhs.Value)
}
func isEquals(flip bool) func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
return func(_ *dataTreeNavigator, _ Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
value := false
log.Debugf("isEquals cross function")
if lhs == nil && rhs == nil {
log.Debugf("both are nil")
owner := &CandidateNode{}
return createBooleanCandidate(owner, !flip), nil
} else if lhs == nil {
log.Debugf("lhs nil, but rhs is not")
value := rhs.Tag == "!!null"
if flip {
value = !value
}
return createBooleanCandidate(rhs, value), nil
} else if rhs == nil {
log.Debugf("lhs not nil, but rhs is")
value := lhs.Tag == "!!null"
if flip {
value = !value
}
return createBooleanCandidate(lhs, value), nil
}
if lhs.Tag == "!!null" {
value = (rhs.Tag == "!!null")
} else if lhs.Kind == ScalarNode && rhs.Kind == ScalarNode {
value = scalarsMatch(lhs, rhs)
}
log.Debugf("%v == %v ? %v", NodeToString(lhs), NodeToString(rhs), value)
if flip {
value = !value
}
return createBooleanCandidate(lhs, value), nil
}
}
func notEqualsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("notEqualsOperator")
return crossFunction(d, context.ReadOnlyClone(), expressionNode, isEquals(true), true)
}