mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 05:38:04 +00:00
Fixed delete bug
This commit is contained in:
parent
0816e16d30
commit
3c466dc66e
@ -42,3 +42,13 @@ func (n *Context) ChildContext(results *list.List) Context {
|
|||||||
clone.MatchingNodes = results
|
clone.MatchingNodes = results
|
||||||
return clone
|
return clone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Context) Clone() Context {
|
||||||
|
clone := Context{}
|
||||||
|
err := copier.Copy(&clone, n)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Error cloning context :(")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return clone
|
||||||
|
}
|
||||||
|
@ -7,8 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
|
contextToUse := context.Clone()
|
||||||
nodesToDelete, err := d.GetMatchingNodes(context, expressionNode.Rhs)
|
contextToUse.DontAutoCreate = true
|
||||||
|
nodesToDelete, err := d.GetMatchingNodes(contextToUse, expressionNode.Rhs)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Context{}, err
|
return Context{}, err
|
||||||
@ -17,19 +18,21 @@ func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *
|
|||||||
for el := nodesToDelete.MatchingNodes.Back(); el != nil; el = el.Prev() {
|
for el := nodesToDelete.MatchingNodes.Back(); el != nil; el = el.Prev() {
|
||||||
candidate := el.Value.(*CandidateNode)
|
candidate := el.Value.(*CandidateNode)
|
||||||
|
|
||||||
deleteImmediateChildOp := &Operation{
|
if len(candidate.Path) > 0 {
|
||||||
OperationType: deleteImmediateChildOpType,
|
deleteImmediateChildOp := &Operation{
|
||||||
Value: candidate.Path[len(candidate.Path)-1],
|
OperationType: deleteImmediateChildOpType,
|
||||||
}
|
Value: candidate.Path[len(candidate.Path)-1],
|
||||||
|
}
|
||||||
|
|
||||||
deleteImmediateChildOpNode := &ExpressionNode{
|
deleteImmediateChildOpNode := &ExpressionNode{
|
||||||
Operation: deleteImmediateChildOp,
|
Operation: deleteImmediateChildOp,
|
||||||
Rhs: createTraversalTree(candidate.Path[0:len(candidate.Path)-1], traversePreferences{}),
|
Rhs: createTraversalTree(candidate.Path[0:len(candidate.Path)-1], traversePreferences{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := d.GetMatchingNodes(context, deleteImmediateChildOpNode)
|
_, err := d.GetMatchingNodes(contextToUse, deleteImmediateChildOpNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Context{}, err
|
return Context{}, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return context, nil
|
return context, nil
|
||||||
|
@ -53,6 +53,22 @@ var deleteOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (doc)::a: [x, x]\n",
|
"D0, P[], (doc)::a: [x, x]\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `a: {thing1: yep, thing2: cool, thing3: hi, b: {thing1: cool, great: huh}}`,
|
||||||
|
expression: `del(..)`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!map)::{}\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `a: {thing1: yep, thing2: cool, thing3: hi, b: {thing1: cool, great: huh}}`,
|
||||||
|
expression: `del(.. | select(tag == "!!map") | (.b.thing1,.thing2))`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!map)::a: {thing1: yep, thing3: hi, b: {great: huh}}\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "Delete nested entry in array",
|
description: "Delete nested entry in array",
|
||||||
document: `[{a: cat, b: dog}]`,
|
document: `[{a: cat, b: dog}]`,
|
||||||
|
Loading…
Reference in New Issue
Block a user