diff --git a/pkg/yqlib/operator_add.go b/pkg/yqlib/operator_add.go index 2f850ee0..4bd5045c 100644 --- a/pkg/yqlib/operator_add.go +++ b/pkg/yqlib/operator_add.go @@ -15,11 +15,27 @@ func createAddOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { } func addAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { + lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) + if err != nil { + return Context{}, err + } + assignmentOp := &Operation{OperationType: assignOpType} - assignmentOp.UpdateAssign = true - selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}} - assignmentOpNode := &ExpressionNode{Operation: assignmentOp, Lhs: expressionNode.Lhs, Rhs: createAddOp(selfExpression, expressionNode.Rhs)} - return d.GetMatchingNodes(context, assignmentOpNode) + 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} + + assignmentOpNode := &ExpressionNode{Operation: assignmentOp, Lhs: valueExpression, Rhs: createAddOp(valueExpression, expressionNode.Rhs)} + + _, err = d.GetMatchingNodes(context, assignmentOpNode) + if err != nil { + return Context{}, err + } + } + return context, nil } func toNodes(candidate *CandidateNode) []*yaml.Node { diff --git a/pkg/yqlib/operator_add_test.go b/pkg/yqlib/operator_add_test.go index da7d5852..cd478d37 100644 --- a/pkg/yqlib/operator_add_test.go +++ b/pkg/yqlib/operator_add_test.go @@ -14,6 +14,14 @@ var addOperatorScenarios = []expressionScenario{ "D0, P[1 a], (!!int)::3\n", }, }, + { + skipDoc: true, + document: "array: [1]\narray2: [2]", + expression: ".array += .array2", + expected: []string{ + "D0, P[], (doc)::array: [1, 2]\narray2: [2]\n", + }, + }, { skipDoc: true, document: `{}`, diff --git a/pkg/yqlib/operator_assign.go b/pkg/yqlib/operator_assign.go index e9695b43..6cb11b7a 100644 --- a/pkg/yqlib/operator_assign.go +++ b/pkg/yqlib/operator_assign.go @@ -8,6 +8,9 @@ func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode var rhs Context if !expressionNode.Operation.UpdateAssign { rhs, err = d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + if err != nil { + return Context{}, err + } } for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {