This commit is contained in:
Mike Farah 2021-07-07 15:30:45 +10:00
parent d1b6a6fdd9
commit ce3e347157
3 changed files with 31 additions and 4 deletions

View File

@ -15,11 +15,27 @@ func createAddOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
} }
func addAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { 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 := &Operation{OperationType: assignOpType}
assignmentOp.UpdateAssign = true valueOp := &Operation{OperationType: valueOpType}
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
assignmentOpNode := &ExpressionNode{Operation: assignmentOp, Lhs: expressionNode.Lhs, Rhs: createAddOp(selfExpression, expressionNode.Rhs)} for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
return d.GetMatchingNodes(context, assignmentOpNode) 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 { func toNodes(candidate *CandidateNode) []*yaml.Node {

View File

@ -14,6 +14,14 @@ var addOperatorScenarios = []expressionScenario{
"D0, P[1 a], (!!int)::3\n", "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, skipDoc: true,
document: `{}`, document: `{}`,

View File

@ -8,6 +8,9 @@ func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode
var rhs Context var rhs Context
if !expressionNode.Operation.UpdateAssign { if !expressionNode.Operation.UpdateAssign {
rhs, err = d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err = d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs)
if err != nil {
return Context{}, err
}
} }
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() { for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {