Skip to content

Commit

Permalink
Fixing relative merge bug #1333
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Sep 9, 2022
1 parent 851d93e commit 51b64e6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
18 changes: 18 additions & 0 deletions pkg/yqlib/operator_multiply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,24 @@ var multiplyOperatorScenarios = []expressionScenario{
"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) {
Expand Down
16 changes: 15 additions & 1 deletion pkg/yqlib/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNod
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() {
candidate := el.Value.(*CandidateNode)
Expand Down

0 comments on commit 51b64e6

Please sign in to comment.