Fixed += with multiple matches #1145

This commit is contained in:
Mike Farah 2022-03-19 18:42:12 +11:00
parent 99cd9e8aae
commit 0ffee92175
4 changed files with 27 additions and 7 deletions

View File

@ -161,15 +161,13 @@ func addSequences(target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode)
target.Node.Style = lhs.Node.Style target.Node.Style = lhs.Node.Style
} }
target.Node.Tag = lhs.Node.Tag 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) extraNodes, err := toNodes(rhs, lhs)
if err != nil { if err != nil {
return err return err
} }
target.Node.Content = append(target.Node.Content, extraNodes...) target.Node.Content = append(deepCloneContent(lhs.Node.Content), extraNodes...)
return nil return nil
} }

View File

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

View File

@ -93,6 +93,15 @@ var multiplyOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::sample:\n - &a\n - !!merge <<: *a\n", "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, skipDoc: true,
document: docWithHeader, document: docWithHeader,

View File

@ -19,14 +19,18 @@ func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNod
} }
assignmentOp := &Operation{OperationType: assignOpType, Preferences: expressionNode.Operation.Preferences} assignmentOp := &Operation{OperationType: assignOpType, Preferences: expressionNode.Operation.Preferences}
valueOp := &Operation{OperationType: valueOpType}
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() { for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
valueOp.CandidateNode = candidate clone, err := candidate.Copy()
valueExpression := &ExpressionNode{Operation: valueOp} if err != nil {
return Context{}, err
}
valueCopyExp := &ExpressionNode{Operation: &Operation{OperationType: valueOpType, CandidateNode: clone}}
assignmentOpNode := &ExpressionNode{Operation: assignmentOp, LHS: valueExpression, RHS: calculation(valueExpression, expressionNode.RHS)} valueExpression := &ExpressionNode{Operation: &Operation{OperationType: valueOpType, CandidateNode: candidate}}
assignmentOpNode := &ExpressionNode{Operation: assignmentOp, LHS: valueExpression, RHS: calculation(valueCopyExp, expressionNode.RHS)}
_, err = d.GetMatchingNodes(context, assignmentOpNode) _, err = d.GetMatchingNodes(context, assignmentOpNode)
if err != nil { if err != nil {