2020-10-10 11:42:09 +00:00
|
|
|
package treeops
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/elliotchance/orderedmap"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func DeleteChildOperator(d *dataTreeNavigator, matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
|
|
|
|
lhs, err := d.getMatchingNodes(matchingNodes, pathNode.Lhs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// for each lhs, splat the node,
|
|
|
|
// the intersect it against the rhs expression
|
|
|
|
// recreate the contents using only the intersection result.
|
|
|
|
|
|
|
|
for el := lhs.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
elMap := orderedmap.NewOrderedMap()
|
2020-10-13 01:51:37 +00:00
|
|
|
elMap.Set(candidate.GetKey(), candidate)
|
2020-10-10 11:42:09 +00:00
|
|
|
nodesToDelete, err := d.getMatchingNodes(elMap, pathNode.Rhs)
|
|
|
|
log.Debug("nodesToDelete:\n%v", NodesToString(nodesToDelete))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if candidate.Node.Kind == yaml.SequenceNode {
|
|
|
|
deleteFromArray(candidate, nodesToDelete)
|
2020-10-11 23:09:13 +00:00
|
|
|
} else if candidate.Node.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
|
|
|
}
|
|
|
|
}
|
|
|
|
return lhs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteFromMap(candidate *CandidateNode, nodesToDelete *orderedmap.OrderedMap) {
|
|
|
|
log.Debug("deleteFromMap")
|
|
|
|
node := candidate.Node
|
|
|
|
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-10-13 01:51:37 +00:00
|
|
|
_, shouldDelete := nodesToDelete.Get(childCandidate.GetKey())
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteFromArray(candidate *CandidateNode, nodesToDelete *orderedmap.OrderedMap) {
|
|
|
|
log.Debug("deleteFromArray")
|
|
|
|
node := candidate.Node
|
|
|
|
contents := node.Content
|
|
|
|
newContents := make([]*yaml.Node, 0)
|
|
|
|
|
|
|
|
for index := 0; index < len(contents); index = index + 1 {
|
|
|
|
value := contents[index]
|
|
|
|
|
|
|
|
childCandidate := &CandidateNode{
|
|
|
|
Node: value,
|
|
|
|
Document: candidate.Document,
|
|
|
|
Path: append(candidate.Path, index),
|
|
|
|
}
|
|
|
|
|
2020-10-13 01:51:37 +00:00
|
|
|
_, shouldDelete := nodesToDelete.Get(childCandidate.GetKey())
|
2020-10-10 11:42:09 +00:00
|
|
|
if !shouldDelete {
|
|
|
|
newContents = append(newContents, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node.Content = newContents
|
|
|
|
}
|