yq/pkg/yqlib/operator_select.go

39 lines
924 B
Go
Raw 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
}
// grab the first value
first := rhs.MatchingNodes.Front()
2020-10-17 11:10:47 +00:00
if first != nil {
result := first.Value.(*CandidateNode)
log.Debugf("result %v", NodeToString(result))
2020-10-17 11:10:47 +00:00
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 {
2020-10-21 01:54:58 +00:00
results.PushBack(candidate)
2020-10-17 11:10:47 +00:00
}
}
}
return context.ChildContext(results), nil
2020-10-17 11:10:47 +00:00
}