yq/pkg/yqlib/operator_delete.go

90 lines
2.6 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-04-09 01:14:51 +00:00
if candidate.Kind == DocumentNode {
//need to delete this node from context.
newResults := list.New()
for item := context.MatchingNodes.Front(); item != nil; item = item.Next() {
nodeInContext := item.Value.(*CandidateNode)
2023-04-09 01:14:51 +00:00
if nodeInContext != candidate {
newResults.PushBack(nodeInContext)
} else {
log.Info("Need to delete this %v", NodeToString(nodeInContext))
}
}
return context.ChildContext(newResults), nil
} else if candidate.Parent == nil {
//problem: context may already be '.a' and then I pass in '.a.a2'.
// should pass in .a2.
log.Info("Could not find parent of %v", NodeToString(candidate))
2021-04-25 02:05:56 +00:00
return context, nil
2020-10-10 11:42:09 +00:00
}
2020-11-14 02:38:44 +00:00
2023-04-09 01:14:51 +00:00
parentNode := candidate.Parent
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
}
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
}