mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-06 18:24:57 +08:00
Fixing merge anchor key order
This commit is contained in:
@@ -138,6 +138,41 @@ func explodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expr
|
||||
return context, nil
|
||||
}
|
||||
|
||||
func fixedReconstructAliasedMap(node *CandidateNode) error {
|
||||
var newContent = []*CandidateNode{}
|
||||
|
||||
for index := 0; index < len(node.Content); index = index + 2 {
|
||||
keyNode := node.Content[index]
|
||||
valueNode := node.Content[index+1]
|
||||
if keyNode.Value != "<<" {
|
||||
// always add in plain nodes
|
||||
newContent = append(newContent, keyNode, valueNode)
|
||||
} else {
|
||||
sequence := valueNode
|
||||
if sequence.Kind != SequenceNode {
|
||||
sequence = &CandidateNode{Content: []*CandidateNode{sequence}}
|
||||
}
|
||||
for index := 0; index < len(sequence.Content); index = index + 1 {
|
||||
// for merge anchors, we only set them if the key is not already in node or the newContent
|
||||
mergeNodeSeq := sequence.Content[index]
|
||||
if mergeNodeSeq.Kind == AliasNode {
|
||||
mergeNodeSeq = mergeNodeSeq.Alias
|
||||
}
|
||||
if mergeNodeSeq.Kind != MappingNode {
|
||||
return fmt.Errorf("merge anchor only supports maps, got !!seq instead")
|
||||
}
|
||||
itemsToAdd := mergeNodeSeq.FilterMapContentByKey(func(keyNode *CandidateNode) bool {
|
||||
return getContentValueByKey(node.Content, keyNode.Value) == nil &&
|
||||
getContentValueByKey(newContent, keyNode.Value) == nil
|
||||
})
|
||||
newContent = append(newContent, itemsToAdd...)
|
||||
}
|
||||
}
|
||||
}
|
||||
node.Content = newContent
|
||||
return nil
|
||||
}
|
||||
|
||||
func reconstructAliasedMap(node *CandidateNode, context Context) error {
|
||||
var newContent = list.New()
|
||||
// can I short cut here by prechecking if there's an anchor in the map?
|
||||
@@ -215,6 +250,10 @@ func explodeNode(node *CandidateNode, context Context) error {
|
||||
}
|
||||
|
||||
if hasAlias {
|
||||
if ConfiguredYamlPreferences.FixMergeAnchorToSpec {
|
||||
return fixedReconstructAliasedMap(node)
|
||||
}
|
||||
log.Warning("--yaml-fix-merge-anchor-to-spec is false; causing merge anchors to override the existing values which isn't to the yaml spec. This flag will default to true in late 2025.")
|
||||
// this is a slow op, which is why we want to check before running it.
|
||||
return reconstructAliasedMap(node, context)
|
||||
}
|
||||
@@ -272,10 +311,7 @@ func overrideEntry(node *CandidateNode, key *CandidateNode, value *CandidateNode
|
||||
log.Debugf("checking new content %v:%v", keyNode.Value, valueEl.Value.(*CandidateNode).Value)
|
||||
if keyNode.Value == key.Value && keyNode.Alias == nil && key.Alias == nil {
|
||||
log.Debugf("overridign new content")
|
||||
if !ConfiguredYamlPreferences.FixMergeAnchorToSpec {
|
||||
log.Warning("--yaml-fix-merge-anchor-to-spec is false; causing the merge anchor to override the existing value at %v which isn't to the yaml spec. This flag will default to true in late 2025.", keyNode.GetNicePath())
|
||||
valueEl.Value = value
|
||||
}
|
||||
valueEl.Value = value
|
||||
return nil
|
||||
}
|
||||
newEl = valueEl // move forward twice
|
||||
|
||||
Reference in New Issue
Block a user