Skip to content

Commit

Permalink
Fixed append array issue #874
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Jul 7, 2021
1 parent d1b6a6f commit ce3e347
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
24 changes: 20 additions & 4 deletions pkg/yqlib/operator_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions pkg/yqlib/operator_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: `{}`,
Expand Down
3 changes: 3 additions & 0 deletions pkg/yqlib/operator_assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ce3e347

Please sign in to comment.