From ff047d074862426ea25494cb5c2c7c8bd6b58147 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 25 Feb 2022 09:14:41 +1100 Subject: [PATCH] Fixes #1123 --- pkg/yqlib/candidate_node.go | 4 +++- pkg/yqlib/operator_assign_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pkg/yqlib/candidate_node.go b/pkg/yqlib/candidate_node.go index 18dc60e9b9..81642ac29b 100644 --- a/pkg/yqlib/candidate_node.go +++ b/pkg/yqlib/candidate_node.go @@ -124,7 +124,9 @@ func (n *CandidateNode) Copy() (*CandidateNode, error) { func (n *CandidateNode) UpdateFrom(other *CandidateNode, prefs assignPreferences) { // if this is an empty map or empty array, use the style of other node. - if n.Node.Kind != yaml.ScalarNode && len(n.Node.Content) == 0 { + if (n.Node.Kind != yaml.ScalarNode && len(n.Node.Content) == 0) || + // if the tag has changed (e.g. from str to bool) + (guessTagFromCustomType(n.Node) != guessTagFromCustomType(other.Node)) { n.Node.Style = other.Node.Style } diff --git a/pkg/yqlib/operator_assign_test.go b/pkg/yqlib/operator_assign_test.go index 42d11b24a2..74b6d77b2b 100644 --- a/pkg/yqlib/operator_assign_test.go +++ b/pkg/yqlib/operator_assign_test.go @@ -37,6 +37,33 @@ var assignOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::a:\n - cat\n", }, }, + { + skipDoc: true, + description: "change to number when old value is valid number", + document: `a: "3"`, + expression: `.a = 3`, + expected: []string{ + "D0, P[], (doc)::a: 3\n", + }, + }, + { + skipDoc: true, + description: "change to bool when old value is valid bool", + document: `a: "true"`, + expression: `.a = true`, + expected: []string{ + "D0, P[], (doc)::a: true\n", + }, + }, + { + skipDoc: true, + description: "update custom tag string, dont clobber style", + document: `a: !cat "meow"`, + expression: `.a = "woof"`, + expected: []string{ + "D0, P[], (doc)::a: !cat \"woof\"\n", + }, + }, { description: "Update node to be the child value", document: `{a: {b: {g: foof}}}`,