yq/pkg/yqlib/delete_navigation_strategy.go

77 lines
2.5 KiB
Go
Raw Normal View History

2019-12-25 01:11:04 +00:00
package yqlib
import (
yaml "gopkg.in/yaml.v3"
)
2020-02-12 04:40:21 +00:00
func DeleteNavigationStrategy(pathElementToDelete interface{}) 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 {
2020-02-03 22:58:20 +00:00
return false
2019-12-25 01:11:04 +00:00
},
2020-02-07 05:32:39 +00:00
shouldDeeplyTraverse: func(nodeContext NodeContext) bool {
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
},
}
}
2020-02-12 04:40:21 +00:00
func deleteFromMap(pathParser PathParser, contents []*yaml.Node, pathStack []interface{}, pathElementToDelete interface{}) []*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-02-12 04:40:21 +00:00
if !pathParser.MatchesNextPathElement(NewNodeContext(keyNode, pathElementToDelete, make([]interface{}, 0), 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-02-12 04:40:21 +00:00
func deleteFromArray(pathParser PathParser, content []*yaml.Node, pathStack []interface{}, pathElementToDelete interface{}) []*yaml.Node {
2019-12-27 21:51:54 +00:00
2020-02-12 04:40:21 +00:00
switch pathElementToDelete := pathElementToDelete.(type) {
case int64:
return deleteIndexInArray(content, pathElementToDelete)
default:
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-02-12 04:40:21 +00:00
for _, childValue := range content {
if !pathParser.MatchesNextPathElement(NewNodeContext(childValue, pathElementToDelete, make([]interface{}, 0), pathStack), childValue.Value) {
newArray = append(newArray, childValue)
}
2020-01-30 03:55:58 +00:00
}
2020-02-12 04:40:21 +00:00
return newArray
2019-12-25 01:11:04 +00:00
}
2020-02-12 04:40:21 +00:00
2020-01-30 03:55:58 +00:00
}
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
}