2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-21 01:54:58 +00:00
|
|
|
|
2020-10-21 02:54:51 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var collectObjectOperatorScenarios = []expressionScenario{
|
2023-05-05 04:13:18 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `{"name": "mike"} | .name`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[name], (!!str)::mike\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `{"person": {"names": ["mike"]}} | .person.names[0]`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[person names 0], (!!str)::mike\n",
|
|
|
|
},
|
|
|
|
},
|
2023-04-17 06:20:00 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[{name: cat}, {name: dog}]`,
|
|
|
|
expression: `.[] | {.name: "great"}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::cat: great\n",
|
|
|
|
"D0, P[], (!!map)::dog: great\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `({} + {}) | (.b = 3)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::b: 3\n",
|
|
|
|
},
|
|
|
|
},
|
2021-05-21 22:22:45 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "a: []",
|
|
|
|
expression: `.a += [{"key": "att2", "value": "val2"}]`,
|
|
|
|
expected: []string{
|
2022-01-26 22:58:13 +00:00
|
|
|
"D0, P[], (doc)::a:\n - key: att2\n value: val2\n",
|
2021-05-21 22:22:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "",
|
|
|
|
expression: `.a += {"key": "att2", "value": "val2"}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], ()::a:\n key: att2\n value: val2\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "",
|
|
|
|
expression: `.a += [0]`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], ()::a:\n - 0\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-13 02:19:54 +00:00
|
|
|
{
|
2020-11-13 10:34:43 +00:00
|
|
|
description: `Collect empty object`,
|
|
|
|
document: ``,
|
|
|
|
expression: `{}`,
|
2020-11-22 02:16:54 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::{}\n",
|
|
|
|
},
|
2020-11-13 02:19:54 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 10:34:43 +00:00
|
|
|
description: `Wrap (prefix) existing object`,
|
|
|
|
document: "{name: Mike}\n",
|
|
|
|
expression: `{"wrap": .}`,
|
2020-11-13 02:19:54 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: {name: Mike}\n",
|
|
|
|
},
|
|
|
|
},
|
2021-11-30 02:22:18 +00:00
|
|
|
{
|
2023-04-17 06:20:00 +00:00
|
|
|
skipDoc: true,
|
|
|
|
description: "Two documents",
|
|
|
|
document: "{name: Mike}\n",
|
|
|
|
document2: "{name: Bob}\n",
|
|
|
|
expression: `{"wrap": .}`,
|
2021-11-30 02:22:18 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: {name: Mike}\n",
|
|
|
|
"D0, P[], (!!map)::wrap: {name: Bob}\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-13 02:19:54 +00:00
|
|
|
{
|
2023-04-17 06:20:00 +00:00
|
|
|
skipDoc: true,
|
|
|
|
description: "two embedded documents",
|
|
|
|
document: "{name: Mike}\n---\n{name: Bob}",
|
|
|
|
expression: `{"wrap": .}`,
|
2020-11-13 02:19:54 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: {name: Mike}\n",
|
2023-04-17 06:20:00 +00:00
|
|
|
"D1, P[], (!!map)::wrap: {name: Bob}\n",
|
2020-11-13 02:19:54 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-21 02:54:51 +00:00
|
|
|
{
|
2020-11-13 10:34:43 +00:00
|
|
|
skipDoc: true,
|
2020-10-21 02:54:51 +00:00
|
|
|
document: `{name: Mike, age: 32}`,
|
|
|
|
expression: `{.name: .age}`,
|
|
|
|
expected: []string{
|
2020-10-28 02:00:26 +00:00
|
|
|
"D0, P[], (!!map)::Mike: 32\n",
|
2020-10-21 02:54:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 10:34:43 +00:00
|
|
|
description: `Using splat to create multiple objects`,
|
|
|
|
document: `{name: Mike, pets: [cat, dog]}`,
|
2020-11-22 02:50:32 +00:00
|
|
|
expression: `{.name: .pets.[]}`,
|
2020-10-21 02:54:51 +00:00
|
|
|
expected: []string{
|
2020-10-28 02:00:26 +00:00
|
|
|
"D0, P[], (!!map)::Mike: cat\n",
|
|
|
|
"D0, P[], (!!map)::Mike: dog\n",
|
2020-10-21 02:54:51 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-13 02:19:54 +00:00
|
|
|
{
|
2020-11-16 01:09:57 +00:00
|
|
|
description: `Working with multiple documents`,
|
|
|
|
dontFormatInputForDoc: false,
|
|
|
|
document: "{name: Mike, pets: [cat, dog]}\n---\n{name: Rosey, pets: [monkey, sheep]}",
|
2020-11-22 02:50:32 +00:00
|
|
|
expression: `{.name: .pets.[]}`,
|
2020-11-13 02:19:54 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::Mike: cat\n",
|
|
|
|
"D0, P[], (!!map)::Mike: dog\n",
|
2023-04-17 06:20:00 +00:00
|
|
|
"D1, P[], (!!map)::Rosey: monkey\n",
|
|
|
|
"D1, P[], (!!map)::Rosey: sheep\n",
|
2020-11-13 02:19:54 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-21 02:54:51 +00:00
|
|
|
{
|
2020-11-13 10:34:43 +00:00
|
|
|
skipDoc: true,
|
2020-10-21 02:54:51 +00:00
|
|
|
document: `{name: Mike, pets: [cat, dog], food: [hotdog, burger]}`,
|
2020-11-22 02:50:32 +00:00
|
|
|
expression: `{.name: .pets.[], "f":.food.[]}`,
|
2020-10-21 02:54:51 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::Mike: cat\nf: hotdog\n",
|
|
|
|
"D0, P[], (!!map)::Mike: cat\nf: burger\n",
|
|
|
|
"D0, P[], (!!map)::Mike: dog\nf: hotdog\n",
|
|
|
|
"D0, P[], (!!map)::Mike: dog\nf: burger\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-28 00:34:01 +00:00
|
|
|
{
|
2020-11-13 10:34:43 +00:00
|
|
|
skipDoc: true,
|
2022-04-14 22:27:22 +00:00
|
|
|
document: "name: Mike\npets:\n cows:\n - apl\n - bba",
|
|
|
|
document2: "name: Rosey\npets:\n sheep:\n - frog\n - meow",
|
2020-10-28 00:34:01 +00:00
|
|
|
expression: `{"a":.name, "b":.pets}`,
|
|
|
|
expected: []string{
|
2022-04-14 22:27:22 +00:00
|
|
|
"D0, P[], (!!map)::a: Mike\nb:\n cows:\n - apl\n - bba\n",
|
|
|
|
"D0, P[], (!!map)::a: Rosey\nb:\n sheep:\n - frog\n - meow\n",
|
2020-10-28 02:00:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 10:34:43 +00:00
|
|
|
description: "Creating yaml from scratch",
|
|
|
|
document: ``,
|
|
|
|
expression: `{"wrap": "frog"}`,
|
2020-10-28 02:00:26 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: frog\n",
|
|
|
|
},
|
|
|
|
},
|
2021-11-30 22:14:18 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `{"wrap": "frog", "bing": "bong"}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: frog\nbing: bong\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-28 02:00:26 +00:00
|
|
|
{
|
2020-11-13 10:34:43 +00:00
|
|
|
skipDoc: true,
|
2020-10-28 02:00:26 +00:00
|
|
|
document: `{name: Mike}`,
|
|
|
|
expression: `{"wrap": .}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: {name: Mike}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 10:34:43 +00:00
|
|
|
skipDoc: true,
|
2020-10-28 02:00:26 +00:00
|
|
|
document: `{name: Mike}`,
|
2020-11-13 02:19:54 +00:00
|
|
|
expression: `{"wrap": {"further": .}} | (.. style= "flow")`,
|
2020-10-28 02:00:26 +00:00
|
|
|
expected: []string{
|
2020-11-13 02:19:54 +00:00
|
|
|
"D0, P[], (!!map)::{wrap: {further: {name: Mike}}}\n",
|
2020-10-28 00:34:01 +00:00
|
|
|
},
|
|
|
|
},
|
2022-05-27 01:22:10 +00:00
|
|
|
{
|
|
|
|
description: "Creating yaml from scratch with multiple objects",
|
|
|
|
expression: `(.a.b = "foo") | (.d.e = "bar")`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], ()::a:\n b: foo\nd:\n e: bar\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-21 02:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCollectObjectOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range collectObjectOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
documentOperatorScenarios(t, "create-collect-into-object", collectObjectOperatorScenarios)
|
2020-10-21 02:54:51 +00:00
|
|
|
}
|