yq/pkg/yqlib/operator_delete.go

91 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
2023-06-05 22:27:59 +00:00
if candidate.Parent == nil {
// must be a top level thing, delete it
2023-05-30 05:05:28 +00:00
return removeFromContext(context, candidate)
2020-10-10 11:42:09 +00:00
}
2023-05-30 05:05:28 +00:00
log.Debugf("processing deletion of candidate %v", NodeToString(candidate))
2020-11-14 02:38:44 +00:00
2023-04-09 01:14:51 +00:00
parentNode := candidate.Parent
2023-05-30 05:05:28 +00:00
2023-05-02 05:07:04 +00:00
candidatePath := candidate.GetPath()
childPath := candidatePath[len(candidatePath)-1]
2020-12-22 00:45:51 +00:00
2023-04-09 01:14:51 +00:00
if parentNode.Kind == MappingNode {
2021-04-25 02:05:56 +00:00
deleteFromMap(candidate.Parent, childPath)
2023-04-09 01:14:51 +00:00
} 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 {
2023-05-02 05:07:04 +00:00
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
}
2023-05-30 05:05:28 +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
}
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")
2023-04-09 01:14:51 +00:00
node := candidate.unwrapDocument()
2020-10-10 11:42:09 +00:00
contents := node.Content
2023-04-09 01:14:51 +00:00
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
2023-04-11 02:39:22 +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
}
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")
2023-04-09 01:14:51 +00:00
node := candidate.unwrapDocument()
2020-10-10 11:42:09 +00:00
contents := node.Content
2023-04-09 01:14:51 +00:00
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
}