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 { if err != nil {
return nil, err return nil, err
} }
clone.Node = deepClone(n.Node)
return clone, nil return clone, nil
} }
// updates this candidate from the given candidate node // updates this candidate from the given candidate node
func (n *CandidateNode) UpdateFrom(other *CandidateNode, prefs assignPreferences) { func (n *CandidateNode) UpdateFrom(other *CandidateNode, prefs assignPreferences) {
n.UpdateAttributesFrom(other, prefs) // if this is an empty map or empty array, use the style of other node.
n.Node.Content = other.Node.Content 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.Node.Value = other.Node.Value
n.UpdateAttributesFrom(other, prefs)
} }
func (n *CandidateNode) UpdateAttributesFrom(other *CandidateNode, prefs assignPreferences) { 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 // merge will pickup the style of the new thing
// when autocreating nodes // 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 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 recurseNodeObjectEqual(lhs, rhs)
} }
return false 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... // 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.Kind = node.Alias.Kind
node.Style = node.Alias.Style node.Style = node.Alias.Style
node.Tag = node.Alias.Tag node.Tag = node.Alias.Tag
node.Content = node.Alias.Content node.Content = deepCloneContent(node.Alias.Content)
node.Value = node.Alias.Value node.Value = node.Alias.Value
node.Alias = nil node.Alias = nil
} }

View File

@ -28,6 +28,15 @@ var assignOperatorScenarios = []expressionScenario{
"D0, P[], (doc)::{a: null}\n", "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", description: "Update node to be the child value",
document: `{a: {b: {g: foof}}}`, document: `{a: {b: {g: foof}}}`,

View File

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