Skip to content

Commit

Permalink
Make sure to update the key when deleting array elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jandubois committed Dec 3, 2024
1 parent 80310ea commit e3d2cc3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/yqlib/operator_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func deleteFromArray(node *CandidateNode, childPath interface{}) {
shouldDelete := fmt.Sprintf("%v", index) == fmt.Sprintf("%v", childPath)

if !shouldDelete {
value.Key = createScalarNode(len(newContents), fmt.Sprintf("%v", len(newContents)))
newContents = append(newContents, value)
}
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/yqlib/operator_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e3d2cc3

Please sign in to comment.