Fixing relative merge bug #1333

This commit is contained in:
Mike Farah 2022-09-09 12:28:38 +10:00
parent 851d93e5a8
commit 51b64e6cf3
2 changed files with 33 additions and 1 deletions

View File

@ -561,6 +561,24 @@ var multiplyOperatorScenarios = []expressionScenario{
"D0, P[], (doc)::a: {cat: !horse 5}\nb: {cat: 5}\n", "D0, P[], (doc)::a: {cat: !horse 5}\nb: {cat: 5}\n",
}, },
}, },
{
skipDoc: true,
description: "Relative merge, new fields only",
document: "a: {a: original}\n",
expression: `.a *=n load("../../examples/thing.yml")`,
expected: []string{
"D0, P[], (doc)::a: {a: original, b: cool.}\n",
},
},
{
skipDoc: true,
description: "Relative merge",
document: "a: {a: original}\n",
expression: `.a *= load("../../examples/thing.yml")`,
expected: []string{
"D0, P[], (doc)::a: {a: apple is included, b: cool.}\n",
},
},
} }
func TestMultiplyOperatorScenarios(t *testing.T) { func TestMultiplyOperatorScenarios(t *testing.T) {

View File

@ -19,7 +19,21 @@ func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNod
return Context{}, err return Context{}, err
} }
assignmentOp := &Operation{OperationType: assignOpType, Preferences: expressionNode.Operation.Preferences} // tricky logic when we are running *= with flags.
// we have an op like: .a *=nc .b
// which should roughly translate to .a =c .a *nc .b
// note that the 'n' flag only applies to the multiple op, not the assignment
// but the clobber flag applies to both!
prefs := assignPreferences{}
switch typedPref := expressionNode.Operation.Preferences.(type) {
case assignPreferences:
prefs = typedPref
case multiplyPreferences:
prefs.ClobberCustomTags = typedPref.AssignPrefs.ClobberCustomTags
}
assignmentOp := &Operation{OperationType: assignOpType, Preferences: prefs}
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)