mirror of
https://github.com/mikefarah/yq.git
synced 2025-03-16 17:15:36 +00:00
recurse
This commit is contained in:
parent
dad61ec615
commit
d97f1d8be2
@ -1,6 +1,7 @@
|
|||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
logging "gopkg.in/op/go-logging.v1"
|
logging "gopkg.in/op/go-logging.v1"
|
||||||
@ -29,6 +30,7 @@ func (n *navigator) Get(value *yaml.Node, path []string) (*yaml.Node, error) {
|
|||||||
}
|
}
|
||||||
if len(path) > 0 {
|
if len(path) > 0 {
|
||||||
n.log.Debugf("diving into %v", path[0])
|
n.log.Debugf("diving into %v", path[0])
|
||||||
|
n.debugNode(value)
|
||||||
return n.recurse(realValue, path[0], path[1:])
|
return n.recurse(realValue, path[0], path[1:])
|
||||||
}
|
}
|
||||||
return realValue, nil
|
return realValue, nil
|
||||||
@ -37,7 +39,7 @@ func (n *navigator) Get(value *yaml.Node, path []string) (*yaml.Node, error) {
|
|||||||
func (n *navigator) guessKind(tail []string) yaml.Kind {
|
func (n *navigator) guessKind(tail []string) yaml.Kind {
|
||||||
n.log.Debug("tail %v", tail)
|
n.log.Debug("tail %v", tail)
|
||||||
if len(tail) == 0 {
|
if len(tail) == 0 {
|
||||||
n.log.Debug("scalar")
|
n.log.Debug("end of path, must be a scalar")
|
||||||
return yaml.ScalarNode
|
return yaml.ScalarNode
|
||||||
}
|
}
|
||||||
var _, errorParsingInt = strconv.ParseInt(tail[0], 10, 64)
|
var _, errorParsingInt = strconv.ParseInt(tail[0], 10, 64)
|
||||||
@ -53,11 +55,23 @@ func (n *navigator) getOrReplace(original *yaml.Node, expectedKind yaml.Kind) *y
|
|||||||
// when reading, it should deal with the original kind
|
// when reading, it should deal with the original kind
|
||||||
// when writing, it will clobber the kind anyway
|
// when writing, it will clobber the kind anyway
|
||||||
if original.Kind != expectedKind && (expectedKind != yaml.ScalarNode) {
|
if original.Kind != expectedKind && (expectedKind != yaml.ScalarNode) {
|
||||||
|
n.log.Debug("wanted %v but it was %v, overriding", expectedKind, original.Kind)
|
||||||
return &yaml.Node{Kind: expectedKind}
|
return &yaml.Node{Kind: expectedKind}
|
||||||
}
|
}
|
||||||
return original
|
return original
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *navigator) debugNode(value *yaml.Node) {
|
||||||
|
if n.log.IsEnabledFor(logging.DEBUG) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
encoder := yaml.NewEncoder(buf)
|
||||||
|
encoder.Encode(value)
|
||||||
|
encoder.Close()
|
||||||
|
n.log.Debug("Tag: %v", value.Tag)
|
||||||
|
n.log.Debug("%v", buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (n *navigator) recurse(value *yaml.Node, head string, tail []string) (*yaml.Node, error) {
|
func (n *navigator) recurse(value *yaml.Node, head string, tail []string) (*yaml.Node, error) {
|
||||||
switch value.Kind {
|
switch value.Kind {
|
||||||
case yaml.MappingNode:
|
case yaml.MappingNode:
|
||||||
@ -77,20 +91,24 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string) (*yaml
|
|||||||
n.log.Debug("adding new node %v", value.Content)
|
n.log.Debug("adding new node %v", value.Content)
|
||||||
return n.Get(&mapEntryValue, tail)
|
return n.Get(&mapEntryValue, tail)
|
||||||
case yaml.SequenceNode:
|
case yaml.SequenceNode:
|
||||||
n.log.Debug("its a sequence of %v things!", len(value.Content))
|
n.log.Debug("its a sequence of %v things!, %v", len(value.Content))
|
||||||
if head == "*" {
|
if head == "*" {
|
||||||
var newNode = yaml.Node{Kind: yaml.SequenceNode, Style: value.Style}
|
originalContent := value.Content
|
||||||
newNode.Content = make([]*yaml.Node, len(value.Content))
|
value.Content = make([]*yaml.Node, len(value.Content))
|
||||||
|
|
||||||
for index, value := range value.Content {
|
for index, childValue := range originalContent {
|
||||||
value.Content[index] = n.getOrReplace(value.Content[index], n.guessKind(tail))
|
n.log.Debug("processing")
|
||||||
var nestedValue, err = n.Get(value.Content[index], tail)
|
n.debugNode(childValue)
|
||||||
|
childValue = n.getOrReplace(childValue, n.guessKind(tail))
|
||||||
|
var nestedValue, err = n.Get(childValue, tail)
|
||||||
|
n.log.Debug("nestedValue")
|
||||||
|
n.debugNode(nestedValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
newNode.Content[index] = nestedValue
|
value.Content[index] = nestedValue
|
||||||
}
|
}
|
||||||
return &newNode, nil
|
return value, nil
|
||||||
} else if head == "+" {
|
} else if head == "+" {
|
||||||
|
|
||||||
var newNode = yaml.Node{Kind: n.guessKind(tail)}
|
var newNode = yaml.Node{Kind: n.guessKind(tail)}
|
||||||
|
Loading…
Reference in New Issue
Block a user