Skip to content

Commit

Permalink
Fixes with operation bug #1174
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Apr 14, 2022
1 parent 13a27e8 commit 6f9f80c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/yqlib/operator_with.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/operator_with_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6f9f80c

Please sign in to comment.