From 10600dd29a4a5d7ea01f5311425837890127988e Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 4 Feb 2021 12:48:07 +1100 Subject: [PATCH] Fixed delete bug --- pkg/yqlib/operator_delete.go | 4 ++-- pkg/yqlib/operator_delete_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/yqlib/operator_delete.go b/pkg/yqlib/operator_delete.go index abda4cd2..f28569a9 100644 --- a/pkg/yqlib/operator_delete.go +++ b/pkg/yqlib/operator_delete.go @@ -13,8 +13,8 @@ func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode * if err != nil { return Context{}, err } - - for el := nodesToDelete.MatchingNodes.Front(); el != nil; el = el.Next() { + //need to iterate backwards to ensure correct indices when deleting multiple + for el := nodesToDelete.MatchingNodes.Back(); el != nil; el = el.Prev() { candidate := el.Value.(*CandidateNode) deleteImmediateChildOp := &Operation{ diff --git a/pkg/yqlib/operator_delete_test.go b/pkg/yqlib/operator_delete_test.go index f7fe831c..364f271e 100644 --- a/pkg/yqlib/operator_delete_test.go +++ b/pkg/yqlib/operator_delete_test.go @@ -37,6 +37,22 @@ var deleteOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::[1, 3]\n", }, }, + { + skipDoc: true, + document: `a: [1,2,3]`, + expression: `del(.a[])`, + expected: []string{ + "D0, P[], (doc)::a: []\n", + }, + }, + { + skipDoc: true, + document: `a: [10,x,10, 10, x, 10]`, + expression: `del(.a[] | select(. == 10))`, + expected: []string{ + "D0, P[], (doc)::a: [x, x]\n", + }, + }, { description: "Delete nested entry in array", document: `[{a: cat, b: dog}]`,