mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Refactoring
This commit is contained in:
parent
9771e7001c
commit
586ffb833b
@ -1,8 +1,9 @@
|
||||
a: Easy! as one two three
|
||||
b:
|
||||
c:
|
||||
name: c1
|
||||
f: things
|
||||
d:
|
||||
name: d1
|
||||
f: other
|
||||
c: 2
|
||||
d: [3, 4]
|
||||
e:
|
||||
- name: fred
|
||||
value: 3
|
||||
- name: sam
|
||||
value: 4
|
@ -87,11 +87,7 @@ func (n *navigator) guessKind(tail []string, guess yaml.Kind) yaml.Kind {
|
||||
}
|
||||
|
||||
func (n *navigator) getOrReplace(original *yaml.Node, expectedKind yaml.Kind) *yaml.Node {
|
||||
// expected is a scalar when we reach the end of the path
|
||||
// no need to clobber the original because:
|
||||
// when reading, it should deal with the original kind
|
||||
// when writing, it will clobber the kind anyway
|
||||
if original.Kind != expectedKind && (expectedKind != yaml.ScalarNode) {
|
||||
if original.Kind != expectedKind {
|
||||
n.log.Debug("wanted %v but it was %v, overriding", expectedKind, original.Kind)
|
||||
return &yaml.Node{Kind: expectedKind}
|
||||
}
|
||||
@ -114,6 +110,23 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visito
|
||||
case yaml.MappingNode:
|
||||
n.log.Debug("its a map with %v entries", len(value.Content)/2)
|
||||
if head == "*" {
|
||||
return n.splatMap(value, tail, visitor)
|
||||
}
|
||||
return n.recurseMap(value, head, tail, visitor)
|
||||
case yaml.SequenceNode:
|
||||
n.log.Debug("its a sequence of %v things!, %v", len(value.Content))
|
||||
if head == "*" {
|
||||
return n.splatArray(value, tail, visitor)
|
||||
} else if head == "+" {
|
||||
return n.appendArray(value, tail, visitor)
|
||||
}
|
||||
return n.recurseArray(value, head, tail, visitor)
|
||||
default:
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (n *navigator) splatMap(value *yaml.Node, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
||||
var newNode = yaml.Node{Kind: yaml.SequenceNode}
|
||||
for index, content := range value.Content {
|
||||
if index%2 == 0 {
|
||||
@ -127,8 +140,9 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visito
|
||||
newNode.Content = append(newNode.Content, nestedValue)
|
||||
}
|
||||
return &newNode, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
||||
for index, content := range value.Content {
|
||||
// value.Content is a concatenated array of key, value,
|
||||
// so keys are in the even indexes, values in odd.
|
||||
@ -143,9 +157,9 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visito
|
||||
value.Content = append(value.Content, &mapEntryValue)
|
||||
n.log.Debug("adding new node %v", value.Content)
|
||||
return n.Visit(&mapEntryValue, tail, visitor)
|
||||
case yaml.SequenceNode:
|
||||
n.log.Debug("its a sequence of %v things!, %v", len(value.Content))
|
||||
if head == "*" {
|
||||
}
|
||||
|
||||
func (n *navigator) splatArray(value *yaml.Node, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
||||
var newNode = yaml.Node{Kind: yaml.SequenceNode, Style: value.Style}
|
||||
newNode.Content = make([]*yaml.Node, len(value.Content))
|
||||
|
||||
@ -162,13 +176,16 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visito
|
||||
newNode.Content[index] = nestedValue
|
||||
}
|
||||
return &newNode, nil
|
||||
} else if head == "+" {
|
||||
}
|
||||
|
||||
func (n *navigator) appendArray(value *yaml.Node, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
||||
var newNode = yaml.Node{Kind: n.guessKind(tail, 0)}
|
||||
value.Content = append(value.Content, &newNode)
|
||||
n.log.Debug("appending a new node, %v", value.Content)
|
||||
return n.Visit(&newNode, tail, visitor)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *navigator) recurseArray(value *yaml.Node, head string, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
||||
var index, err = strconv.ParseInt(head, 10, 64) // nolint
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -178,9 +195,6 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visito
|
||||
}
|
||||
value.Content[index] = n.getOrReplace(value.Content[index], n.guessKind(tail, value.Content[index].Kind))
|
||||
return n.Visit(value.Content[index], tail, visitor)
|
||||
default:
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
// func matchesKey(key string, actual interface{}) bool {
|
||||
|
Loading…
x
Reference in New Issue
Block a user