mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-01 14:14:52 +08:00
Improving select docs (#839)
This commit is contained in:
@@ -28,7 +28,17 @@ var selectOperatorScenarios = []expressionScenario{
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
description: "Select elements from array",
|
||||
description: "Select elements from array with regular expression",
|
||||
subdescription: "See more regular expression examples under the `string` operator docs.",
|
||||
document: `[this_0, not_this, nor_0_this, thisTo_4]`,
|
||||
expression: `.[] | select(test("[a-zA-Z]+_[0-9]$"))`,
|
||||
expected: []string{
|
||||
"D0, P[0], (!!str)::this_0\n",
|
||||
"D0, P[3], (!!str)::thisTo_4\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Select elements from array using wildcard prefix",
|
||||
document: `[cat,goat,dog]`,
|
||||
expression: `.[] | select(. == "*at")`,
|
||||
expected: []string{
|
||||
@@ -36,6 +46,25 @@ var selectOperatorScenarios = []expressionScenario{
|
||||
"D0, P[1], (!!str)::goat\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Select elements from array using wildcard suffix",
|
||||
document: `[go-kart,goat,dog]`,
|
||||
expression: `.[] | select(. == "go*")`,
|
||||
expected: []string{
|
||||
"D0, P[0], (!!str)::go-kart\n",
|
||||
"D0, P[1], (!!str)::goat\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Select elements from array using wildcard prefix and suffix",
|
||||
document: `[ago, go, meow, going]`,
|
||||
expression: `.[] | select(. == "*go*")`,
|
||||
expected: []string{
|
||||
"D0, P[0], (!!str)::ago\n",
|
||||
"D0, P[1], (!!str)::go\n",
|
||||
"D0, P[3], (!!str)::going\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: "a: hello",
|
||||
@@ -69,9 +98,27 @@ var selectOperatorScenarios = []expressionScenario{
|
||||
"D0, P[a 1], (!!str)::goat\n"},
|
||||
},
|
||||
{
|
||||
description: "Select and update matching values in map",
|
||||
document: `a: { things: cat, bob: goat, horse: dog }`,
|
||||
expression: `(.a.[] | select(. == "*at")) |= "rabbit"`,
|
||||
description: "Select items from a map",
|
||||
document: `{ things: cat, bob: goat, horse: dog }`,
|
||||
expression: `.[] | select(. == "cat" or test("og$"))`,
|
||||
expected: []string{
|
||||
"D0, P[things], (!!str)::cat\n",
|
||||
"D0, P[horse], (!!str)::dog\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Use select + with_entries to filter map keys",
|
||||
document: `{name: bob, legs: 2, game: poker}`,
|
||||
expression: `with_entries(select(.key | test("ame$")))`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!map)::name: bob\ngame: poker\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Select multiple items in a map and update",
|
||||
subdescription: "Note the brackets around the entire LHS.",
|
||||
document: `a: { things: cat, bob: goat, horse: dog }`,
|
||||
expression: `(.a.[] | select(. == "cat" or . == "goat")) |= "rabbit"`,
|
||||
expected: []string{
|
||||
"D0, P[], (doc)::a: {things: rabbit, bob: rabbit, horse: dog}\n",
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user