mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-14 15:18:06 +00:00
36 lines
809 B
Go
36 lines
809 B
Go
package yqlib
|
|
|
|
func alternativeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
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)
|
|
}
|
|
|
|
func alternativeFunc(_ *dataTreeNavigator, _ Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
|
if lhs == nil {
|
|
return rhs, nil
|
|
}
|
|
if rhs == nil {
|
|
return lhs, nil
|
|
}
|
|
|
|
isTrue := isTruthyNode(lhs)
|
|
if isTrue {
|
|
return lhs, nil
|
|
}
|
|
return rhs, nil
|
|
}
|