Fixed bug - can now delete documents #1377

This commit is contained in:
Mike Farah 2022-10-11 11:48:18 +11:00
parent 33ec66cfdd
commit c640888133
2 changed files with 27 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package yqlib
import (
"container/list"
"fmt"
yaml "gopkg.in/yaml.v3"
@ -16,10 +17,22 @@ func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *
for el := nodesToDelete.MatchingNodes.Back(); el != nil; el = el.Prev() {
candidate := el.Value.(*CandidateNode)
//problem: context may already be '.a' and then I pass in '.a.a2'.
// should pass in .a2.
if candidate.Parent == nil {
log.Info("Could not find parent of %v", candidate.GetKey())
if candidate.Node.Kind == yaml.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)
if nodeInContext.Node != candidate.Node {
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))
return context, nil
}

View File

@ -29,6 +29,16 @@ var deleteOperatorScenarios = []expressionScenario{
"D0, P[a], (!!map)::{a2: frood}\n",
},
},
{
skipDoc: true,
description: "delete whole document",
document2: `a: slow`,
document: `a: fast`,
expression: `del(select(.a == "fast"))`,
expected: []string{
"D0, P[], (doc)::a: slow\n",
},
},
{
skipDoc: true,
document: `a: [1,2,3]`,