diff --git a/pkg/yqlib/operator_entries.go b/pkg/yqlib/operator_entries.go index 3434ed94..9b791d63 100644 --- a/pkg/yqlib/operator_entries.go +++ b/pkg/yqlib/operator_entries.go @@ -142,15 +142,18 @@ func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode * return Context{}, err } - result, err := d.GetMatchingNodes(splatted, expressionNode.RHS) - log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType) - log.Debug("result %v", result) - if err != nil { - return Context{}, err + newResults := list.New() + + for itemEl := splatted.MatchingNodes.Front(); itemEl != nil; itemEl = itemEl.Next() { + result, err := d.GetMatchingNodes(splatted.SingleChildContext(itemEl.Value.(*CandidateNode)), expressionNode.RHS) + if err != nil { + return Context{}, err + } + newResults.PushBackList(result.MatchingNodes) } selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}} - collected, err := collectTogether(d, result, selfExpression) + collected, err := collectTogether(d, splatted.ChildContext(newResults), selfExpression) if err != nil { return Context{}, err } diff --git a/pkg/yqlib/operator_entries_test.go b/pkg/yqlib/operator_entries_test.go index 701f6d82..d4607124 100644 --- a/pkg/yqlib/operator_entries_test.go +++ b/pkg/yqlib/operator_entries_test.go @@ -66,11 +66,21 @@ var entriesOperatorScenarios = []expressionScenario{ { description: "Use with_entries to update keys", document: `{a: 1, b: 2}`, - expression: `with_entries(.key |= "KEY_" + .)`, + // expression: `to_entries | with(.[]; .key |= "KEY_" + .) | from_entries`, + expression: `with_entries(.key |= "KEY_" + .)`, expected: []string{ "D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n", }, }, + { + skipDoc: true, + description: "Use with_entries to update keys comment", + document: `{a: 1, b: 2}`, + expression: `with_entries(.key headComment= .value)`, + expected: []string{ + "D0, P[], (!!map)::# 1\na: 1\n# 2\nb: 2\n", + }, + }, { description: "Custom sort map keys", subdescription: "Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.",