Skip to content

Commit

Permalink
Fixes #1123
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Feb 27, 2022
1 parent 6c220d4 commit ff047d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/yqlib/candidate_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/yqlib/operator_assign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}`,
Expand Down

0 comments on commit ff047d0

Please sign in to comment.