Refactoring

This commit is contained in:
Mike Farah 2019-12-09 13:57:10 +11:00
parent 9771e7001c
commit 586ffb833b
2 changed files with 84 additions and 69 deletions

View File

@ -1,8 +1,9 @@
a: Easy! as one two three a: Easy! as one two three
b: b:
c: c: 2
name: c1 d: [3, 4]
f: things e:
d: - name: fred
name: d1 value: 3
f: other - name: sam
value: 4

View File

@ -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 { func (n *navigator) getOrReplace(original *yaml.Node, expectedKind yaml.Kind) *yaml.Node {
// expected is a scalar when we reach the end of the path if original.Kind != expectedKind {
// 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) {
n.log.Debug("wanted %v but it was %v, overriding", expectedKind, original.Kind) n.log.Debug("wanted %v but it was %v, overriding", expectedKind, original.Kind)
return &yaml.Node{Kind: expectedKind} return &yaml.Node{Kind: expectedKind}
} }
@ -114,6 +110,23 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visito
case yaml.MappingNode: case yaml.MappingNode:
n.log.Debug("its a map with %v entries", len(value.Content)/2) n.log.Debug("its a map with %v entries", len(value.Content)/2)
if head == "*" { 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} var newNode = yaml.Node{Kind: yaml.SequenceNode}
for index, content := range value.Content { for index, content := range value.Content {
if index%2 == 0 { if index%2 == 0 {
@ -129,6 +142,7 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visito
return &newNode, nil 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 { for index, content := range value.Content {
// value.Content is a concatenated array of key, value, // value.Content is a concatenated array of key, value,
// so keys are in the even indexes, values in odd. // 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) value.Content = append(value.Content, &mapEntryValue)
n.log.Debug("adding new node %v", value.Content) n.log.Debug("adding new node %v", value.Content)
return n.Visit(&mapEntryValue, tail, visitor) 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} var newNode = yaml.Node{Kind: yaml.SequenceNode, Style: value.Style}
newNode.Content = make([]*yaml.Node, len(value.Content)) 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 newNode.Content[index] = nestedValue
} }
return &newNode, nil 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)} var newNode = yaml.Node{Kind: n.guessKind(tail, 0)}
value.Content = append(value.Content, &newNode) value.Content = append(value.Content, &newNode)
n.log.Debug("appending a new node, %v", value.Content) n.log.Debug("appending a new node, %v", value.Content)
return n.Visit(&newNode, tail, visitor) 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 var index, err = strconv.ParseInt(head, 10, 64) // nolint
if err != nil { if err != nil {
return nil, err 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)) value.Content[index] = n.getOrReplace(value.Content[index], n.guessKind(tail, value.Content[index].Kind))
return n.Visit(value.Content[index], tail, visitor) return n.Visit(value.Content[index], tail, visitor)
default:
return nil, nil
}
} }
// func matchesKey(key string, actual interface{}) bool { // func matchesKey(key string, actual interface{}) bool {