2019-11-23 03:52:29 +00:00
|
|
|
package yqlib
|
2015-10-01 23:05:13 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-25 01:11:04 +00:00
|
|
|
"fmt"
|
2015-10-01 23:05:13 +00:00
|
|
|
"strconv"
|
2017-09-20 23:40:33 +00:00
|
|
|
|
2019-12-06 04:57:46 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2015-10-01 23:05:13 +00:00
|
|
|
)
|
|
|
|
|
2019-12-01 19:44:44 +00:00
|
|
|
type DataNavigator interface {
|
2019-12-25 01:11:04 +00:00
|
|
|
Traverse(value *yaml.Node, path []string) error
|
2019-12-01 19:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type navigator struct {
|
2019-12-28 07:19:37 +00:00
|
|
|
navigationStrategy NavigationStrategy
|
2019-12-01 19:44:44 +00:00
|
|
|
}
|
|
|
|
|
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-01 19:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01 19:44:44 +00:00
|
|
|
}
|
2019-12-25 01:11:04 +00:00
|
|
|
return n.doTraverse(value, "", path, emptyArray)
|
2019-12-22 06:13:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 01:11:04 +00:00
|
|
|
func (n *navigator) doTraverse(value *yaml.Node, head string, path []string, pathStack []interface{}) error {
|
2019-12-06 04:57:46 +00:00
|
|
|
if len(path) > 0 {
|
2019-12-25 01:11:04 +00:00
|
|
|
log.Debugf("diving into %v", path[0])
|
|
|
|
DebugNode(value)
|
|
|
|
return n.recurse(value, path[0], path[1:], pathStack)
|
2019-12-09 02:44:53 +00:00
|
|
|
}
|
2019-12-29 22:21:21 +00:00
|
|
|
return n.navigationStrategy.Visit(NewNodeContext(value, head, path, 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-25 01:11:04 +00:00
|
|
|
log.Debug("its a sequence of %v things!, %v", len(value.Content))
|
2019-12-06 04:57:46 +00:00
|
|
|
if head == "*" {
|
2019-12-25 01:11:04 +00:00
|
|
|
return n.splatArray(value, tail, pathStack)
|
2019-12-06 04:57:46 +00:00
|
|
|
} else if head == "+" {
|
2019-12-25 01:11:04 +00:00
|
|
|
return n.appendArray(value, 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)
|
2019-12-29 22:21:21 +00:00
|
|
|
if n.navigationStrategy.FollowAlias(NewNodeContext(value, head, tail, pathStack)) == true {
|
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
|
|
|
}
|
|
|
|
}
|
2018-05-04 15:46:58 +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()
|
|
|
|
log.Debug("should I traverse? %v, %v", head, PathStackToString(newPathStack))
|
|
|
|
DebugNode(value)
|
|
|
|
if n.navigationStrategy.ShouldTraverse(NewNodeContext(contents[indexInMap+1], head, tail, newPathStack), contents[indexInMap].Value) == true {
|
|
|
|
log.Debug("recurseMap: Going to traverse")
|
2019-12-27 08:06:08 +00:00
|
|
|
traversedEntry = true
|
|
|
|
contents[indexInMap+1] = n.getOrReplace(contents[indexInMap+1], guessKind(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
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:21:21 +00:00
|
|
|
if traversedEntry == true || head == "*" || n.navigationStrategy.AutoCreateMap(NewNodeContext(value, head, tail, pathStack)) == false {
|
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-25 01:11:04 +00:00
|
|
|
mapEntryValue := yaml.Node{Kind: guessKind(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
|
|
|
|
2019-12-29 22:21:21 +00:00
|
|
|
if errorVisitedDirectEntries != nil || n.navigationStrategy.FollowAlias(NewNodeContext(node, head, tail, pathStack)) == false {
|
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-25 01:11:04 +00:00
|
|
|
func (n *navigator) splatArray(value *yaml.Node, 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)
|
|
|
|
head := fmt.Sprintf("%v", index)
|
|
|
|
childValue = n.getOrReplace(childValue, guessKind(tail, childValue.Kind))
|
|
|
|
var err = n.doTraverse(childValue, head, tail, append(pathStack, index))
|
2019-12-09 02:57:10 +00:00
|
|
|
if err != nil {
|
2019-12-15 07:24:23 +00:00
|
|
|
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-25 01:11:04 +00:00
|
|
|
func (n *navigator) appendArray(value *yaml.Node, tail []string, pathStack []interface{}) error {
|
|
|
|
var newNode = yaml.Node{Kind: guessKind(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)
|
|
|
|
head := fmt.Sprintf("%v", len(value.Content)-1)
|
|
|
|
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 {
|
2019-12-15 07:24:23 +00:00
|
|
|
return err
|
2019-12-09 02:57:10 +00:00
|
|
|
}
|
|
|
|
if index >= int64(len(value.Content)) {
|
2019-12-15 07:24:23 +00:00
|
|
|
return nil
|
2018-05-04 15:46:58 +00:00
|
|
|
}
|
2019-12-25 01:11:04 +00:00
|
|
|
value.Content[index] = n.getOrReplace(value.Content[index], guessKind(tail, value.Content[index].Kind))
|
2019-12-29 22:21:21 +00:00
|
|
|
|
|
|
|
// THERES SOMETHING WRONG HERE, ./yq read -p kv examples/sample.yaml b.e.1.*
|
|
|
|
// THERES SOMETHING WRONG HERE, ./yq read -p kv examples/sample.yaml b.e.1.name
|
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
|
|
|
}
|