Make sure to update the key when deleting array elements

This commit is contained in:
Jan Dubois 2024-12-02 18:42:22 -08:00 committed by Mike Farah
parent 3bca3a23c0
commit c16b1800a0
2 changed files with 10 additions and 0 deletions

View File

@ -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)
}
}

View File

@ -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) {