mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08: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)
|
newPathStack := append(pathStack, contents[indexInMap].Value)
|
||||||
log.Debug("appended %v", contents[indexInMap].Value)
|
log.Debug("appended %v", contents[indexInMap].Value)
|
||||||
n.navigationStrategy.DebugVisitedNodes()
|
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)
|
DebugNode(value)
|
||||||
if n.navigationStrategy.ShouldTraverse(NewNodeContext(contents[indexInMap+1], head, tail, newPathStack), contents[indexInMap].Value) == true {
|
if n.navigationStrategy.ShouldTraverse(NewNodeContext(contents[indexInMap+1], head, tail, newPathStack), contents[indexInMap].Value) == true {
|
||||||
log.Debug("recurseMap: Going to traverse")
|
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 {
|
func (n *navigator) recurseArray(value *yaml.Node, head string, tail []string, pathStack []interface{}) 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 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 {
|
for int64(len(value.Content)) <= index {
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
|
|
||||||
var log = logging.MustGetLogger("yq")
|
var log = logging.MustGetLogger("yq")
|
||||||
|
|
||||||
// TODO: enumerate
|
|
||||||
type UpdateCommand struct {
|
type UpdateCommand struct {
|
||||||
Command string
|
Command string
|
||||||
Path string
|
Path string
|
||||||
@ -33,11 +32,11 @@ func DebugNode(value *yaml.Node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PathStackToString(pathStack []interface{}) string {
|
func pathStackToString(pathStack []interface{}) string {
|
||||||
return MergePathStackToString(pathStack, false)
|
return mergePathStackToString(pathStack, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MergePathStackToString(pathStack []interface{}, appendArrays bool) string {
|
func mergePathStackToString(pathStack []interface{}, appendArrays bool) string {
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
for index, path := range pathStack {
|
for index, path := range pathStack {
|
||||||
switch path.(type) {
|
switch path.(type) {
|
||||||
@ -89,6 +88,9 @@ type YqLib interface {
|
|||||||
Get(rootNode *yaml.Node, path string) ([]*NodeContext, error)
|
Get(rootNode *yaml.Node, path string) ([]*NodeContext, error)
|
||||||
Update(rootNode *yaml.Node, updateCommand UpdateCommand, autoCreate bool) error
|
Update(rootNode *yaml.Node, updateCommand UpdateCommand, autoCreate bool) error
|
||||||
New(path string) yaml.Node
|
New(path string) yaml.Node
|
||||||
|
|
||||||
|
PathStackToString(pathStack []interface{}) string
|
||||||
|
MergePathStackToString(pathStack []interface{}, appendArrays bool) string
|
||||||
}
|
}
|
||||||
|
|
||||||
type lib struct {
|
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 {
|
func (l *lib) New(path string) yaml.Node {
|
||||||
var paths = l.parser.ParsePath(path)
|
var paths = l.parser.ParsePath(path)
|
||||||
newNode := yaml.Node{Kind: guessKind("", paths, 0)}
|
newNode := yaml.Node{Kind: guessKind("", paths, 0)}
|
||||||
|
@ -6,6 +6,9 @@ import (
|
|||||||
|
|
||||||
func TestLib(t *testing.T) {
|
func TestLib(t *testing.T) {
|
||||||
|
|
||||||
|
// PathStackToString
|
||||||
|
// MergePathStackToString (with true)
|
||||||
|
|
||||||
// var log = logging.MustGetLogger("yq")
|
// var log = logging.MustGetLogger("yq")
|
||||||
// subject := NewYqLib(log)
|
// subject := NewYqLib(log)
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ func (ns *NavigationStrategyImpl) shouldVisit(nodeContext NodeContext) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ns *NavigationStrategyImpl) Visit(nodeContext NodeContext) error {
|
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)
|
DebugNode(nodeContext.Node)
|
||||||
if ns.shouldVisit(nodeContext) {
|
if ns.shouldVisit(nodeContext) {
|
||||||
log.Debug("yep, visiting")
|
log.Debug("yep, visiting")
|
||||||
@ -112,12 +112,12 @@ func (ns *NavigationStrategyImpl) Visit(nodeContext NodeContext) error {
|
|||||||
func (ns *NavigationStrategyImpl) DebugVisitedNodes() {
|
func (ns *NavigationStrategyImpl) DebugVisitedNodes() {
|
||||||
log.Debug("Visited Nodes:")
|
log.Debug("Visited Nodes:")
|
||||||
for _, candidate := range ns.visitedNodes {
|
for _, candidate := range ns.visitedNodes {
|
||||||
log.Debug(" - %v", PathStackToString(candidate.PathStack))
|
log.Debug(" - %v", pathStackToString(candidate.PathStack))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ns *NavigationStrategyImpl) alreadyVisited(pathStack []interface{}) bool {
|
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 {
|
for _, candidate := range ns.visitedNodes {
|
||||||
candidatePathStack := candidate.PathStack
|
candidatePathStack := candidate.PathStack
|
||||||
if patchStacksMatch(candidatePathStack, pathStack) {
|
if patchStacksMatch(candidatePathStack, pathStack) {
|
||||||
@ -131,7 +131,7 @@ func (ns *NavigationStrategyImpl) alreadyVisited(pathStack []interface{}) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func patchStacksMatch(path1 []interface{}, path2 []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) {
|
if len(path1) != len(path2) {
|
||||||
return false
|
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 {
|
for index, mappedDoc := range matchingNodes {
|
||||||
switch printMode {
|
switch printMode {
|
||||||
case "k":
|
case "k":
|
||||||
cmd.Print(yqlib.PathStackToString(mappedDoc.PathStack))
|
cmd.Print(lib.PathStackToString(mappedDoc.PathStack))
|
||||||
if index < len(matchingNodes)-1 {
|
if index < len(matchingNodes)-1 {
|
||||||
cmd.Print("\n")
|
cmd.Print("\n")
|
||||||
}
|
}
|
||||||
@ -343,7 +343,7 @@ func printResults(matchingNodes []*yqlib.NodeContext, cmd *cobra.Command) error
|
|||||||
// put it into a node and print that.
|
// put it into a node and print that.
|
||||||
var parentNode = yaml.Node{Kind: yaml.MappingNode}
|
var parentNode = yaml.Node{Kind: yaml.MappingNode}
|
||||||
parentNode.Content = make([]*yaml.Node, 2)
|
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
|
parentNode.Content[1] = mappedDoc.Node
|
||||||
if err := printValue(&parentNode, cmd); err != nil {
|
if err := printValue(&parentNode, cmd); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -438,7 +438,7 @@ func mergeProperties(cmd *cobra.Command, args []string) error {
|
|||||||
return errorProcessingFile
|
return errorProcessingFile
|
||||||
}
|
}
|
||||||
for _, matchingNode := range matchingNodes {
|
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})
|
updateCommands = append(updateCommands, yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user