mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-12 19:25:37 +00:00
Added missing functions to interface
This commit is contained in:
parent
52eef67e37
commit
924eb6c462
@ -96,7 +96,7 @@ func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, pat
|
||||
newPathStack := append(pathStack, contents[indexInMap].Value)
|
||||
log.Debug("appended %v", contents[indexInMap].Value)
|
||||
n.navigationStrategy.DebugVisitedNodes()
|
||||
log.Debug("should I traverse? %v, %v", head, PathStackToString(newPathStack))
|
||||
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")
|
||||
@ -232,7 +232,7 @@ func (n *navigator) appendArray(value *yaml.Node, head string, tail []string, pa
|
||||
func (n *navigator) recurseArray(value *yaml.Node, head string, tail []string, pathStack []interface{}) error {
|
||||
var index, err = strconv.ParseInt(head, 10, 64) // nolint
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Error parsing array index '%v' for '%v'", head, PathStackToString(pathStack))
|
||||
return errors.Wrapf(err, "Error parsing array index '%v' for '%v'", head, pathStackToString(pathStack))
|
||||
}
|
||||
|
||||
for int64(len(value.Content)) <= index {
|
||||
|
@ -12,7 +12,6 @@ import (
|
||||
|
||||
var log = logging.MustGetLogger("yq")
|
||||
|
||||
// TODO: enumerate
|
||||
type UpdateCommand struct {
|
||||
Command string
|
||||
Path string
|
||||
@ -33,11 +32,11 @@ func DebugNode(value *yaml.Node) {
|
||||
}
|
||||
}
|
||||
|
||||
func PathStackToString(pathStack []interface{}) string {
|
||||
return MergePathStackToString(pathStack, false)
|
||||
func pathStackToString(pathStack []interface{}) string {
|
||||
return mergePathStackToString(pathStack, false)
|
||||
}
|
||||
|
||||
func MergePathStackToString(pathStack []interface{}, appendArrays bool) string {
|
||||
func mergePathStackToString(pathStack []interface{}, appendArrays bool) string {
|
||||
var sb strings.Builder
|
||||
for index, path := range pathStack {
|
||||
switch path.(type) {
|
||||
@ -89,6 +88,9 @@ type YqLib interface {
|
||||
Get(rootNode *yaml.Node, path string) ([]*NodeContext, error)
|
||||
Update(rootNode *yaml.Node, updateCommand UpdateCommand, autoCreate bool) error
|
||||
New(path string) yaml.Node
|
||||
|
||||
PathStackToString(pathStack []interface{}) string
|
||||
MergePathStackToString(pathStack []interface{}, appendArrays bool) string
|
||||
}
|
||||
|
||||
type lib struct {
|
||||
@ -111,6 +113,14 @@ func (l *lib) Get(rootNode *yaml.Node, path string) ([]*NodeContext, error) {
|
||||
|
||||
}
|
||||
|
||||
func (l *lib) PathStackToString(pathStack []interface{}) string {
|
||||
return pathStackToString(pathStack)
|
||||
}
|
||||
|
||||
func (l *lib) MergePathStackToString(pathStack []interface{}, appendArrays bool) string {
|
||||
return mergePathStackToString(pathStack, appendArrays)
|
||||
}
|
||||
|
||||
func (l *lib) New(path string) yaml.Node {
|
||||
var paths = l.parser.ParsePath(path)
|
||||
newNode := yaml.Node{Kind: guessKind("", paths, 0)}
|
||||
|
@ -6,6 +6,9 @@ import (
|
||||
|
||||
func TestLib(t *testing.T) {
|
||||
|
||||
// PathStackToString
|
||||
// MergePathStackToString (with true)
|
||||
|
||||
// var log = logging.MustGetLogger("yq")
|
||||
// subject := NewYqLib(log)
|
||||
|
||||
|
@ -95,7 +95,7 @@ func (ns *NavigationStrategyImpl) shouldVisit(nodeContext NodeContext) bool {
|
||||
}
|
||||
|
||||
func (ns *NavigationStrategyImpl) Visit(nodeContext NodeContext) error {
|
||||
log.Debug("Visit?, %v, %v", nodeContext.Head, PathStackToString(nodeContext.PathStack))
|
||||
log.Debug("Visit?, %v, %v", nodeContext.Head, pathStackToString(nodeContext.PathStack))
|
||||
DebugNode(nodeContext.Node)
|
||||
if ns.shouldVisit(nodeContext) {
|
||||
log.Debug("yep, visiting")
|
||||
@ -112,12 +112,12 @@ func (ns *NavigationStrategyImpl) Visit(nodeContext NodeContext) error {
|
||||
func (ns *NavigationStrategyImpl) DebugVisitedNodes() {
|
||||
log.Debug("Visited Nodes:")
|
||||
for _, candidate := range ns.visitedNodes {
|
||||
log.Debug(" - %v", PathStackToString(candidate.PathStack))
|
||||
log.Debug(" - %v", pathStackToString(candidate.PathStack))
|
||||
}
|
||||
}
|
||||
|
||||
func (ns *NavigationStrategyImpl) alreadyVisited(pathStack []interface{}) bool {
|
||||
log.Debug("checking already visited pathStack: %v", PathStackToString(pathStack))
|
||||
log.Debug("checking already visited pathStack: %v", pathStackToString(pathStack))
|
||||
for _, candidate := range ns.visitedNodes {
|
||||
candidatePathStack := candidate.PathStack
|
||||
if patchStacksMatch(candidatePathStack, pathStack) {
|
||||
@ -131,7 +131,7 @@ func (ns *NavigationStrategyImpl) alreadyVisited(pathStack []interface{}) bool {
|
||||
}
|
||||
|
||||
func patchStacksMatch(path1 []interface{}, path2 []interface{}) bool {
|
||||
log.Debug("checking against path: %v", PathStackToString(path1))
|
||||
log.Debug("checking against path: %v", pathStackToString(path1))
|
||||
|
||||
if len(path1) != len(path2) {
|
||||
return false
|
||||
|
6
yq.go
6
yq.go
@ -335,7 +335,7 @@ func printResults(matchingNodes []*yqlib.NodeContext, cmd *cobra.Command) error
|
||||
for index, mappedDoc := range matchingNodes {
|
||||
switch printMode {
|
||||
case "k":
|
||||
cmd.Print(yqlib.PathStackToString(mappedDoc.PathStack))
|
||||
cmd.Print(lib.PathStackToString(mappedDoc.PathStack))
|
||||
if index < len(matchingNodes)-1 {
|
||||
cmd.Print("\n")
|
||||
}
|
||||
@ -343,7 +343,7 @@ func printResults(matchingNodes []*yqlib.NodeContext, cmd *cobra.Command) error
|
||||
// put it into a node and print that.
|
||||
var parentNode = yaml.Node{Kind: yaml.MappingNode}
|
||||
parentNode.Content = make([]*yaml.Node, 2)
|
||||
parentNode.Content[0] = &yaml.Node{Kind: yaml.ScalarNode, Value: yqlib.PathStackToString(mappedDoc.PathStack)}
|
||||
parentNode.Content[0] = &yaml.Node{Kind: yaml.ScalarNode, Value: lib.PathStackToString(mappedDoc.PathStack)}
|
||||
parentNode.Content[1] = mappedDoc.Node
|
||||
if err := printValue(&parentNode, cmd); err != nil {
|
||||
return err
|
||||
@ -438,7 +438,7 @@ func mergeProperties(cmd *cobra.Command, args []string) error {
|
||||
return errorProcessingFile
|
||||
}
|
||||
for _, matchingNode := range matchingNodes {
|
||||
mergePath := yqlib.MergePathStackToString(matchingNode.PathStack, appendFlag)
|
||||
mergePath := lib.MergePathStackToString(matchingNode.PathStack, appendFlag)
|
||||
updateCommands = append(updateCommands, yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user