From 6f9f80cc4805af949195c0bf605475588304022f Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 14 Apr 2022 12:03:18 +1000 Subject: [PATCH] Fixes with operation bug #1174 --- pkg/yqlib/operator_with.go | 10 +++++++--- pkg/yqlib/operator_with_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/yqlib/operator_with.go b/pkg/yqlib/operator_with.go index 87726267..92da7348 100644 --- a/pkg/yqlib/operator_with.go +++ b/pkg/yqlib/operator_with.go @@ -20,9 +20,13 @@ func withOperator(d *dataTreeNavigator, context Context, expressionNode *Express updateExp := expressionNode.RHS.RHS - _, err = d.GetMatchingNodes(updateContext, updateExp) - if err != nil { - return Context{}, err + for el := updateContext.MatchingNodes.Front(); el != nil; el = el.Next() { + candidate := el.Value.(*CandidateNode) + _, err = d.GetMatchingNodes(updateContext.SingleChildContext(candidate), updateExp) + if err != nil { + return Context{}, err + } + } return context, nil diff --git a/pkg/yqlib/operator_with_test.go b/pkg/yqlib/operator_with_test.go index aad4d15c..04f184c2 100644 --- a/pkg/yqlib/operator_with_test.go +++ b/pkg/yqlib/operator_with_test.go @@ -28,6 +28,16 @@ var withOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::myArray: [{a: apple, b: apple yum}, {a: banana, b: banana yum}]\n", }, }, + { + description: "Update array elements relatively +=", + skipDoc: true, + subdescription: "The second expression runs with each element of the array as it's contextual root. This allows you to make updates relative to the element.", + document: `myArray: [{a: apple},{a: banana}]`, + expression: `with(.myArray[]; .a += .a)`, + expected: []string{ + "D0, P[], (doc)::myArray: [{a: appleapple}, {a: bananabanana}]\n", + }, + }, } func TestWithOperatorScenarios(t *testing.T) {