2020-12-21 00:32:34 +00:00
|
|
|
package yqlib
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func alternativeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-12-21 00:32:34 +00:00
|
|
|
log.Debugf("-- alternative")
|
2022-09-09 00:18:49 +00:00
|
|
|
prefs := crossFunctionPreferences{
|
|
|
|
CalcWhenEmpty: true,
|
|
|
|
Calculation: alternativeFunc,
|
|
|
|
LhsResultValue: func(lhs *CandidateNode) (*CandidateNode, error) {
|
|
|
|
if lhs == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
truthy := isTruthyNode(lhs)
|
2022-09-09 00:18:49 +00:00
|
|
|
if truthy {
|
|
|
|
return lhs, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return crossFunctionWithPrefs(d, context, expressionNode, prefs)
|
2020-12-21 00:32:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func alternativeFunc(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
2021-04-13 00:42:20 +00:00
|
|
|
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
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
isTrue := isTruthyNode(lhs)
|
|
|
|
if isTrue {
|
2020-12-21 00:32:34 +00:00
|
|
|
return lhs, nil
|
|
|
|
}
|
|
|
|
return rhs, nil
|
|
|
|
}
|