2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-10 11:42:09 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-22 00:45:51 +00:00
|
|
|
"fmt"
|
2020-10-21 01:54:58 +00:00
|
|
|
|
2020-11-14 02:38:44 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-10-10 11:42:09 +00:00
|
|
|
)
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2021-02-04 02:47:59 +00:00
|
|
|
contextToUse := context.Clone()
|
|
|
|
contextToUse.DontAutoCreate = true
|
|
|
|
nodesToDelete, err := d.GetMatchingNodes(contextToUse, expressionNode.Rhs)
|
2020-12-22 00:45:51 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
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
|
|
|
|
2021-02-04 02:47:59 +00:00
|
|
|
if len(candidate.Path) > 0 {
|
|
|
|
deleteImmediateChildOp := &Operation{
|
|
|
|
OperationType: deleteImmediateChildOpType,
|
|
|
|
Value: candidate.Path[len(candidate.Path)-1],
|
|
|
|
}
|
2020-12-22 00:45:51 +00:00
|
|
|
|
2021-02-04 02:47:59 +00:00
|
|
|
deleteImmediateChildOpNode := &ExpressionNode{
|
|
|
|
Operation: deleteImmediateChildOp,
|
2021-02-08 02:58:46 +00:00
|
|
|
Rhs: createTraversalTree(candidate.Path[0:len(candidate.Path)-1], traversePreferences{}, false),
|
2021-02-04 02:47:59 +00:00
|
|
|
}
|
2020-12-22 00:45:51 +00:00
|
|
|
|
2021-02-04 02:47:59 +00:00
|
|
|
_, err := d.GetMatchingNodes(contextToUse, deleteImmediateChildOpNode)
|
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
2020-10-10 11:42:09 +00:00
|
|
|
}
|
2020-12-22 00:45:51 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context, nil
|
2020-12-22 00:45:51 +00:00
|
|
|
}
|
2020-10-10 11:42:09 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func deleteImmediateChildOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
parents, err := d.GetMatchingNodes(context, expressionNode.Rhs)
|
2020-11-14 02:38:44 +00:00
|
|
|
|
2020-12-22 00:45:51 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-12-22 00:45:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
childPath := expressionNode.Operation.Value
|
2020-12-22 00:45:51 +00:00
|
|
|
|
|
|
|
log.Debug("childPath to remove %v", childPath)
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := parents.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-12-22 00:45:51 +00:00
|
|
|
parent := el.Value.(*CandidateNode)
|
2021-01-12 23:00:51 +00:00
|
|
|
parentNode := unwrapDoc(parent.Node)
|
2020-12-22 00:45:51 +00:00
|
|
|
if parentNode.Kind == yaml.MappingNode {
|
|
|
|
deleteFromMap(parent, childPath)
|
|
|
|
} else if parentNode.Kind == yaml.SequenceNode {
|
|
|
|
deleteFromArray(parent, childPath)
|
2020-10-11 23:09:13 +00:00
|
|
|
} else {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, fmt.Errorf("Cannot delete nodes from parent of tag %v", parentNode.Tag)
|
2020-10-10 11:42:09 +00:00
|
|
|
}
|
2020-12-22 00:45:51 +00:00
|
|
|
|
2020-10-10 11:42:09 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +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")
|
2021-01-12 23:00:51 +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]
|
|
|
|
|
2021-01-12 08:36:28 +00:00
|
|
|
childCandidate := candidate.CreateChild(key.Value, value)
|
2020-11-14 02:38:44 +00:00
|
|
|
|
2020-12-22 00:45:51 +00:00
|
|
|
shouldDelete := key.Value == childPath
|
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-12-22 00:45:51 +00:00
|
|
|
func deleteFromArray(candidate *CandidateNode, childPath interface{}) {
|
2020-10-10 11:42:09 +00:00
|
|
|
log.Debug("deleteFromArray")
|
2021-01-12 23:00:51 +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-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
|
|
|
|
}
|