1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Can delete arrays

This commit is contained in:
Mike Farah
2019-12-12 20:47:22 +11:00
parent 8c0046a622
commit d061b2f9f9
4 changed files with 75 additions and 51 deletions

View File

@@ -10,8 +10,9 @@ import (
type DataNavigator interface {
DebugNode(node *yaml.Node)
Get(rootNode *yaml.Node, remainingPath []string) (*yaml.Node, error)
Update(rootNode *yaml.Node, remainingPath []string, changesToApply yaml.Node) error
Get(rootNode *yaml.Node, path []string) (*yaml.Node, error)
Update(rootNode *yaml.Node, path []string, changesToApply yaml.Node) error
Delete(rootNode *yaml.Node, path []string) error
}
type navigator struct {
@@ -34,8 +35,8 @@ func (n *navigator) Get(value *yaml.Node, path []string) (*yaml.Node, error) {
return n.Visit(value, path, identityVisitor)
}
func (n *navigator) Update(value *yaml.Node, path []string, changesToApply yaml.Node) error {
_, errorVisiting := n.Visit(value, path, func(nodeToUpdate *yaml.Node) (*yaml.Node, error) {
func (n *navigator) Update(rootNode *yaml.Node, path []string, changesToApply yaml.Node) error {
_, errorVisiting := n.Visit(rootNode, path, func(nodeToUpdate *yaml.Node) (*yaml.Node, error) {
n.log.Debug("going to update")
n.DebugNode(nodeToUpdate)
n.log.Debug("with")
@@ -53,6 +54,36 @@ func (n *navigator) Update(value *yaml.Node, path []string, changesToApply yaml.
return errorVisiting
}
func (n *navigator) Delete(rootNode *yaml.Node, path []string) error {
lastBit, newTail := path[len(path)-1], path[:len(path)-1]
n.log.Debug("splitting path, %v", lastBit)
n.log.Debug("new tail, %v", newTail)
_, errorVisiting := n.Visit(rootNode, newTail, func(nodeToUpdate *yaml.Node) (*yaml.Node, error) {
n.log.Debug("need to find %v in here", lastBit)
n.DebugNode(nodeToUpdate)
if nodeToUpdate.Kind == yaml.SequenceNode {
var index, err = strconv.ParseInt(lastBit, 10, 64) // nolint
if err != nil {
return nil, err
}
if index >= int64(len(nodeToUpdate.Content)) {
n.log.Debug("index %v is greater than content lenth %v", index, len(nodeToUpdate.Content))
return nodeToUpdate, nil
}
original := nodeToUpdate.Content
nodeToUpdate.Content = append(original[:index], original[index+1:]...)
} else if nodeToUpdate.Kind == yaml.MappingNode {
}
return nodeToUpdate, nil
})
return errorVisiting
}
func (n *navigator) Visit(value *yaml.Node, path []string, visitor VisitorFn) (*yaml.Node, error) {
realValue := value
if realValue.Kind == yaml.DocumentNode {

View File

@@ -50,8 +50,8 @@ func (l *lib) Update(rootNode *yaml.Node, updateCommand UpdateCommand) error {
var paths = l.parser.ParsePath(updateCommand.Path)
return l.navigator.Update(rootNode, paths, updateCommand.Value)
case "delete":
l.log.Debugf("need to implement delete")
return nil
var paths = l.parser.ParsePath(updateCommand.Path)
return l.navigator.Delete(rootNode, paths)
default:
return fmt.Errorf("Unknown command %v", updateCommand.Command)
}