yq/pkg/yqlib/operator_alternative.go

36 lines
809 B
Go
Raw Normal View History

2020-12-21 00:32:34 +00:00
package yqlib
func alternativeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2024-02-15 22:41:33 +00:00
log.Debugf("alternative")
prefs := crossFunctionPreferences{
CalcWhenEmpty: true,
Calculation: alternativeFunc,
LhsResultValue: func(lhs *CandidateNode) (*CandidateNode, error) {
if lhs == nil {
return nil, nil
}
truthy := isTruthyNode(lhs)
if truthy {
return lhs, nil
}
return nil, nil
},
}
return crossFunctionWithPrefs(d, context, expressionNode, prefs)
2020-12-21 00:32:34 +00:00
}
2024-01-11 02:17:34 +00:00
func alternativeFunc(_ *dataTreeNavigator, _ Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
if lhs == nil {
return rhs, nil
}
2021-06-09 23:35:07 +00:00
if rhs == nil {
return lhs, nil
}
2020-12-21 00:32:34 +00:00
isTrue := isTruthyNode(lhs)
if isTrue {
2020-12-21 00:32:34 +00:00
return lhs, nil
}
return rhs, nil
}