Skip to content

Commit 51b64e6

Browse files
committed
Fixing relative merge bug #1333
1 parent 851d93e commit 51b64e6

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pkg/yqlib/operator_multiply_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,24 @@ var multiplyOperatorScenarios = []expressionScenario{
561561
"D0, P[], (doc)::a: {cat: !horse 5}\nb: {cat: 5}\n",
562562
},
563563
},
564+
{
565+
skipDoc: true,
566+
description: "Relative merge, new fields only",
567+
document: "a: {a: original}\n",
568+
expression: `.a *=n load("../../examples/thing.yml")`,
569+
expected: []string{
570+
"D0, P[], (doc)::a: {a: original, b: cool.}\n",
571+
},
572+
},
573+
{
574+
skipDoc: true,
575+
description: "Relative merge",
576+
document: "a: {a: original}\n",
577+
expression: `.a *= load("../../examples/thing.yml")`,
578+
expected: []string{
579+
"D0, P[], (doc)::a: {a: apple is included, b: cool.}\n",
580+
},
581+
},
564582
}
565583

566584
func TestMultiplyOperatorScenarios(t *testing.T) {

pkg/yqlib/operators.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,21 @@ func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNod
1919
return Context{}, err
2020
}
2121

22-
assignmentOp := &Operation{OperationType: assignOpType, Preferences: expressionNode.Operation.Preferences}
22+
// tricky logic when we are running *= with flags.
23+
// we have an op like: .a *=nc .b
24+
// which should roughly translate to .a =c .a *nc .b
25+
// note that the 'n' flag only applies to the multiple op, not the assignment
26+
// but the clobber flag applies to both!
27+
28+
prefs := assignPreferences{}
29+
switch typedPref := expressionNode.Operation.Preferences.(type) {
30+
case assignPreferences:
31+
prefs = typedPref
32+
case multiplyPreferences:
33+
prefs.ClobberCustomTags = typedPref.AssignPrefs.ClobberCustomTags
34+
}
35+
36+
assignmentOp := &Operation{OperationType: assignOpType, Preferences: prefs}
2337

2438
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
2539
candidate := el.Value.(*CandidateNode)

0 commit comments

Comments
 (0)