From d1b6a6fdd9f7716e663c167b2405ad09aa36638a Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 7 Jul 2021 14:29:24 +1000 Subject: [PATCH] Fixed merge bug https://github.com/mikefarah/yq/issues/880 --- pkg/yqlib/context.go | 6 ++++++ pkg/yqlib/operator_multiply.go | 4 ++-- pkg/yqlib/operator_multiply_test.go | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/yqlib/context.go b/pkg/yqlib/context.go index e5d9fb83..817527ab 100644 --- a/pkg/yqlib/context.go +++ b/pkg/yqlib/context.go @@ -66,3 +66,9 @@ func (n *Context) ReadOnlyClone() Context { clone.DontAutoCreate = true return clone } + +func (n *Context) WritableClone() Context { + clone := n.Clone() + clone.DontAutoCreate = false + return clone +} diff --git a/pkg/yqlib/operator_multiply.go b/pkg/yqlib/operator_multiply.go index 2cd94f5f..832b5c3c 100644 --- a/pkg/yqlib/operator_multiply.go +++ b/pkg/yqlib/operator_multiply.go @@ -32,11 +32,11 @@ func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, contex var newBlank = lhs.CreateChild(nil, &yaml.Node{}) - var newThing, err = mergeObjects(d, context, newBlank, lhs, multiplyPreferences{}) + var newThing, err = mergeObjects(d, context.WritableClone(), newBlank, lhs, multiplyPreferences{}) if err != nil { return nil, err } - return mergeObjects(d, context, newThing, rhs, preferences) + return mergeObjects(d, context.WritableClone(), newThing, rhs, preferences) } else if lhs.Node.Tag == "!!int" && rhs.Node.Tag == "!!int" { return multiplyIntegers(lhs, rhs) } else if (lhs.Node.Tag == "!!int" || lhs.Node.Tag == "!!float") && (rhs.Node.Tag == "!!int" || rhs.Node.Tag == "!!float") { diff --git a/pkg/yqlib/operator_multiply_test.go b/pkg/yqlib/operator_multiply_test.go index 1393801f..4de3f90a 100644 --- a/pkg/yqlib/operator_multiply_test.go +++ b/pkg/yqlib/operator_multiply_test.go @@ -47,6 +47,20 @@ var multiplyOperatorScenarios = []expressionScenario{ docExpected, }, }, + { + skipDoc: true, + expression: `.x = {"things": "whatever"} * {}`, + expected: []string{ + "D0, P[], ()::x:\n things: whatever\n", + }, + }, + { + skipDoc: true, + expression: `.x = {} * {"things": "whatever"}`, + expected: []string{ + "D0, P[], ()::x:\n things: whatever\n", + }, + }, { skipDoc: true, expression: `3 * 4.5`,