This commit is contained in:
Mike Farah 2022-02-25 09:14:41 +11:00
parent 6c220d4562
commit ff047d0748
2 changed files with 30 additions and 1 deletions

View File

@ -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
}

View File

@ -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}}}`,