yq/pkg/yqlib/operator_delete.go

80 lines
2.1 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-10 11:42:09 +00:00
import (
2020-12-22 00:45:51 +00:00
"fmt"
2020-10-21 01:54:58 +00:00
2020-11-14 02:38:44 +00:00
yaml "gopkg.in/yaml.v3"
2020-10-10 11:42:09 +00:00
)
func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
nodesToDelete, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
2020-12-22 00:45:51 +00:00
if err != nil {
return Context{}, err
2020-12-22 00:45:51 +00:00
}
2021-02-04 01:48:07 +00:00
//need to iterate backwards to ensure correct indices when deleting multiple
for el := nodesToDelete.MatchingNodes.Back(); el != nil; el = el.Prev() {
2020-10-10 11:42:09 +00:00
candidate := el.Value.(*CandidateNode)
2020-12-22 00:45:51 +00:00
2021-04-25 02:05:56 +00:00
//problem: context may already be '.a' and then I pass in '.a.a2'.
// should pass in .a2.
if candidate.Parent == nil {
log.Info("Could not find parent of %v", candidate.GetKey())
return context, nil
2020-10-10 11:42:09 +00:00
}
2020-11-14 02:38:44 +00:00
2021-04-25 02:05:56 +00:00
parentNode := candidate.Parent.Node
childPath := candidate.Path[len(candidate.Path)-1]
2020-12-22 00:45:51 +00:00
if parentNode.Kind == yaml.MappingNode {
2021-04-25 02:05:56 +00:00
deleteFromMap(candidate.Parent, childPath)
2020-12-22 00:45:51 +00:00
} else if parentNode.Kind == yaml.SequenceNode {
2021-04-25 02:05:56 +00:00
deleteFromArray(candidate.Parent, childPath)
2020-10-11 23:09:13 +00:00
} else {
return Context{}, fmt.Errorf("Cannot delete nodes from parent of tag %v", parentNode.Tag)
2020-10-10 11:42:09 +00:00
}
}
return context, nil
2020-10-10 11:42:09 +00:00
}
2020-12-22 00:45:51 +00:00
func deleteFromMap(candidate *CandidateNode, childPath interface{}) {
2020-10-10 11:42:09 +00:00
log.Debug("deleteFromMap")
2021-01-12 23:00:51 +00:00
node := unwrapDoc(candidate.Node)
2020-10-10 11:42:09 +00:00
contents := node.Content
newContents := make([]*yaml.Node, 0)
for index := 0; index < len(contents); index = index + 2 {
key := contents[index]
value := contents[index+1]
2021-11-23 22:57:35 +00:00
childCandidate := candidate.CreateChildInMap(key, value)
2020-11-14 02:38:44 +00:00
2020-12-22 00:45:51 +00:00
shouldDelete := key.Value == childPath
2020-10-10 11:42:09 +00:00
2020-10-13 01:51:37 +00:00
log.Debugf("shouldDelete %v ? %v", childCandidate.GetKey(), shouldDelete)
2020-10-10 11:42:09 +00:00
if !shouldDelete {
newContents = append(newContents, key, value)
}
}
node.Content = newContents
}
2020-12-22 00:45:51 +00:00
func deleteFromArray(candidate *CandidateNode, childPath interface{}) {
2020-10-10 11:42:09 +00:00
log.Debug("deleteFromArray")
2021-01-12 23:00:51 +00:00
node := unwrapDoc(candidate.Node)
2020-10-10 11:42:09 +00:00
contents := node.Content
newContents := make([]*yaml.Node, 0)
for index := 0; index < len(contents); index = index + 1 {
value := contents[index]
2020-12-22 00:45:51 +00:00
shouldDelete := fmt.Sprintf("%v", index) == fmt.Sprintf("%v", childPath)
2020-10-10 11:42:09 +00:00
if !shouldDelete {
newContents = append(newContents, value)
}
}
node.Content = newContents
}