Skip to content

Commit

Permalink
Fixed += with multiple matches #1145
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Mar 19, 2022
1 parent 99cd9e8 commit 0ffee92
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
4 changes: 1 addition & 3 deletions pkg/yqlib/operator_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,13 @@ func addSequences(target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode)
target.Node.Style = lhs.Node.Style
}
target.Node.Tag = lhs.Node.Tag
target.Node.Content = make([]*yaml.Node, len(lhs.Node.Content))
copy(target.Node.Content, lhs.Node.Content)

extraNodes, err := toNodes(rhs, lhs)
if err != nil {
return err
}

target.Node.Content = append(target.Node.Content, extraNodes...)
target.Node.Content = append(deepCloneContent(lhs.Node.Content), extraNodes...)
return nil

}
Expand Down
9 changes: 9 additions & 0 deletions pkg/yqlib/operator_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ var addOperatorScenarios = []expressionScenario{
"D0, P[1 a], (!!int)::3\n",
},
},
{
skipDoc: true,
document: `[[c], [b]]`,
expression: `.[] | . += "a"`,
expected: []string{
"D0, P[0], (!!seq)::[c, a]\n",
"D0, P[1], (!!seq)::[b, a]\n",
},
},
{
skipDoc: true,
document: `{}`,
Expand Down
9 changes: 9 additions & 0 deletions pkg/yqlib/operator_multiply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ var multiplyOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::sample:\n - &a\n - !!merge <<: *a\n",
},
},
{
skipDoc: true,
document: `[[c], [b]]`,
expression: `.[] | . *+ ["a"]`,
expected: []string{
"D0, P[0], (!!seq)::[c, a]\n",
"D0, P[1], (!!seq)::[b, a]\n",
},
},
{
skipDoc: true,
document: docWithHeader,
Expand Down
12 changes: 8 additions & 4 deletions pkg/yqlib/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNod
}

assignmentOp := &Operation{OperationType: assignOpType, Preferences: expressionNode.Operation.Preferences}
valueOp := &Operation{OperationType: valueOpType}

for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
valueOp.CandidateNode = candidate
valueExpression := &ExpressionNode{Operation: valueOp}
clone, err := candidate.Copy()
if err != nil {
return Context{}, err
}
valueCopyExp := &ExpressionNode{Operation: &Operation{OperationType: valueOpType, CandidateNode: clone}}

valueExpression := &ExpressionNode{Operation: &Operation{OperationType: valueOpType, CandidateNode: candidate}}

assignmentOpNode := &ExpressionNode{Operation: assignmentOp, LHS: valueExpression, RHS: calculation(valueExpression, expressionNode.RHS)}
assignmentOpNode := &ExpressionNode{Operation: assignmentOp, LHS: valueExpression, RHS: calculation(valueCopyExp, expressionNode.RHS)}

_, err = d.GetMatchingNodes(context, assignmentOpNode)
if err != nil {
Expand Down

0 comments on commit 0ffee92

Please sign in to comment.