mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-13 22:38:04 +00:00
Fixed bug - can now delete documents #1377
This commit is contained in:
parent
33ec66cfdd
commit
c640888133
@ -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)
|
||||
|
||||
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.
|
||||
if candidate.Parent == nil {
|
||||
log.Info("Could not find parent of %v", candidate.GetKey())
|
||||
log.Info("Could not find parent of %v", NodeToString(candidate))
|
||||
return context, nil
|
||||
}
|
||||
|
||||
|
@ -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]`,
|
||||
|
Loading…
Reference in New Issue
Block a user