From c16b1800a0896e0d5e31795380931cc77dce84c5 Mon Sep 17 00:00:00 2001 From: Jan Dubois Date: Mon, 2 Dec 2024 18:42:22 -0800 Subject: [PATCH] Make sure to update the key when deleting array elements --- pkg/yqlib/operator_delete.go | 1 + pkg/yqlib/operator_delete_test.go | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/pkg/yqlib/operator_delete.go b/pkg/yqlib/operator_delete.go index 63f05f81..653a40cd 100644 --- a/pkg/yqlib/operator_delete.go +++ b/pkg/yqlib/operator_delete.go @@ -81,6 +81,7 @@ func deleteFromArray(node *CandidateNode, childPath interface{}) { shouldDelete := fmt.Sprintf("%v", index) == fmt.Sprintf("%v", childPath) if !shouldDelete { + value.Key.Value = fmt.Sprintf("%v", len(newContents)) newContents = append(newContents, value) } } diff --git a/pkg/yqlib/operator_delete_test.go b/pkg/yqlib/operator_delete_test.go index 473b329a..cae7bc83 100644 --- a/pkg/yqlib/operator_delete_test.go +++ b/pkg/yqlib/operator_delete_test.go @@ -179,6 +179,15 @@ var deleteOperatorScenarios = []expressionScenario{ "D0, P[], (!!map)::{a: {b: {age: 12}}}\n", }, }, + { + skipDoc: true, + description: "Repeatedly delete the first element of a list", + document: `a: [0, 1, 2, 3]`, + expression: `del(.a[0]) | del(.a[0])`, + expected: []string{ + "D0, P[], (!!map)::a: [2, 3]\n", + }, + }, } func TestDeleteOperatorScenarios(t *testing.T) {