diff --git a/pkg/yqlib/operator_union.go b/pkg/yqlib/operator_union.go index e1d54e3a..f2334b2d 100644 --- a/pkg/yqlib/operator_union.go +++ b/pkg/yqlib/operator_union.go @@ -1,5 +1,7 @@ package yqlib +import "container/list" + func unionOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { log.Debug("unionOperator") log.Debug("context: %v", NodesToString(context.MatchingNodes)) @@ -18,19 +20,25 @@ func unionOperator(d *dataTreeNavigator, context Context, expressionNode *Expres log.Debug("lhs: %v", lhs.ToString()) log.Debug("rhs: %v", rhs.ToString()) + results := lhs.ChildContext(list.New()) + for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() { + node := el.Value.(*CandidateNode) + results.MatchingNodes.PushBack(node) + } + // 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) log.Debug("processing %v", NodeToString(node)) - lhs.MatchingNodes.PushBack(node) + results.MatchingNodes.PushBack(node) } } - log.Debug("all together: %v", lhs.ToString()) - return lhs, nil + log.Debug("and lets print it out") + log.Debug("all together: %v", results.ToString()) + return results, nil } diff --git a/pkg/yqlib/operator_union_test.go b/pkg/yqlib/operator_union_test.go index 1409fd7f..ce12f19a 100644 --- a/pkg/yqlib/operator_union_test.go +++ b/pkg/yqlib/operator_union_test.go @@ -13,6 +13,14 @@ var unionOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::{}\n", }, }, + { + skipDoc: true, + description: "clone test", + expression: `"abc" as $a | [$a, "cat"]`, + expected: []string{ + "D0, P[], (!!seq)::- abc\n- cat\n", + }, + }, { skipDoc: true, expression: `(.foo = "bar"), (.toe = "jam")`,