yq/pkg/yqlib/operator_delete.go

89 lines
2.5 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 (
"container/list"
2020-12-22 00:45:51 +00:00
"fmt"
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
if candidate.Parent == nil {
// must be a top level thing, delete it
return removeFromContext(context, candidate)
2020-10-10 11:42:09 +00:00
}
log.Debugf("processing deletion of candidate %v", NodeToString(candidate))
parentNode := candidate.Parent
2020-11-14 02:38:44 +00:00
candidatePath := candidate.GetPath()
childPath := candidatePath[len(candidatePath)-1]
2020-12-22 00:45:51 +00:00
if parentNode.Kind == MappingNode {
2021-04-25 02:05:56 +00:00
deleteFromMap(candidate.Parent, childPath)
} else if parentNode.Kind == 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
}
func removeFromContext(context Context, candidate *CandidateNode) (Context, error) {
newResults := list.New()
for item := context.MatchingNodes.Front(); item != nil; item = item.Next() {
nodeInContext := item.Value.(*CandidateNode)
if nodeInContext != candidate {
newResults.PushBack(nodeInContext)
} else {
log.Info("Need to delete this %v", NodeToString(nodeInContext))
}
}
return context.ChildContext(newResults), nil
}
func deleteFromMap(node *CandidateNode, childPath interface{}) {
2020-10-10 11:42:09 +00:00
log.Debug("deleteFromMap")
contents := node.Content
newContents := make([]*CandidateNode, 0)
2020-10-10 11:42:09 +00:00
for index := 0; index < len(contents); index = index + 2 {
key := contents[index]
value := contents[index+1]
2020-12-22 00:45:51 +00:00
shouldDelete := key.Value == childPath
2020-10-10 11:42:09 +00:00
log.Debugf("shouldDelete %v ? %v", NodeToString(value), shouldDelete)
2020-10-10 11:42:09 +00:00
if !shouldDelete {
newContents = append(newContents, key, value)
}
}
node.Content = newContents
}
func deleteFromArray(node *CandidateNode, childPath interface{}) {
2020-10-10 11:42:09 +00:00
log.Debug("deleteFromArray")
contents := node.Content
newContents := make([]*CandidateNode, 0)
2020-10-10 11:42:09 +00:00
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
}