2020-11-14 02:38:44 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var deleteOperatorScenarios = []expressionScenario{
|
|
|
|
{
|
|
|
|
description: "Delete entry in map",
|
|
|
|
document: `{a: cat, b: dog}`,
|
|
|
|
expression: `del(.b)`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!map)::{a: cat}\n",
|
2020-11-14 02:38:44 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-22 00:45:51 +00:00
|
|
|
{
|
|
|
|
description: "Delete nested entry in map",
|
|
|
|
document: `{a: {a1: fred, a2: frood}}`,
|
|
|
|
expression: `del(.a.a1)`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!map)::{a: {a2: frood}}\n",
|
2020-12-22 00:45:51 +00:00
|
|
|
},
|
|
|
|
},
|
2021-04-25 02:05:56 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `{a: {a1: fred, a2: frood}}`,
|
|
|
|
expression: `.a | del(.a1)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!map)::{a2: frood}\n",
|
|
|
|
},
|
|
|
|
},
|
2022-10-11 00:48:18 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
description: "delete whole document",
|
|
|
|
document2: `a: slow`,
|
|
|
|
document: `a: fast`,
|
|
|
|
expression: `del(select(.a == "fast"))`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!map)::a: slow\n",
|
2022-10-11 00:48:18 +00:00
|
|
|
},
|
|
|
|
},
|
2021-04-25 02:05:56 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: [1,2,3]`,
|
|
|
|
expression: `.a | del(.[1])`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!seq)::[1, 3]\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[0, {a: cat, b: dog}]`,
|
|
|
|
expression: `.[1] | del(.a)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[1], (!!map)::{b: dog}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[{a: cat, b: dog}]`,
|
|
|
|
expression: `.[0] | del(.a)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!map)::{b: dog}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[{a: {b: thing, c: frog}}]`,
|
|
|
|
expression: `.[0].a | del(.b)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0 a], (!!map)::{c: frog}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[{a: {b: thing, c: frog}}]`,
|
|
|
|
expression: `.[0] | del(.a.b)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!map)::{a: {c: frog}}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `{a: [0, {b: thing, c: frog}]}`,
|
|
|
|
expression: `.a[1] | del(.b)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a 1], (!!map)::{c: frog}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `{a: [0, {b: thing, c: frog}]}`,
|
|
|
|
expression: `.a | del(.[1].b)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!seq)::[0, {c: frog}]\n",
|
|
|
|
},
|
|
|
|
},
|
2020-12-22 00:45:51 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `{a: {a1: fred, a2: frood}}`,
|
|
|
|
expression: `del(.. | select(.=="frood"))`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::{a: {a1: fred}}\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-14 02:38:44 +00:00
|
|
|
{
|
|
|
|
description: "Delete entry in array",
|
|
|
|
document: `[1,2,3]`,
|
|
|
|
expression: `del(.[1])`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!seq)::[1, 3]\n",
|
2020-11-14 02:38:44 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-04 01:48:07 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: [1,2,3]`,
|
|
|
|
expression: `del(.a[])`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!map)::a: []\n",
|
2021-02-04 01:48:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: [10,x,10, 10, x, 10]`,
|
|
|
|
expression: `del(.a[] | select(. == 10))`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!map)::a: [x, x]\n",
|
2021-02-04 01:48:07 +00:00
|
|
|
},
|
|
|
|
},
|
2023-10-18 01:11:53 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: null`,
|
|
|
|
expression: `del(..)`,
|
|
|
|
expected: []string{},
|
|
|
|
},
|
2021-02-04 02:47:59 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: {thing1: yep, thing2: cool, thing3: hi, b: {thing1: cool, great: huh}}`,
|
|
|
|
expression: `del(..)`,
|
2023-10-18 01:11:53 +00:00
|
|
|
expected: []string{},
|
2021-02-04 02:47:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
2020-12-22 00:45:51 +00:00
|
|
|
{
|
|
|
|
description: "Delete nested entry in array",
|
|
|
|
document: `[{a: cat, b: dog}]`,
|
|
|
|
expression: `del(.[0].a)`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!seq)::[{b: dog}]\n",
|
2020-12-22 00:45:51 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-14 02:38:44 +00:00
|
|
|
{
|
|
|
|
description: "Delete no matches",
|
|
|
|
document: `{a: cat, b: dog}`,
|
|
|
|
expression: `del(.c)`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!map)::{a: cat, b: dog}\n",
|
2020-11-14 02:38:44 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-19 11:11:26 +00:00
|
|
|
{
|
|
|
|
description: "Delete matching entries",
|
|
|
|
document: `{a: cat, b: dog, c: bat}`,
|
|
|
|
expression: `del( .[] | select(. == "*at") )`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!map)::{b: dog}\n",
|
2020-11-19 11:11:26 +00:00
|
|
|
},
|
|
|
|
},
|
2021-01-08 00:59:49 +00:00
|
|
|
{
|
|
|
|
description: "Recursively delete matching keys",
|
|
|
|
document: `{a: {name: frog, b: {name: blog, age: 12}}}`,
|
|
|
|
expression: `del(.. | select(has("name")).name)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::{a: {b: {age: 12}}}\n",
|
|
|
|
},
|
|
|
|
},
|
2024-12-03 02:42:22 +00:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
2020-11-14 02:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range deleteOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
documentOperatorScenarios(t, "delete", deleteOperatorScenarios)
|
2020-11-14 02:38:44 +00:00
|
|
|
}
|