yq/pkg/yqlib/operator_union.go

44 lines
1.4 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:10:47 +00:00
2022-01-14 03:55:25 +00:00
import "container/list"
func unionOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2023-04-15 05:24:40 +00:00
log.Debug("unionOperator--")
log.Debug("unionOperator: context: %v", NodesToString(context.MatchingNodes))
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
2020-10-17 11:10:47 +00:00
if err != nil {
return Context{}, err
2020-10-17 11:10:47 +00:00
}
2023-04-15 05:24:40 +00:00
log.Debug("unionOperator: lhs: %v", NodesToString(lhs.MatchingNodes))
log.Debug("unionOperator: rhs input: %v", NodesToString(context.MatchingNodes))
log.Debug("unionOperator: rhs: %v", expressionNode.RHS.Operation.toString())
rhs, err := d.GetMatchingNodes(context, expressionNode.RHS)
2021-09-05 01:07:40 +00:00
2020-10-17 11:10:47 +00:00
if err != nil {
return Context{}, err
2020-10-17 11:10:47 +00:00
}
2023-04-15 05:24:40 +00:00
log.Debug("unionOperator: lhs: %v", lhs.ToString())
log.Debug("unionOperator: rhs: %v", rhs.ToString())
2021-09-05 01:07:40 +00:00
2022-01-14 03:55:25 +00:00
results := lhs.ChildContext(list.New())
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)
results.MatchingNodes.PushBack(node)
}
2021-09-05 01:07:40 +00:00
// this can happen when both expressions modify the context
// instead of creating their own.
/// (.foo = "bar"), (.thing = "cat")
if rhs.MatchingNodes != lhs.MatchingNodes {
for el := rhs.MatchingNodes.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)
2023-04-15 05:24:40 +00:00
log.Debug("union operator rhs: processing %v", NodeToString(node))
2021-09-05 01:07:40 +00:00
2022-01-14 03:55:25 +00:00
results.MatchingNodes.PushBack(node)
2021-09-05 01:07:40 +00:00
}
2020-10-17 11:10:47 +00:00
}
2023-04-15 05:24:40 +00:00
log.Debug("union operator: all together: %v", results.ToString())
2022-01-14 03:55:25 +00:00
return results, nil
2020-10-17 11:10:47 +00:00
}