yq/pkg/yqlib/operator_equals.go

54 lines
1.8 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-16 01:29:26 +00:00
func equalsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2020-10-16 01:29:26 +00:00
log.Debugf("-- equalsOperation")
return crossFunction(d, context, expressionNode, isEquals(false), true)
2020-10-16 01:29:26 +00:00
}
2021-02-04 22:54:03 +00:00
func isEquals(flip bool) func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
return func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
value := false
log.Debugf("-- isEquals cross function")
if lhs == nil && rhs == nil {
2023-05-05 04:13:18 +00:00
log.Debugf("-- both are nil")
owner := &CandidateNode{}
return createBooleanCandidate(owner, !flip), nil
} else if lhs == nil {
log.Debugf("lhs nil, but rhs is not")
2023-04-11 02:39:22 +00:00
rhsNode := rhs.unwrapDocument()
value := rhsNode.Tag == "!!null"
if flip {
value = !value
}
return createBooleanCandidate(rhs, value), nil
} else if rhs == nil {
log.Debugf("lhs not nil, but rhs is")
2023-04-11 02:39:22 +00:00
lhsNode := lhs.unwrapDocument()
value := lhsNode.Tag == "!!null"
if flip {
value = !value
}
return createBooleanCandidate(lhs, value), nil
}
2023-04-11 02:39:22 +00:00
lhsNode := lhs.unwrapDocument()
rhsNode := rhs.unwrapDocument()
2021-02-04 22:54:03 +00:00
if lhsNode.Tag == "!!null" {
value = (rhsNode.Tag == "!!null")
2023-04-11 02:39:22 +00:00
} else if lhsNode.Kind == ScalarNode && rhsNode.Kind == ScalarNode {
2021-02-04 22:54:03 +00:00
value = matchKey(lhsNode.Value, rhsNode.Value)
}
log.Debugf("%v == %v ? %v", NodeToString(lhs), NodeToString(rhs), value)
if flip {
value = !value
}
return createBooleanCandidate(lhs, value), nil
2020-10-16 01:29:26 +00:00
}
2021-02-04 22:54:03 +00:00
}
func notEqualsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- notEqualsOperator")
return crossFunction(d, context.ReadOnlyClone(), expressionNode, isEquals(true), true)
2020-10-16 01:29:26 +00:00
}