Refactor wip

This commit is contained in:
Mike Farah 2019-12-27 19:06:08 +11:00
parent ff5b23251b
commit cf389bed4a
9 changed files with 125 additions and 107 deletions

View File

@ -154,9 +154,10 @@ func TestReadMergeAnchorsPrefixMatchCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `foobar.thing: ice
foobar.thirty: well beyond
foobar.thirsty: yep`
expectedOutput := `foobar.thirty: well beyond
foobar.thing: ice
foobar.thirsty: yep
`
test.AssertResult(t, expectedOutput, result.Output)
}

View File

@ -14,6 +14,6 @@ foobarList:
foobar:
<<: *foo
thing: ice
thirty: well beyond
thing: ice
c: 3

View File

@ -3,6 +3,7 @@ b:
c: things
d: whatever
things:
borg: snorg
thing1:
cat: 'fred'
thing2:

View File

@ -37,6 +37,7 @@ func (n *navigator) doTraverse(value *yaml.Node, head string, path []string, pat
DebugNode(value)
return n.recurse(value, path[0], path[1:], pathStack)
}
log.Debug("should I visit?")
return n.navigationSettings.Visit(value, head, path, pathStack)
}
@ -76,16 +77,27 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, pathSt
}
func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, pathStack []interface{}) error {
visited, errorVisiting := n.visitMatchingEntries(value, head, tail, pathStack, func(contents []*yaml.Node, indexInMap int) error {
traversedEntry := false
errorVisiting := n.visitMatchingEntries(value, head, tail, pathStack, func(contents []*yaml.Node, indexInMap int) error {
log.Debug("should I traverse? %v", head)
DebugNode(value)
if n.navigationSettings.ShouldTraverse(contents[indexInMap+1], head, tail, append(pathStack, contents[indexInMap].Value)) == true {
log.Debug("yep!")
traversedEntry = true
contents[indexInMap+1] = n.getOrReplace(contents[indexInMap+1], guessKind(tail, contents[indexInMap+1].Kind))
return n.doTraverse(contents[indexInMap+1], head, tail, append(pathStack, contents[indexInMap].Value))
} else {
log.Debug("nope not traversing")
}
return nil
})
if errorVisiting != nil {
return errorVisiting
}
if visited || head == "*" || n.navigationSettings.AutoCreateMap(value, head, tail, pathStack) == false {
if traversedEntry == true || head == "*" || n.navigationSettings.AutoCreateMap(value, head, tail, pathStack) == false {
return nil
}
@ -93,33 +105,27 @@ func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, pat
value.Content = append(value.Content, &mapEntryKey)
mapEntryValue := yaml.Node{Kind: guessKind(tail, 0)}
value.Content = append(value.Content, &mapEntryValue)
log.Debug("adding new node %v", value.Content)
log.Debug("adding new node %v", head)
return n.doTraverse(&mapEntryValue, head, tail, append(pathStack, head))
}
// need to pass the node in, as it may be aliased
type mapVisitorFn func(contents []*yaml.Node, index int) error
func (n *navigator) visitDirectMatchingEntries(node *yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) (bool, error) {
func (n *navigator) visitDirectMatchingEntries(node *yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) error {
var contents = node.Content
visited := false
for index := 0; index < len(contents); index = index + 2 {
content := contents[index]
log.Debug("index %v, checking %v, %v", index, content.Value, content.Tag)
if n.navigationSettings.ShouldVisit(content, head, tail, pathStack) == true {
log.Debug("found a match! %v", content.Value)
errorVisiting := visit(contents, index)
if errorVisiting != nil {
return visited, errorVisiting
}
visited = true
return errorVisiting
}
}
return visited, nil
return nil
}
func (n *navigator) visitMatchingEntries(node *yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) (bool, error) {
func (n *navigator) visitMatchingEntries(node *yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) error {
var contents = node.Content
log.Debug("visitMatchingEntries %v", head)
DebugNode(node)
@ -127,20 +133,15 @@ func (n *navigator) visitMatchingEntries(node *yaml.Node, head string, tail []st
// 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.
visited, errorVisitedDirectEntries := n.visitDirectMatchingEntries(node, head, tail, pathStack, visit)
errorVisitedDirectEntries := n.visitDirectMatchingEntries(node, head, tail, pathStack, visit)
//TODO: crap we have to remember what we visited so we dont print the same key in the alias
// eff
if errorVisitedDirectEntries != nil || visited == true || n.navigationSettings.FollowAlias(node, head, tail, pathStack) == false {
return visited, errorVisitedDirectEntries
if errorVisitedDirectEntries != nil || n.navigationSettings.FollowAlias(node, head, tail, pathStack) == false {
return errorVisitedDirectEntries
}
// didnt find a match, lets check the aliases.
return n.visitAliases(contents, head, tail, pathStack, visit)
}
func (n *navigator) visitAliases(contents []*yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) (bool, error) {
func (n *navigator) visitAliases(contents []*yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) error {
// 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.
@ -156,36 +157,35 @@ func (n *navigator) visitAliases(contents []*yaml.Node, head string, tail []stri
DebugNode(contents[index])
DebugNode(valueNode)
visitedAlias, errorInAlias := n.visitMatchingEntries(valueNode.Alias, head, tail, pathStack, visit)
if visitedAlias == true || errorInAlias != nil {
return visitedAlias, errorInAlias
errorInAlias := n.visitMatchingEntries(valueNode.Alias, head, tail, pathStack, visit)
if errorInAlias != nil {
return errorInAlias
}
} else if contents[index+1].Kind == yaml.SequenceNode {
// could be an array of aliases...
visitedAliasSeq, errorVisitingAliasSeq := n.visitAliasSequence(contents[index+1].Content, head, tail, pathStack, visit)
if visitedAliasSeq == true || errorVisitingAliasSeq != nil {
return visitedAliasSeq, errorVisitingAliasSeq
errorVisitingAliasSeq := n.visitAliasSequence(contents[index+1].Content, head, tail, pathStack, visit)
if errorVisitingAliasSeq != nil {
return errorVisitingAliasSeq
}
}
}
log.Debug("nope no matching aliases found")
return false, nil
return nil
}
func (n *navigator) visitAliasSequence(possibleAliasArray []*yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) (bool, error) {
func (n *navigator) visitAliasSequence(possibleAliasArray []*yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) error {
// need to search this backwards too, so that aliases defined last override the preceding.
for aliasIndex := len(possibleAliasArray) - 1; aliasIndex >= 0; aliasIndex = aliasIndex - 1 {
child := possibleAliasArray[aliasIndex]
if child.Kind == yaml.AliasNode {
log.Debug("found an alias")
DebugNode(child)
visitedAlias, errorInAlias := n.visitMatchingEntries(child.Alias, head, tail, pathStack, visit)
if visitedAlias == true || errorInAlias != nil {
return visitedAlias, errorInAlias
errorInAlias := n.visitMatchingEntries(child.Alias, head, tail, pathStack, visit)
if errorInAlias != nil {
return errorInAlias
}
}
}
return false, nil
return nil
}
func (n *navigator) splatArray(value *yaml.Node, tail []string, pathStack []interface{}) error {

View File

@ -2,7 +2,6 @@ package yqlib
import (
"strconv"
"strings"
yaml "gopkg.in/yaml.v3"
)
@ -16,15 +15,6 @@ func DeleteNavigationSettings(lastBit string) NavigationSettings {
autoCreateMap: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return true
},
shouldVisit: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
var prefixMatch = strings.TrimSuffix(head, "*")
if prefixMatch != head {
log.Debug("prefix match, %v", strings.HasPrefix(node.Value, prefixMatch))
return strings.HasPrefix(node.Value, prefixMatch)
}
log.Debug("equals match, %v", node.Value == head)
return node.Value == head
},
visit: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) error {
log.Debug("need to find %v in here", lastBit)
DebugNode(node)

View File

@ -1,6 +1,7 @@
package yqlib
import (
"fmt"
"strings"
yaml "gopkg.in/yaml.v3"
@ -16,15 +17,14 @@ type VisitedNode struct {
type NavigationSettings interface {
FollowAlias(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
AutoCreateMap(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
ShouldVisit(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
Visit(node *yaml.Node, head string, tail []string, pathStack []interface{}) error
ShouldTraverse(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
GetVisitedNodes() []*VisitedNode
}
type NavigationSettingsImpl struct {
followAlias func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
autoCreateMap func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
shouldVisit func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
visit func(node *yaml.Node, head string, tail []string, pathStack []interface{}) error
visitedNodes []*VisitedNode
}
@ -51,26 +51,88 @@ func (ns *NavigationSettingsImpl) AutoCreateMap(node *yaml.Node, head string, ta
return ns.autoCreateMap(node, head, tail, pathStack)
}
func (ns *NavigationSettingsImpl) Visit(node *yaml.Node, head string, tail []string, pathStack []interface{}) error {
ns.visitedNodes = append(ns.visitedNodes, &VisitedNode{node, head, tail, pathStack})
log.Debug("adding to visited nodes")
return ns.visit(node, head, tail, pathStack)
func (ns *NavigationSettingsImpl) matchesNextPath(path string, candidate string) bool {
var prefixMatch = strings.TrimSuffix(path, "*")
if prefixMatch != path {
log.Debug("prefix match, %v", strings.HasPrefix(candidate, prefixMatch))
return strings.HasPrefix(candidate, prefixMatch)
}
return candidate == path
}
func (ns *NavigationSettingsImpl) ShouldVisit(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
if !ns.alreadyVisited(node) {
return ns.shouldVisit(node, head, tail, pathStack)
} else {
log.Debug("Skipping over %v as we have seen it already", node.Value)
}
return false
}
func (ns *NavigationSettingsImpl) alreadyVisited(node *yaml.Node) bool {
for _, candidate := range ns.visitedNodes {
if candidate.Node.Value == node.Value {
func (ns *NavigationSettingsImpl) ShouldTraverse(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
// we should traverse aliases (if enabled), but not visit them :/
if len(pathStack) == 0 {
return true
}
}
if ns.alreadyVisited(pathStack) {
return false
}
lastBit := fmt.Sprintf("%v", pathStack[len(pathStack)-1])
return (lastBit == "<<" && ns.FollowAlias(node, head, tail, pathStack)) || (lastBit != "<<" && ns.matchesNextPath(head, lastBit))
}
func (ns *NavigationSettingsImpl) shouldVisit(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
// we should traverse aliases (if enabled), but not visit them :/
if len(pathStack) == 0 {
return true
}
if ns.alreadyVisited(pathStack) {
return false
}
lastBit := fmt.Sprintf("%v", pathStack[len(pathStack)-1])
// only visit aliases if its an exact match
return (lastBit == "<<" && head == "<<") || (lastBit != "<<" && ns.matchesNextPath(head, lastBit))
}
func (ns *NavigationSettingsImpl) Visit(node *yaml.Node, head string, tail []string, pathStack []interface{}) error {
if ns.shouldVisit(node, head, tail, pathStack) {
ns.visitedNodes = append(ns.visitedNodes, &VisitedNode{node, head, tail, pathStack})
log.Debug("adding to visited nodes, %v", head)
return ns.visit(node, head, tail, pathStack)
}
return nil
}
func (ns *NavigationSettingsImpl) alreadyVisited(pathStack []interface{}) bool {
log.Debug("looking for pathStack")
for _, val := range pathStack {
log.Debug("\t %v", val)
}
for _, candidate := range ns.visitedNodes {
candidatePathStack := candidate.PathStack
if patchStacksMatch(candidatePathStack, pathStack) {
log.Debug("paths match, already seen it")
return true
}
}
log.Debug("never seen it before!")
return false
}
func patchStacksMatch(path1 []interface{}, path2 []interface{}) bool {
log.Debug("checking against path")
for _, val := range path1 {
log.Debug("\t %v", val)
}
if len(path1) != len(path2) {
return false
}
for index, p1Value := range path1 {
p2Value := path2[index]
if p1Value != p2Value {
return false
}
}
return true
}

View File

@ -1,8 +1,6 @@
package yqlib
import (
"strings"
yaml "gopkg.in/yaml.v3"
)
@ -15,21 +13,6 @@ func ReadNavigationSettings() NavigationSettings {
autoCreateMap: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return false
},
shouldVisit: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
log.Debug("shouldVisit h: %v, actual: %v", head, node.Value)
if node.Value == "<<" {
log.Debug("its an alias, skip it")
// dont match alias keys, as we'll follow them instead
return false
}
var prefixMatch = strings.TrimSuffix(head, "*")
if prefixMatch != head {
log.Debug("prefix match, %v", strings.HasPrefix(node.Value, prefixMatch))
return strings.HasPrefix(node.Value, prefixMatch)
}
log.Debug("equals match, %v", node.Value == head)
return node.Value == head
},
visit: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) error {
return nil
},

View File

@ -1,8 +1,6 @@
package yqlib
import (
"strings"
yaml "gopkg.in/yaml.v3"
)
@ -15,15 +13,6 @@ func UpdateNavigationSettings(changesToApply *yaml.Node) NavigationSettings {
autoCreateMap: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return true
},
shouldVisit: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
var prefixMatch = strings.TrimSuffix(head, "*")
if prefixMatch != head {
log.Debug("prefix match, %v", strings.HasPrefix(node.Value, prefixMatch))
return strings.HasPrefix(node.Value, prefixMatch)
}
log.Debug("equals match, %v", node.Value == head)
return node.Value == head
},
visit: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) error {
log.Debug("going to update")
DebugNode(node)

View File

@ -1,11 +1,3 @@
a: 2
b:
hi:
c: things
d: something else
there:
c: more things
d: more something else
there2:
c: more things also
d: more something else also
c: thing
d: another thing