yq/pkg/yqlib/delete_navigation_strategy.go

74 lines
2.4 KiB
Go
Raw Normal View History

2019-12-25 01:11:04 +00:00
package yqlib
import (
"strconv"
yaml "gopkg.in/yaml.v3"
)
2019-12-28 07:19:37 +00:00
func DeleteNavigationStrategy(pathElementToDelete string) NavigationStrategy {
2019-12-27 21:51:54 +00:00
parser := NewPathParser()
2019-12-28 07:19:37 +00:00
return &NavigationStrategyImpl{
visitedNodes: []*NodeContext{},
pathParser: parser,
2019-12-28 07:19:37 +00:00
followAlias: func(nodeContext NodeContext) bool {
2019-12-25 01:11:04 +00:00
return false
},
2019-12-28 07:19:37 +00:00
autoCreateMap: func(nodeContext NodeContext) bool {
2019-12-25 01:11:04 +00:00
return true
},
2019-12-28 07:19:37 +00:00
visit: func(nodeContext NodeContext) error {
node := nodeContext.Node
log.Debug("need to find and delete %v in here", pathElementToDelete)
2019-12-25 01:11:04 +00:00
DebugNode(node)
if node.Kind == yaml.SequenceNode {
2020-01-30 03:55:58 +00:00
newContent := deleteFromArray(parser, node.Content, nodeContext.PathStack, pathElementToDelete)
2019-12-25 01:11:04 +00:00
node.Content = newContent
} else if node.Kind == yaml.MappingNode {
2019-12-28 07:19:37 +00:00
node.Content = deleteFromMap(parser, node.Content, nodeContext.PathStack, pathElementToDelete)
2019-12-25 01:11:04 +00:00
}
return nil
},
}
}
2019-12-28 07:19:37 +00:00
func deleteFromMap(pathParser PathParser, contents []*yaml.Node, pathStack []interface{}, pathElementToDelete string) []*yaml.Node {
2019-12-27 21:51:54 +00:00
newContents := make([]*yaml.Node, 0)
for index := 0; index < len(contents); index = index + 2 {
keyNode := contents[index]
valueNode := contents[index+1]
2020-01-09 10:27:52 +00:00
if !pathParser.MatchesNextPathElement(NewNodeContext(keyNode, pathElementToDelete, []string{}, pathStack), keyNode.Value) {
2019-12-27 21:51:54 +00:00
log.Debug("adding node %v", keyNode.Value)
newContents = append(newContents, keyNode, valueNode)
} else {
log.Debug("skipping node %v", keyNode.Value)
}
}
return newContents
}
2019-12-25 01:11:04 +00:00
2020-01-30 03:55:58 +00:00
func deleteFromArray(pathParser PathParser, content []*yaml.Node, pathStack []interface{}, pathElementToDelete string) []*yaml.Node {
2019-12-27 21:51:54 +00:00
2020-01-30 03:55:58 +00:00
var indexToDelete, err = strconv.ParseInt(pathElementToDelete, 10, 64) // nolint
if err == nil {
return deleteIndexInArray(content, indexToDelete)
2019-12-27 21:51:54 +00:00
}
2020-01-30 03:55:58 +00:00
log.Debug("%v is not a numeric index, finding matching patterns", pathElementToDelete)
var newArray = make([]*yaml.Node, 0)
2019-12-27 21:51:54 +00:00
2020-01-30 03:55:58 +00:00
for _, childValue := range content {
if !pathParser.MatchesNextPathElement(NewNodeContext(childValue, pathElementToDelete, []string{}, pathStack), childValue.Value) {
newArray = append(newArray, childValue)
}
2019-12-25 01:11:04 +00:00
}
2020-01-30 03:55:58 +00:00
return newArray
}
func deleteIndexInArray(content []*yaml.Node, index int64) []*yaml.Node {
log.Debug("deleting index %v in array", index)
2019-12-25 01:11:04 +00:00
if index >= int64(len(content)) {
log.Debug("index %v is greater than content length %v", index, len(content))
2020-01-30 03:55:58 +00:00
return content
2019-12-25 01:11:04 +00:00
}
2020-01-30 03:55:58 +00:00
return append(content[:index], content[index+1:]...)
2019-12-25 01:11:04 +00:00
}