yq/pkg/yqlib/data_navigator.go

296 lines
11 KiB
Go
Raw Normal View History

package yqlib
2015-10-01 23:05:13 +00:00
import (
2020-02-12 04:40:21 +00:00
"fmt"
2015-10-01 23:05:13 +00:00
"strconv"
2019-12-06 04:57:46 +00:00
yaml "gopkg.in/yaml.v3"
2015-10-01 23:05:13 +00:00
)
type DataNavigator interface {
2020-02-12 04:40:21 +00:00
Traverse(value *yaml.Node, path []interface{}) error
}
type navigator struct {
2019-12-28 07:19:37 +00:00
navigationStrategy NavigationStrategy
}
2019-12-28 07:19:37 +00:00
func NewDataNavigator(NavigationStrategy NavigationStrategy) DataNavigator {
2019-12-01 20:10:42 +00:00
return &navigator{
2019-12-28 07:19:37 +00:00
navigationStrategy: NavigationStrategy,
}
}
2020-02-12 04:40:21 +00:00
func (n *navigator) Traverse(value *yaml.Node, path []interface{}) error {
2019-12-22 06:13:11 +00:00
emptyArray := make([]interface{}, 0)
2020-06-10 23:40:59 +00:00
log.Debugf("Traversing path %v", pathStackToString(path))
2019-12-25 01:11:04 +00:00
return n.doTraverse(value, "", path, emptyArray)
2019-12-22 06:13:11 +00:00
}
2020-02-12 04:40:21 +00:00
func (n *navigator) doTraverse(value *yaml.Node, head interface{}, tail []interface{}, pathStack []interface{}) error {
2020-02-06 23:42:07 +00:00
2019-12-30 03:51:07 +00:00
log.Debug("head %v", head)
DebugNode(value)
2020-02-07 05:32:39 +00:00
var nodeContext = NewNodeContext(value, head, tail, pathStack)
2020-02-06 23:42:07 +00:00
2019-12-31 02:21:39 +00:00
var errorDeepSplatting error
2020-06-17 22:38:36 +00:00
// no need to deeply traverse the DocumentNode, as it's already covered by its first child.
if head == "**" && value.Kind != yaml.DocumentNode && value.Kind != yaml.ScalarNode && n.navigationStrategy.ShouldDeeplyTraverse(nodeContext) {
if len(pathStack) == 0 || pathStack[len(pathStack)-1] != "<<" {
errorDeepSplatting = n.recurse(value, head, tail, pathStack)
}
2019-12-31 02:21:39 +00:00
// ignore errors here, we are deep splatting so we may accidently give a string key
// to an array sequence
if len(tail) > 0 {
2020-01-09 10:27:52 +00:00
_ = n.recurse(value, tail[0], tail[1:], pathStack)
2019-12-31 02:21:39 +00:00
}
return errorDeepSplatting
2019-12-30 03:51:07 +00:00
}
2020-06-17 22:38:36 +00:00
if value.Kind == yaml.DocumentNode {
log.Debugf("its a document, diving into %v", head)
DebugNode(value)
return n.recurse(value, head, tail, pathStack)
} else if len(tail) > 0 && value.Kind != yaml.ScalarNode {
2019-12-30 03:51:07 +00:00
log.Debugf("diving into %v", tail[0])
2019-12-25 01:11:04 +00:00
DebugNode(value)
2019-12-30 03:51:07 +00:00
return n.recurse(value, tail[0], tail[1:], pathStack)
2019-12-09 02:44:53 +00:00
}
2020-02-07 05:32:39 +00:00
return n.navigationStrategy.Visit(nodeContext)
2015-10-01 23:05:13 +00:00
}
2019-12-06 04:57:46 +00:00
func (n *navigator) getOrReplace(original *yaml.Node, expectedKind yaml.Kind) *yaml.Node {
2019-12-09 02:57:10 +00:00
if original.Kind != expectedKind {
2020-02-13 07:08:13 +00:00
log.Debug("wanted %v but it was %v, overriding", KindString(expectedKind), KindString(original.Kind))
2019-12-06 04:57:46 +00:00
return &yaml.Node{Kind: expectedKind}
2017-02-26 22:01:52 +00:00
}
2019-12-06 04:57:46 +00:00
return original
2015-10-01 23:05:13 +00:00
}
2020-02-12 04:40:21 +00:00
func (n *navigator) recurse(value *yaml.Node, head interface{}, tail []interface{}, pathStack []interface{}) error {
2020-02-06 23:09:20 +00:00
log.Debug("recursing, processing %v, pathStack %v", head, pathStackToString(pathStack))
2020-06-10 23:40:59 +00:00
nodeContext := NewNodeContext(value, head, tail, pathStack)
if head == "**" && !n.navigationStrategy.ShouldOnlyDeeplyVisitLeaves(nodeContext) {
nodeContext.IsMiddleNode = true
2020-06-10 23:40:59 +00:00
errorVisitingDeeply := n.navigationStrategy.Visit(nodeContext)
if errorVisitingDeeply != nil {
return errorVisitingDeeply
}
}
2019-12-06 04:57:46 +00:00
switch value.Kind {
case yaml.MappingNode:
2019-12-25 01:11:04 +00:00
log.Debug("its a map with %v entries", len(value.Content)/2)
2020-02-12 04:40:21 +00:00
headString := fmt.Sprintf("%v", head)
return n.recurseMap(value, headString, tail, pathStack)
2019-12-06 04:57:46 +00:00
case yaml.SequenceNode:
2019-12-31 02:21:39 +00:00
log.Debug("its a sequence of %v things!", len(value.Content))
2020-02-12 04:40:21 +00:00
switch head := head.(type) {
case int64:
return n.recurseArray(value, head, head, tail, pathStack)
default:
2020-02-12 04:40:21 +00:00
if head == "+" {
return n.appendArray(value, head, tail, pathStack)
} else if len(value.Content) == 0 && head == "**" {
2020-06-10 23:40:59 +00:00
return n.navigationStrategy.Visit(nodeContext)
2020-02-12 04:40:21 +00:00
}
return n.splatArray(value, head, tail, pathStack)
}
2019-12-16 09:38:55 +00:00
case yaml.AliasNode:
2019-12-25 01:11:04 +00:00
log.Debug("its an alias!")
DebugNode(value.Alias)
2020-06-10 23:40:59 +00:00
if n.navigationStrategy.FollowAlias(nodeContext) {
2020-02-07 03:52:37 +00:00
log.Debug("following the alias")
return n.recurse(value.Alias, head, tail, pathStack)
2019-12-16 09:38:55 +00:00
}
return nil
2020-06-17 22:38:36 +00:00
case yaml.DocumentNode:
return n.doTraverse(value.Content[0], head, tail, pathStack)
2019-12-09 02:57:10 +00:00
default:
2020-06-10 23:40:59 +00:00
return n.navigationStrategy.Visit(nodeContext)
2019-12-09 02:57:10 +00:00
}
}
2020-02-12 04:40:21 +00:00
func (n *navigator) recurseMap(value *yaml.Node, head string, tail []interface{}, pathStack []interface{}) error {
2019-12-27 08:06:08 +00:00
traversedEntry := false
errorVisiting := n.visitMatchingEntries(value, head, tail, pathStack, func(contents []*yaml.Node, indexInMap int) error {
log.Debug("recurseMap: visitMatchingEntries for %v", contents[indexInMap].Value)
2019-12-29 22:21:21 +00:00
n.navigationStrategy.DebugVisitedNodes()
2019-12-27 21:51:54 +00:00
newPathStack := append(pathStack, contents[indexInMap].Value)
2020-01-11 07:52:15 +00:00
log.Debug("should I traverse? head: %v, path: %v", head, pathStackToString(newPathStack))
2019-12-29 22:21:21 +00:00
DebugNode(value)
2020-01-09 10:27:52 +00:00
if n.navigationStrategy.ShouldTraverse(NewNodeContext(contents[indexInMap+1], head, tail, newPathStack), contents[indexInMap].Value) {
2019-12-29 22:21:21 +00:00
log.Debug("recurseMap: Going to traverse")
2019-12-27 08:06:08 +00:00
traversedEntry = true
2020-02-13 07:08:13 +00:00
contents[indexInMap+1] = n.getOrReplace(contents[indexInMap+1], guessKind(head, tail, contents[indexInMap+1].Kind))
2019-12-29 22:21:21 +00:00
errorTraversing := n.doTraverse(contents[indexInMap+1], head, tail, newPathStack)
log.Debug("recurseMap: Finished traversing")
n.navigationStrategy.DebugVisitedNodes()
return errorTraversing
2019-12-27 08:06:08 +00:00
} else {
log.Debug("nope not traversing")
}
return nil
2019-12-15 07:24:23 +00:00
})
if errorVisiting != nil {
return errorVisiting
}
if len(value.Content) == 0 && head == "**" {
return n.navigationStrategy.Visit(NewNodeContext(value, head, tail, pathStack))
} else if traversedEntry || n.navigationStrategy.GetPathParser().IsPathExpression(head) || !n.navigationStrategy.AutoCreateMap(NewNodeContext(value, head, tail, pathStack)) {
2019-12-15 07:24:23 +00:00
return nil
2019-12-09 02:57:10 +00:00
}
2019-12-15 06:31:26 +00:00
2020-02-12 04:40:21 +00:00
_, errorParsingInt := strconv.ParseInt(head, 10, 64)
2019-12-22 06:13:11 +00:00
mapEntryKey := yaml.Node{Value: head, Kind: yaml.ScalarNode}
2020-02-12 04:40:21 +00:00
if errorParsingInt == nil {
// fixes a json encoding problem where keys that look like numbers
// get treated as numbers and cannot be used in a json map
mapEntryKey.Style = yaml.LiteralStyle
}
2019-12-22 06:13:11 +00:00
value.Content = append(value.Content, &mapEntryKey)
2019-12-31 02:21:39 +00:00
mapEntryValue := yaml.Node{Kind: guessKind(head, tail, 0)}
2019-12-09 02:57:10 +00:00
value.Content = append(value.Content, &mapEntryValue)
2020-02-12 04:40:21 +00:00
log.Debug("adding a new node %v - def a string", head)
2019-12-25 01:11:04 +00:00
return n.doTraverse(&mapEntryValue, head, tail, append(pathStack, head))
2019-12-09 02:57:10 +00:00
}
2019-12-16 10:09:23 +00:00
// need to pass the node in, as it may be aliased
2019-12-22 06:13:11 +00:00
type mapVisitorFn func(contents []*yaml.Node, index int) error
2019-12-15 07:24:23 +00:00
2020-02-12 04:40:21 +00:00
func (n *navigator) visitDirectMatchingEntries(node *yaml.Node, head string, tail []interface{}, pathStack []interface{}, visit mapVisitorFn) error {
2019-12-23 23:35:57 +00:00
var contents = node.Content
2019-12-22 04:15:15 +00:00
for index := 0; index < len(contents); index = index + 2 {
2019-12-15 07:38:40 +00:00
content := contents[index]
2019-12-29 22:21:21 +00:00
2019-12-25 01:11:04 +00:00
log.Debug("index %v, checking %v, %v", index, content.Value, content.Tag)
2019-12-29 22:21:21 +00:00
n.navigationStrategy.DebugVisitedNodes()
2019-12-27 08:06:08 +00:00
errorVisiting := visit(contents, index)
if errorVisiting != nil {
return errorVisiting
2019-12-22 04:15:15 +00:00
}
}
2019-12-27 08:06:08 +00:00
return nil
2019-12-22 04:33:54 +00:00
}
2019-12-22 04:15:15 +00:00
2020-02-12 04:40:21 +00:00
func (n *navigator) visitMatchingEntries(node *yaml.Node, head string, tail []interface{}, pathStack []interface{}, visit mapVisitorFn) error {
2019-12-23 23:35:57 +00:00
var contents = node.Content
2019-12-25 01:11:04 +00:00
log.Debug("visitMatchingEntries %v", head)
DebugNode(node)
2019-12-22 04:33:54 +00:00
// value.Content is a concatenated array of key, value,
// so keys are in the even indexes, values in odd.
// merge aliases are defined first, but we only want to traverse them
// if we don't find a match directly on this node first.
2019-12-27 08:06:08 +00:00
errorVisitedDirectEntries := n.visitDirectMatchingEntries(node, head, tail, pathStack, visit)
2019-12-22 22:25:44 +00:00
2020-01-09 10:27:52 +00:00
if errorVisitedDirectEntries != nil || !n.navigationStrategy.FollowAlias(NewNodeContext(node, head, tail, pathStack)) {
2019-12-27 08:06:08 +00:00
return errorVisitedDirectEntries
2019-12-22 04:33:54 +00:00
}
2019-12-23 23:35:57 +00:00
return n.visitAliases(contents, head, tail, pathStack, visit)
2019-12-22 04:33:54 +00:00
}
2020-02-12 04:40:21 +00:00
func (n *navigator) visitAliases(contents []*yaml.Node, head string, tail []interface{}, pathStack []interface{}, visit mapVisitorFn) error {
2019-12-22 04:15:15 +00:00
// merge aliases are defined first, but we only want to traverse them
// if we don't find a match on this node first.
// traverse them backwards so that the last alias overrides the preceding.
2019-12-22 04:33:54 +00:00
// a node can either be
// an alias to one other node (e.g. <<: *blah)
// or a sequence of aliases (e.g. <<: [*blah, *foo])
2020-02-06 22:08:52 +00:00
log.Debug("checking for aliases, head: %v, pathstack: %v", head, pathStackToString(pathStack))
2019-12-22 04:15:15 +00:00
for index := len(contents) - 2; index >= 0; index = index - 2 {
2020-02-06 22:08:52 +00:00
if contents[index+1].Kind == yaml.AliasNode && contents[index].Value == "<<" {
2019-12-16 10:09:23 +00:00
valueNode := contents[index+1]
2019-12-25 01:11:04 +00:00
log.Debug("found an alias")
DebugNode(contents[index])
DebugNode(valueNode)
2019-12-22 04:33:54 +00:00
2019-12-27 08:06:08 +00:00
errorInAlias := n.visitMatchingEntries(valueNode.Alias, head, tail, pathStack, visit)
if errorInAlias != nil {
return errorInAlias
2019-12-22 04:15:15 +00:00
}
} else if contents[index+1].Kind == yaml.SequenceNode {
2019-12-22 04:35:16 +00:00
// could be an array of aliases...
2019-12-27 08:06:08 +00:00
errorVisitingAliasSeq := n.visitAliasSequence(contents[index+1].Content, head, tail, pathStack, visit)
if errorVisitingAliasSeq != nil {
return errorVisitingAliasSeq
2019-12-15 07:24:23 +00:00
}
2019-12-15 06:31:26 +00:00
}
2019-12-22 04:33:54 +00:00
}
2019-12-27 08:06:08 +00:00
return nil
2019-12-22 04:33:54 +00:00
}
2019-12-22 04:15:15 +00:00
2020-02-12 04:40:21 +00:00
func (n *navigator) visitAliasSequence(possibleAliasArray []*yaml.Node, head string, tail []interface{}, pathStack []interface{}, visit mapVisitorFn) error {
2019-12-22 04:35:16 +00:00
// need to search this backwards too, so that aliases defined last override the preceding.
2019-12-22 04:33:54 +00:00
for aliasIndex := len(possibleAliasArray) - 1; aliasIndex >= 0; aliasIndex = aliasIndex - 1 {
child := possibleAliasArray[aliasIndex]
if child.Kind == yaml.AliasNode {
2019-12-25 01:11:04 +00:00
log.Debug("found an alias")
DebugNode(child)
2019-12-27 08:06:08 +00:00
errorInAlias := n.visitMatchingEntries(child.Alias, head, tail, pathStack, visit)
if errorInAlias != nil {
return errorInAlias
2019-12-22 04:33:54 +00:00
}
}
2019-12-15 06:31:26 +00:00
}
2019-12-27 08:06:08 +00:00
return nil
2019-12-15 06:31:26 +00:00
}
2020-02-12 04:40:21 +00:00
func (n *navigator) splatArray(value *yaml.Node, head interface{}, tail []interface{}, pathStack []interface{}) error {
2019-12-22 06:13:11 +00:00
for index, childValue := range value.Content {
2019-12-25 01:11:04 +00:00
log.Debug("processing")
DebugNode(childValue)
2019-12-31 02:21:39 +00:00
childValue = n.getOrReplace(childValue, guessKind(head, tail, childValue.Kind))
2020-01-11 07:52:15 +00:00
newPathStack := append(pathStack, index)
if n.navigationStrategy.ShouldTraverse(NewNodeContext(childValue, head, tail, newPathStack), childValue.Value) {
2020-02-07 05:32:39 +00:00
// here we should not deeply traverse the array if we are appending..not sure how to do that.
// need to visit instead...
// easiest way is to pop off the head and pass the rest of the tail in.
2020-01-11 07:52:15 +00:00
var err = n.doTraverse(childValue, head, tail, newPathStack)
if err != nil {
return err
}
2019-12-09 02:57:10 +00:00
}
}
2019-12-15 07:24:23 +00:00
return nil
2019-12-09 02:57:10 +00:00
}
2020-02-12 04:40:21 +00:00
func (n *navigator) appendArray(value *yaml.Node, head interface{}, tail []interface{}, pathStack []interface{}) error {
2019-12-31 02:21:39 +00:00
var newNode = yaml.Node{Kind: guessKind(head, tail, 0)}
2019-12-09 02:57:10 +00:00
value.Content = append(value.Content, &newNode)
2019-12-25 01:11:04 +00:00
log.Debug("appending a new node, %v", value.Content)
return n.doTraverse(&newNode, head, tail, append(pathStack, len(value.Content)-1))
2019-12-09 02:57:10 +00:00
}
2020-02-12 04:40:21 +00:00
func (n *navigator) recurseArray(value *yaml.Node, index int64, head interface{}, tail []interface{}, pathStack []interface{}) error {
2020-04-13 00:36:46 +00:00
var contentLength = int64(len(value.Content))
for contentLength <= index {
2020-01-05 21:12:30 +00:00
value.Content = append(value.Content, &yaml.Node{Kind: guessKind(head, tail, 0)})
2020-04-14 01:17:29 +00:00
contentLength = int64(len(value.Content))
2020-01-05 21:12:30 +00:00
}
2020-04-13 00:36:46 +00:00
var indexToUse = index
2020-01-05 21:12:30 +00:00
2020-04-13 00:36:46 +00:00
if indexToUse < 0 {
indexToUse = contentLength + indexToUse
}
if indexToUse < 0 {
return fmt.Errorf("Index [%v] out of range, array size is %v", index, contentLength)
}
value.Content[indexToUse] = n.getOrReplace(value.Content[indexToUse], guessKind(head, tail, value.Content[indexToUse].Kind))
2019-12-29 22:21:21 +00:00
2020-04-13 00:36:46 +00:00
return n.doTraverse(value.Content[indexToUse], head, tail, append(pathStack, index))
2019-05-14 01:20:41 +00:00
}