yq/pkg/yqlib/data_navigator.go

250 lines
9.1 KiB
Go
Raw Normal View History

package yqlib
2015-10-01 23:05:13 +00:00
import (
"strconv"
2019-12-31 02:21:39 +00:00
errors "github.com/pkg/errors"
2019-12-06 04:57:46 +00:00
yaml "gopkg.in/yaml.v3"
2015-10-01 23:05:13 +00:00
)
type DataNavigator interface {
2019-12-25 01:11:04 +00:00
Traverse(value *yaml.Node, path []string) 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,
}
}
2019-12-25 01:11:04 +00:00
func (n *navigator) Traverse(value *yaml.Node, path []string) error {
2019-12-06 04:57:46 +00:00
realValue := value
2019-12-22 06:13:11 +00:00
emptyArray := make([]interface{}, 0)
2019-12-06 04:57:46 +00:00
if realValue.Kind == yaml.DocumentNode {
2019-12-25 01:11:04 +00:00
log.Debugf("its a document! returning the first child")
return n.doTraverse(value.Content[0], "", path, emptyArray)
}
2019-12-25 01:11:04 +00:00
return n.doTraverse(value, "", path, emptyArray)
2019-12-22 06:13:11 +00:00
}
2019-12-30 03:51:07 +00:00
func (n *navigator) doTraverse(value *yaml.Node, head string, tail []string, pathStack []interface{}) error {
log.Debug("head %v", head)
DebugNode(value)
2019-12-31 02:21:39 +00:00
var errorDeepSplatting error
2019-12-30 03:51:07 +00:00
if head == "**" && value.Kind != yaml.ScalarNode {
2019-12-31 02:21:39 +00:00
errorDeepSplatting = n.recurse(value, head, tail, pathStack)
// 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
}
if len(tail) > 0 {
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
}
2019-12-30 03:51:07 +00:00
return n.navigationStrategy.Visit(NewNodeContext(value, head, tail, pathStack))
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 {
2019-12-25 01:11:04 +00:00
log.Debug("wanted %v but it was %v, overriding", expectedKind, 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
}
2019-12-25 01:11:04 +00:00
func (n *navigator) recurse(value *yaml.Node, head string, tail []string, pathStack []interface{}) error {
log.Debug("recursing, processing %v", head)
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)
return n.recurseMap(value, head, 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))
if n.navigationStrategy.GetPathParser().IsPathExpression(head) {
2019-12-30 03:51:07 +00:00
return n.splatArray(value, head, tail, pathStack)
2019-12-06 04:57:46 +00:00
} else if head == "+" {
2019-12-31 02:21:39 +00:00
return n.appendArray(value, head, tail, pathStack)
2019-12-09 02:57:10 +00:00
}
2019-12-25 01:11:04 +00:00
return n.recurseArray(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-01-09 10:27:52 +00:00
if n.navigationStrategy.FollowAlias(NewNodeContext(value, head, tail, pathStack)) {
2019-12-25 01:11:04 +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
2019-12-09 02:57:10 +00:00
default:
2019-12-15 07:24:23 +00:00
return nil
2019-12-09 02:57:10 +00:00
}
}
2019-12-25 01:11:04 +00:00
func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, 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 {
2019-12-29 22:21:21 +00:00
log.Debug("recurseMap: visitMatchingEntries")
n.navigationStrategy.DebugVisitedNodes()
2019-12-27 21:51:54 +00:00
newPathStack := append(pathStack, contents[indexInMap].Value)
2019-12-29 22:21:21 +00:00
log.Debug("appended %v", contents[indexInMap].Value)
n.navigationStrategy.DebugVisitedNodes()
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
2019-12-31 02:21:39 +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 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
2019-12-22 06:13:11 +00:00
mapEntryKey := yaml.Node{Value: head, Kind: yaml.ScalarNode}
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)
2019-12-27 08:06:08 +00:00
log.Debug("adding new node %v", 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
2019-12-27 08:06:08 +00:00
func (n *navigator) visitDirectMatchingEntries(node *yaml.Node, head string, tail []string, 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
2019-12-27 08:06:08 +00:00
func (n *navigator) visitMatchingEntries(node *yaml.Node, head string, tail []string, 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
}
2019-12-27 08:06:08 +00:00
func (n *navigator) visitAliases(contents []*yaml.Node, head string, tail []string, 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])
2019-12-25 01:11:04 +00:00
log.Debug("checking for aliases")
2019-12-22 04:15:15 +00:00
for index := len(contents) - 2; index >= 0; index = index - 2 {
if contents[index+1].Kind == yaml.AliasNode {
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
2019-12-27 08:06:08 +00:00
func (n *navigator) visitAliasSequence(possibleAliasArray []*yaml.Node, head string, tail []string, 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
}
2019-12-30 03:51:07 +00:00
func (n *navigator) splatArray(value *yaml.Node, head string, tail []string, 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) {
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
}
2019-12-31 02:21:39 +00:00
func (n *navigator) appendArray(value *yaml.Node, head string, tail []string, pathStack []interface{}) error {
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
}
2019-12-25 01:11:04 +00:00
func (n *navigator) recurseArray(value *yaml.Node, head string, tail []string, pathStack []interface{}) error {
2019-12-09 02:57:10 +00:00
var index, err = strconv.ParseInt(head, 10, 64) // nolint
if err != nil {
2020-01-09 10:18:24 +00:00
return errors.Wrapf(err, "Error parsing array index '%v' for '%v'", head, pathStackToString(pathStack))
2019-12-09 02:57:10 +00:00
}
2020-01-05 21:12:30 +00:00
for int64(len(value.Content)) <= index {
value.Content = append(value.Content, &yaml.Node{Kind: guessKind(head, tail, 0)})
}
2019-12-31 02:21:39 +00:00
value.Content[index] = n.getOrReplace(value.Content[index], guessKind(head, tail, value.Content[index].Kind))
2019-12-29 22:21:21 +00:00
2019-12-25 01:11:04 +00:00
return n.doTraverse(value.Content[index], head, tail, append(pathStack, index))
2019-05-14 01:20:41 +00:00
}