mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-06 21:55:37 +00:00
Sped up explode operator
This commit is contained in:
parent
839f795710
commit
4a4aae00af
@ -141,31 +141,11 @@ func explodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expr
|
|||||||
return context, nil
|
return context, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func explodeNode(node *yaml.Node, context Context) error {
|
func reconstructAliasedMap(node *yaml.Node, context Context) error {
|
||||||
node.Anchor = ""
|
|
||||||
switch node.Kind {
|
|
||||||
case yaml.SequenceNode, yaml.DocumentNode:
|
|
||||||
for index, contentNode := range node.Content {
|
|
||||||
log.Debugf("exploding index %v", index)
|
|
||||||
errorInContent := explodeNode(contentNode, context)
|
|
||||||
if errorInContent != nil {
|
|
||||||
return errorInContent
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
case yaml.AliasNode:
|
|
||||||
log.Debugf("its an alias!")
|
|
||||||
if node.Alias != nil {
|
|
||||||
node.Kind = node.Alias.Kind
|
|
||||||
node.Style = node.Alias.Style
|
|
||||||
node.Tag = node.Alias.Tag
|
|
||||||
node.Content = node.Alias.Content
|
|
||||||
node.Value = node.Alias.Value
|
|
||||||
node.Alias = nil
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
case yaml.MappingNode:
|
|
||||||
var newContent = list.New()
|
var newContent = list.New()
|
||||||
|
// can I short cut here by prechecking if there's an anchor in the map?
|
||||||
|
// no it needs to recurse in overrideEntry.
|
||||||
|
|
||||||
for index := 0; index < len(node.Content); index = index + 2 {
|
for index := 0; index < len(node.Content); index = index + 2 {
|
||||||
keyNode := node.Content[index]
|
keyNode := node.Content[index]
|
||||||
valueNode := node.Content[index+1]
|
valueNode := node.Content[index+1]
|
||||||
@ -200,8 +180,62 @@ func explodeNode(node *yaml.Node, context Context) error {
|
|||||||
node.Content[index] = newEl.Value.(*yaml.Node)
|
node.Content[index] = newEl.Value.(*yaml.Node)
|
||||||
index++
|
index++
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func explodeNode(node *yaml.Node, context Context) error {
|
||||||
|
node.Anchor = ""
|
||||||
|
switch node.Kind {
|
||||||
|
case yaml.SequenceNode, yaml.DocumentNode:
|
||||||
|
for index, contentNode := range node.Content {
|
||||||
|
log.Debugf("exploding index %v", index)
|
||||||
|
errorInContent := explodeNode(contentNode, context)
|
||||||
|
if errorInContent != nil {
|
||||||
|
return errorInContent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case yaml.AliasNode:
|
||||||
|
log.Debugf("its an alias!")
|
||||||
|
if node.Alias != nil {
|
||||||
|
node.Kind = node.Alias.Kind
|
||||||
|
node.Style = node.Alias.Style
|
||||||
|
node.Tag = node.Alias.Tag
|
||||||
|
node.Content = node.Alias.Content
|
||||||
|
node.Value = node.Alias.Value
|
||||||
|
node.Alias = nil
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case yaml.MappingNode:
|
||||||
|
// //check the map has an alias in it
|
||||||
|
hasAlias := false
|
||||||
|
for index := 0; index < len(node.Content); index = index + 2 {
|
||||||
|
keyNode := node.Content[index]
|
||||||
|
if keyNode.Value == "<<" {
|
||||||
|
hasAlias = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasAlias {
|
||||||
|
// this is a slow op, which is why we want to check before running it.
|
||||||
|
return reconstructAliasedMap(node, context)
|
||||||
|
} else {
|
||||||
|
// this map has no aliases, but it's kids might
|
||||||
|
for index := 0; index < len(node.Content); index = index + 2 {
|
||||||
|
keyNode := node.Content[index]
|
||||||
|
valueNode := node.Content[index+1]
|
||||||
|
err := explodeNode(keyNode, context)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = explodeNode(valueNode, context)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user