Fixed self assigned issue #1107

This commit is contained in:
Mike Farah 2022-02-20 14:29:52 +11:00
parent fc447b46ce
commit 665f6b0267
5 changed files with 53 additions and 6 deletions

View File

@ -116,15 +116,24 @@ func (n *CandidateNode) Copy() (*CandidateNode, error) {
if err != nil {
return nil, err
}
clone.Node = deepClone(n.Node)
return clone, nil
}
// updates this candidate from the given candidate node
func (n *CandidateNode) UpdateFrom(other *CandidateNode, prefs assignPreferences) {
n.UpdateAttributesFrom(other, prefs)
n.Node.Content = other.Node.Content
// 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 {
n.Node.Style = other.Node.Style
}
n.Node.Content = deepCloneContent(other.Node.Content)
n.Node.Kind = other.Node.Kind
n.Node.Value = other.Node.Value
n.UpdateAttributesFrom(other, prefs)
}
func (n *CandidateNode) UpdateAttributesFrom(other *CandidateNode, prefs assignPreferences) {
@ -150,10 +159,8 @@ func (n *CandidateNode) UpdateAttributesFrom(other *CandidateNode, prefs assignP
// merge will pickup the style of the new thing
// when autocreating nodes
//
// if this is an empty map or empty array, use the style of other node.
if n.Node.Style == 0 || (n.Node.Kind != yaml.ScalarNode && len(n.Node.Content) == 0) {
if n.Node.Style == 0 {
n.Node.Style = other.Node.Style
}

View File

@ -245,7 +245,35 @@ func recursiveNodeEqual(lhs *yaml.Node, rhs *yaml.Node) bool {
return recurseNodeObjectEqual(lhs, rhs)
}
return false
}
func deepCloneContent(content []*yaml.Node) []*yaml.Node {
clonedContent := make([]*yaml.Node, len(content))
for i, child := range content {
clonedContent[i] = deepClone(child)
}
return clonedContent
}
func deepClone(node *yaml.Node) *yaml.Node {
if node == nil {
return nil
}
clonedContent := deepCloneContent(node.Content)
return &yaml.Node{
Content: clonedContent,
Kind: node.Kind,
Style: node.Style,
Tag: node.Tag,
Value: node.Value,
Anchor: node.Anchor,
Alias: deepClone(node.Alias),
HeadComment: node.HeadComment,
LineComment: node.LineComment,
FootComment: node.FootComment,
Line: node.Line,
Column: node.Column,
}
}
// yaml numbers can be hex encoded...

View File

@ -201,7 +201,7 @@ func explodeNode(node *yaml.Node, context Context) error {
node.Kind = node.Alias.Kind
node.Style = node.Alias.Style
node.Tag = node.Alias.Tag
node.Content = node.Alias.Content
node.Content = deepCloneContent(node.Alias.Content)
node.Value = node.Alias.Value
node.Alias = nil
}

View File

@ -28,6 +28,15 @@ var assignOperatorScenarios = []expressionScenario{
"D0, P[], (doc)::{a: null}\n",
},
},
{
skipDoc: true,
description: "self reference",
document: "a: cat",
expression: `.a = [.a]`,
expected: []string{
"D0, P[], (doc)::a:\n - cat\n",
},
},
{
description: "Update node to be the child value",
document: `{a: {b: {g: foof}}}`,

View File

@ -1,3 +1,6 @@
4.20.2:
- Fixed self assignment issue (#1107)
4.20.1:
- New [Date Operators]((https://mikefarah.gitbook.io/yq/operators/datetime)) (now, tz, add and subtract durations from dates)
- Can now decode property files!