2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-10 11:42:09 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-21 01:54:58 +00:00
|
|
|
"container/list"
|
|
|
|
|
2020-11-14 02:38:44 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-10-10 11:42:09 +00:00
|
|
|
)
|
|
|
|
|
2020-10-21 01:54:58 +00:00
|
|
|
func DeleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
2020-10-10 11:42:09 +00:00
|
|
|
// for each lhs, splat the node,
|
|
|
|
// the intersect it against the rhs expression
|
|
|
|
// recreate the contents using only the intersection result.
|
|
|
|
|
2020-11-14 02:38:44 +00:00
|
|
|
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
2020-10-10 11:42:09 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
2020-10-21 01:54:58 +00:00
|
|
|
elMap := list.New()
|
|
|
|
elMap.PushBack(candidate)
|
2020-10-27 05:45:16 +00:00
|
|
|
nodesToDelete, err := d.GetMatchingNodes(elMap, pathNode.Rhs)
|
2020-10-10 11:42:09 +00:00
|
|
|
log.Debug("nodesToDelete:\n%v", NodesToString(nodesToDelete))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-14 02:38:44 +00:00
|
|
|
realNode := UnwrapDoc(candidate.Node)
|
|
|
|
|
|
|
|
if realNode.Kind == yaml.SequenceNode {
|
2020-10-10 11:42:09 +00:00
|
|
|
deleteFromArray(candidate, nodesToDelete)
|
2020-11-14 02:38:44 +00:00
|
|
|
} else if realNode.Kind == yaml.MappingNode {
|
2020-10-10 11:42:09 +00:00
|
|
|
deleteFromMap(candidate, nodesToDelete)
|
2020-10-11 23:09:13 +00:00
|
|
|
} else {
|
|
|
|
log.Debug("Cannot delete from node that's not a map or array %v", NodeToString(candidate))
|
2020-10-10 11:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-14 02:38:44 +00:00
|
|
|
return matchingNodes, nil
|
2020-10-10 11:42:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 01:54:58 +00:00
|
|
|
func deleteFromMap(candidate *CandidateNode, nodesToDelete *list.List) {
|
2020-10-10 11:42:09 +00:00
|
|
|
log.Debug("deleteFromMap")
|
2020-11-14 02:38:44 +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]
|
|
|
|
|
|
|
|
childCandidate := &CandidateNode{
|
|
|
|
Node: value,
|
|
|
|
Document: candidate.Document,
|
|
|
|
Path: append(candidate.Path, key.Value),
|
|
|
|
}
|
2020-11-14 02:38:44 +00:00
|
|
|
|
|
|
|
shouldDelete := false
|
|
|
|
for el := nodesToDelete.Front(); el != nil && shouldDelete == false; el = el.Next() {
|
|
|
|
if el.Value.(*CandidateNode).GetKey() == childCandidate.GetKey() {
|
|
|
|
shouldDelete = true
|
|
|
|
}
|
|
|
|
}
|
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-10-21 01:54:58 +00:00
|
|
|
func deleteFromArray(candidate *CandidateNode, nodesToDelete *list.List) {
|
2020-10-10 11:42:09 +00:00
|
|
|
log.Debug("deleteFromArray")
|
2020-11-14 02:38:44 +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-11-14 02:38:44 +00:00
|
|
|
childCandidate := &CandidateNode{
|
|
|
|
Node: value,
|
|
|
|
Document: candidate.Document,
|
|
|
|
Path: append(candidate.Path, index),
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldDelete := false
|
|
|
|
for el := nodesToDelete.Front(); el != nil && shouldDelete == false; el = el.Next() {
|
|
|
|
if el.Value.(*CandidateNode).GetKey() == childCandidate.GetKey() {
|
|
|
|
shouldDelete = true
|
|
|
|
}
|
|
|
|
}
|
2020-10-10 11:42:09 +00:00
|
|
|
|
|
|
|
if !shouldDelete {
|
|
|
|
newContents = append(newContents, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node.Content = newContents
|
|
|
|
}
|