yq/pkg/yqlib/operator_traverse_path_test.go

547 lines
12 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-20 02:53:26 +00:00
import (
"testing"
)
var mergeDocSample = `foo: &foo
2020-10-30 01:00:48 +00:00
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
<<: [*foo,*bar]
c: foobarList_c
foobar:
c: foobar_c
<<: *foo
thing: foobar_thing
`
// cannot use merge anchors with arrays
var badAliasSample = `
_common: &common-docker-file
- FROM ubuntu:18.04
steps:
<<: *common-docker-file
`
2020-10-20 02:53:26 +00:00
var traversePathOperatorScenarios = []expressionScenario{
{
skipDoc: true,
description: "dynamically set parent and key",
expression: `.a.b.c = 3 | .a.b.c`,
expected: []string{
"D0, P[a b c], (!!int)::3\n",
},
},
{
skipDoc: true,
description: "dynamically set parent and key in array",
expression: `.a.b[0] = 3 | .a.b[0]`,
expected: []string{
"D0, P[a b 0], (!!int)::3\n",
},
},
{
skipDoc: true,
description: "dynamically set parent and key",
expression: `.a.b = ["x","y"] | .a.b[1]`,
expected: []string{
"D0, P[a b 1], (!!str)::y\n",
},
},
{
skipDoc: true,
description: "splat empty map",
document: "{}",
expression: ".[]",
expected: []string{},
},
2021-05-21 04:18:24 +00:00
{
skipDoc: true,
document: `[[1]]`,
expression: `.[0][0]`,
expected: []string{
"D0, P[0 0], (!!int)::1\n",
},
},
{
skipDoc: true,
expression: `.cat["12"] = "things"`,
expected: []string{
"D0, P[], ()::cat:\n \"12\": things\n",
},
},
{
skipDoc: true,
document: `blah: {}`,
expression: `.blah.cat = "cool"`,
expected: []string{
"D0, P[], (!!map)::blah:\n cat: cool\n",
},
},
{
skipDoc: true,
document: `blah: []`,
expression: `.blah.0 = "cool"`,
expected: []string{
"D0, P[], (!!map)::blah:\n - cool\n",
},
},
{
skipDoc: true,
document: `b: cat`,
expression: ".b\n",
expected: []string{
"D0, P[b], (!!str)::cat\n",
},
},
2021-05-21 04:18:24 +00:00
{
skipDoc: true,
document: `[[[1]]]`,
expression: `.[0][0][0]`,
expected: []string{
"D0, P[0 0 0], (!!int)::1\n",
},
},
{
skipDoc: true,
expression: `.["cat"] = "thing"`,
expected: []string{
"D0, P[], ()::cat: thing\n",
},
},
2020-10-20 02:53:26 +00:00
{
2020-11-17 23:32:30 +00:00
description: "Simple map navigation",
document: `{a: {b: apple}}`,
expression: `.a`,
2020-10-20 02:53:26 +00:00
expected: []string{
"D0, P[a], (!!map)::{b: apple}\n",
},
},
2020-10-20 04:33:20 +00:00
{
2020-11-17 23:32:30 +00:00
description: "Splat",
subdescription: "Often used to pipe children into other operators",
document: `[{b: apple}, {c: banana}]`,
expression: `.[]`,
2020-10-20 04:33:20 +00:00
expected: []string{
"D0, P[0], (!!map)::{b: apple}\n",
"D0, P[1], (!!map)::{c: banana}\n",
},
},
2021-05-09 05:36:33 +00:00
{
description: "Optional Splat",
subdescription: "Just like splat, but won't error if you run it against scalars",
document: `"cat"`,
expression: `.[]`,
expected: []string{},
},
2020-11-22 02:16:54 +00:00
{
description: "Special characters",
subdescription: "Use quotes with square brackets around path elements with special characters",
2020-11-22 02:16:54 +00:00
document: `{"{}": frog}`,
2021-02-05 02:29:35 +00:00
expression: `.["{}"]`,
2020-11-22 02:16:54 +00:00
expected: []string{
"D0, P[{}], (!!str)::frog\n",
},
},
2021-06-09 22:31:26 +00:00
{
2021-06-09 22:35:41 +00:00
description: "Nested special characters",
2021-06-09 22:31:26 +00:00
document: `a: {"key.withdots": {"another.key": apple}}`,
expression: `.a["key.withdots"]["another.key"]`,
expected: []string{
"D0, P[a key.withdots another.key], (!!str)::apple\n",
},
},
2021-02-05 04:00:46 +00:00
{
description: "Keys with spaces",
subdescription: "Use quotes with square brackets around path elements with special characters",
2021-02-05 04:00:46 +00:00
document: `{"red rabbit": frog}`,
expression: `.["red rabbit"]`,
expected: []string{
"D0, P[red rabbit], (!!str)::frog\n",
},
},
2021-02-05 02:29:35 +00:00
{
skipDoc: true,
document: `{"flying fox": frog}`,
expression: `.["flying fox"]`,
expected: []string{
"D0, P[flying fox], (!!str)::frog\n",
},
},
{
skipDoc: true,
document: `c: dog`,
expression: `.[.a.b] as $x | .`,
expected: []string{
"D0, P[], (!!map)::c: dog\n",
},
},
2021-01-09 01:06:19 +00:00
{
description: "Dynamic keys",
subdescription: `Expressions within [] can be used to dynamically lookup / calculate keys`,
document: `{b: apple, apple: crispy yum, banana: soft yum}`,
expression: `.[.b]`,
expected: []string{
"D0, P[apple], (!!str)::crispy yum\n",
},
},
2021-02-03 04:51:26 +00:00
{
skipDoc: true,
document: `{b: apple, fruit: {apple: yum, banana: smooth}}`,
expression: `.fruit[.b]`,
expected: []string{
"D0, P[fruit apple], (!!str)::yum\n",
},
},
2020-10-20 04:33:20 +00:00
{
2020-11-17 23:32:30 +00:00
description: "Children don't exist",
subdescription: "Nodes are added dynamically while traversing",
document: `{c: banana}`,
expression: `.a.b`,
2020-10-20 04:33:20 +00:00
expected: []string{
2020-10-21 01:54:58 +00:00
"D0, P[a b], (!!null)::null\n",
2020-10-20 04:33:20 +00:00
},
},
2021-05-09 05:12:50 +00:00
{
description: "Optional identifier",
subdescription: "Like jq, does not output an error when the yaml is not an array or object as expected",
document: `[1,2,3]`,
expression: `.a?`,
expected: []string{},
},
2021-05-09 05:36:33 +00:00
{
skipDoc: true,
document: `[[1,2,3], {a: frog}]`,
expression: `.[] | .["a"]?`,
expected: []string{"D0, P[1 a], (!!str)::frog\n"},
},
2020-10-20 04:33:20 +00:00
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-12-26 10:37:08 +00:00
document: ``,
2020-10-20 04:33:20 +00:00
expression: `.[1].a`,
expected: []string{
2020-10-21 01:54:58 +00:00
"D0, P[1 a], (!!null)::null\n",
2020-10-20 04:33:20 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-20 04:33:20 +00:00
document: `{}`,
2021-02-03 04:51:26 +00:00
expression: `.a[1]`,
2020-10-20 04:33:20 +00:00
expected: []string{
2020-10-21 01:54:58 +00:00
"D0, P[a 1], (!!null)::null\n",
2020-10-20 04:33:20 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
description: "Wildcard matching",
document: `{a: {cat: apple, mad: things}}`,
expression: `.a."*a*"`,
2020-10-20 04:33:20 +00:00
expected: []string{
"D0, P[a cat], (!!str)::apple\n",
"D0, P[a mad], (!!str)::things\n",
},
},
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-20 04:33:20 +00:00
document: `{a: {cat: {b: 3}, mad: {b: 4}, fad: {c: t}}}`,
expression: `.a."*a*".b`,
expected: []string{
"D0, P[a cat b], (!!int)::3\n",
"D0, P[a mad b], (!!int)::4\n",
2020-10-21 01:54:58 +00:00
"D0, P[a fad b], (!!null)::null\n",
2020-10-20 04:33:20 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-20 04:33:20 +00:00
document: `{a: {cat: apple, mad: things}}`,
expression: `.a | (.cat, .mad)`,
expected: []string{
"D0, P[a cat], (!!str)::apple\n",
"D0, P[a mad], (!!str)::things\n",
},
},
2020-10-20 05:27:30 +00:00
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-20 05:27:30 +00:00
document: `{a: {cat: apple, mad: things}}`,
expression: `.a | (.cat, .mad, .fad)`,
expected: []string{
"D0, P[a cat], (!!str)::apple\n",
"D0, P[a mad], (!!str)::things\n",
2020-10-21 01:54:58 +00:00
"D0, P[a fad], (!!null)::null\n",
2020-10-20 05:27:30 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-20 05:27:30 +00:00
document: `{a: {cat: apple, mad: things}}`,
expression: `.a | (.cat, .mad, .fad) | select( (. == null) | not)`,
expected: []string{
"D0, P[a cat], (!!str)::apple\n",
"D0, P[a mad], (!!str)::things\n",
},
},
2020-10-29 23:56:45 +00:00
{
2020-11-17 23:32:30 +00:00
description: "Aliases",
document: `{a: &cat {c: frog}, b: *cat}`,
expression: `.b`,
2020-10-29 23:56:45 +00:00
expected: []string{
"D0, P[b], (alias)::*cat\n",
},
},
{
2020-11-17 23:32:30 +00:00
description: "Traversing aliases with splat",
document: `{a: &cat {c: frog}, b: *cat}`,
2020-12-27 12:00:46 +00:00
expression: `.b[]`,
expected: []string{
"D0, P[a c], (!!str)::frog\n",
2020-10-29 23:56:45 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
description: "Traversing aliases explicitly",
document: `{a: &cat {c: frog}, b: *cat}`,
expression: `.b.c`,
2020-10-29 23:56:45 +00:00
expected: []string{
"D0, P[a c], (!!str)::frog\n",
2020-10-29 23:56:45 +00:00
},
},
2020-11-03 23:48:43 +00:00
{
2020-11-17 23:32:30 +00:00
description: "Traversing arrays by index",
document: `[1,2,3]`,
expression: `.[0]`,
2020-11-03 23:48:43 +00:00
expected: []string{
"D0, P[0], (!!int)::1\n",
},
},
2021-06-09 22:31:26 +00:00
{
2021-06-09 22:35:41 +00:00
description: "Traversing nested arrays by index",
dontFormatInputForDoc: true,
document: `[[], [cat]]`,
expression: `.[1][0]`,
2021-06-09 22:31:26 +00:00
expected: []string{
"D0, P[1 0], (!!str)::cat\n",
},
},
2020-11-03 23:48:43 +00:00
{
2020-11-17 23:32:30 +00:00
description: "Maps with numeric keys",
document: `{2: cat}`,
expression: `.[2]`,
2020-11-17 23:32:30 +00:00
expected: []string{
"D0, P[2], (!!str)::cat\n",
},
},
{
description: "Maps with non existing numeric keys",
2020-11-03 23:48:43 +00:00
document: `{a: b}`,
expression: `.[0]`,
2020-11-03 23:48:43 +00:00
expected: []string{
"D0, P[0], (!!null)::null\n",
},
},
2020-10-30 01:00:48 +00:00
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-30 01:00:48 +00:00
document: mergeDocSample,
expression: `.foobar`,
expected: []string{
"D0, P[foobar], (!!map)::c: foobar_c\n!!merge <<: *foo\nthing: foobar_thing\n",
},
},
{
2020-11-17 23:32:30 +00:00
description: "Traversing merge anchors",
document: mergeDocSample,
expression: `.foobar.a`,
2020-10-30 01:00:48 +00:00
expected: []string{
"D0, P[foo a], (!!str)::foo_a\n",
2020-10-30 01:00:48 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
description: "Traversing merge anchors with override",
document: mergeDocSample,
expression: `.foobar.c`,
2020-10-30 01:00:48 +00:00
expected: []string{
"D0, P[foo c], (!!str)::foo_c\n",
2020-10-30 01:00:48 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
description: "Traversing merge anchors with local override",
document: mergeDocSample,
expression: `.foobar.thing`,
2020-10-30 01:00:48 +00:00
expected: []string{
"D0, P[foobar thing], (!!str)::foobar_thing\n",
},
},
{
2020-11-17 23:32:30 +00:00
description: "Splatting merge anchors",
document: mergeDocSample,
2020-12-27 12:00:46 +00:00
expression: `.foobar[]`,
expected: []string{
"D0, P[foo c], (!!str)::foo_c\n",
"D0, P[foo a], (!!str)::foo_a\n",
2020-10-30 01:00:48 +00:00
"D0, P[foobar thing], (!!str)::foobar_thing\n",
},
},
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-30 01:00:48 +00:00
document: mergeDocSample,
expression: `.foobarList`,
expected: []string{
"D0, P[foobarList], (!!map)::b: foobarList_b\n!!merge <<: [*foo, *bar]\nc: foobarList_c\n",
},
},
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-30 01:00:48 +00:00
document: mergeDocSample,
expression: `.foobarList.a`,
expected: []string{
"D0, P[foo a], (!!str)::foo_a\n",
2020-10-30 01:00:48 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
description: "Traversing merge anchor lists",
subdescription: "Note that the later merge anchors override previous",
document: mergeDocSample,
expression: `.foobarList.thing`,
2020-10-30 01:00:48 +00:00
expected: []string{
"D0, P[bar thing], (!!str)::bar_thing\n",
2020-10-30 01:00:48 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-30 01:00:48 +00:00
document: mergeDocSample,
expression: `.foobarList.c`,
expected: []string{
"D0, P[foobarList c], (!!str)::foobarList_c\n",
},
},
{
2020-11-17 23:32:30 +00:00
skipDoc: true,
2020-10-30 01:00:48 +00:00
document: mergeDocSample,
expression: `.foobarList.b`,
expected: []string{
"D0, P[bar b], (!!str)::bar_b\n",
2020-10-30 01:00:48 +00:00
},
},
{
2020-11-17 23:32:30 +00:00
description: "Splatting merge anchor lists",
document: mergeDocSample,
2020-12-27 12:00:46 +00:00
expression: `.foobarList[]`,
expected: []string{
"D0, P[bar b], (!!str)::bar_b\n",
"D0, P[foo a], (!!str)::foo_a\n",
"D0, P[bar thing], (!!str)::bar_thing\n",
2020-10-30 01:00:48 +00:00
"D0, P[foobarList c], (!!str)::foobarList_c\n",
},
},
2020-12-26 10:37:08 +00:00
{
skipDoc: true,
document: `[a,b,c]`,
expression: `.[]`,
expected: []string{
"D0, P[0], (!!str)::a\n",
"D0, P[1], (!!str)::b\n",
"D0, P[2], (!!str)::c\n",
},
},
{
skipDoc: true,
document: `[a,b,c]`,
expression: `[]`,
expected: []string{
"D0, P[], (!!seq)::[]\n",
},
},
{
skipDoc: true,
document: `{a: [a,b,c]}`,
expression: `.a[0]`,
expected: []string{
"D0, P[a 0], (!!str)::a\n",
},
},
{
description: "Select multiple indices",
document: `{a: [a,b,c]}`,
expression: `.a[0, 2]`,
expected: []string{
"D0, P[a 0], (!!str)::a\n",
"D0, P[a 2], (!!str)::c\n",
},
},
{
skipDoc: true,
document: `{a: [a,b,c]}`,
2021-02-03 04:51:26 +00:00
expression: `.a[0, 2]`,
2020-12-26 10:37:08 +00:00
expected: []string{
"D0, P[a 0], (!!str)::a\n",
"D0, P[a 2], (!!str)::c\n",
},
},
{
skipDoc: true,
document: `{a: [a,b,c]}`,
expression: `.a[-1]`,
expected: []string{
"D0, P[a 2], (!!str)::c\n",
2020-12-26 10:37:08 +00:00
},
},
{
skipDoc: true,
document: `{a: [a,b,c]}`,
2021-02-03 04:51:26 +00:00
expression: `.a[-2]`,
2020-12-26 10:37:08 +00:00
expected: []string{
"D0, P[a 1], (!!str)::b\n",
2020-12-26 10:37:08 +00:00
},
},
{
skipDoc: true,
document: `{a: [a,b,c]}`,
expression: `.a[]`,
expected: []string{
"D0, P[a 0], (!!str)::a\n",
"D0, P[a 1], (!!str)::b\n",
"D0, P[a 2], (!!str)::c\n",
},
},
{
skipDoc: true,
document: `{a: [a,b,c]}`,
2021-02-03 04:51:26 +00:00
expression: `.a[]`,
2020-12-26 10:37:08 +00:00
expected: []string{
"D0, P[a 0], (!!str)::a\n",
"D0, P[a 1], (!!str)::b\n",
"D0, P[a 2], (!!str)::c\n",
},
},
{
skipDoc: true,
document: `{a: [a,b,c]}`,
expression: `.a | .[]`,
expected: []string{
"D0, P[a 0], (!!str)::a\n",
"D0, P[a 1], (!!str)::b\n",
"D0, P[a 2], (!!str)::c\n",
},
},
{
skipDoc: true,
document: badAliasSample,
expression: ".steps[]",
expectedError: "can only use merge anchors with maps (!!map), but got !!seq",
},
2020-10-20 02:53:26 +00:00
}
func TestTraversePathOperatorScenarios(t *testing.T) {
for _, tt := range traversePathOperatorScenarios {
testScenario(t, &tt)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "traverse-read", traversePathOperatorScenarios)
2020-10-20 02:53:26 +00:00
}