yq/pkg/yqlib/operator_select.go

42 lines
994 B
Go
Raw Permalink Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:10:47 +00:00
import (
2020-10-21 01:54:58 +00:00
"container/list"
2020-10-17 11:10:47 +00:00
)
func selectOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2020-10-17 11:10:47 +00:00
log.Debugf("-- selectOperation")
2020-10-21 01:54:58 +00:00
var results = list.New()
2020-10-17 11:10:47 +00:00
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-10-17 11:10:47 +00:00
candidate := el.Value.(*CandidateNode)
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
2020-10-17 11:10:47 +00:00
if err != nil {
return Context{}, err
2020-10-17 11:10:47 +00:00
}
2021-10-07 04:04:36 +00:00
// find any truthy node
var errDecoding error
includeResult := false
2020-10-17 11:10:47 +00:00
2021-10-07 04:04:36 +00:00
for resultEl := rhs.MatchingNodes.Front(); resultEl != nil; resultEl = resultEl.Next() {
result := resultEl.Value.(*CandidateNode)
includeResult, errDecoding = isTruthy(result)
log.Debugf("isTruthy %v", includeResult)
2020-10-17 11:10:47 +00:00
if errDecoding != nil {
return Context{}, errDecoding
2020-10-17 11:10:47 +00:00
}
if includeResult {
2021-10-07 04:04:36 +00:00
break
2020-10-17 11:10:47 +00:00
}
}
2021-10-07 04:04:36 +00:00
if includeResult {
results.PushBack(candidate)
}
2020-10-17 11:10:47 +00:00
}
return context.ChildContext(results), nil
2020-10-17 11:10:47 +00:00
}