mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-05 00:54:51 +08:00
Updated var to work like jq #934
This commit is contained in:
@@ -34,15 +34,31 @@ var variableOperatorScenarios = []expressionScenario{
|
||||
description: "Using variables as a lookup",
|
||||
subdescription: "Example taken from [jq](https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...)",
|
||||
document: `{"posts": [{"title": "Frist psot", "author": "anon"},
|
||||
{"title": "A well-written article", "author": "person1"}],
|
||||
"realnames": {"anon": "Anonymous Coward",
|
||||
"person1": "Person McPherson"}}`,
|
||||
{"title": "A well-written article", "author": "person1"}],
|
||||
"realnames": {"anon": "Anonymous Coward",
|
||||
"person1": "Person McPherson"}}`,
|
||||
expression: `.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]}`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!map)::title: \"Frist psot\"\nauthor: \"Anonymous Coward\"\n",
|
||||
"D0, P[], (!!map)::title: \"A well-written article\"\nauthor: \"Person McPherson\"\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Using variables to swap values",
|
||||
document: "a: a_value\nb: b_value",
|
||||
expression: `.a as $x | .b as $y | .b = $x | .a = $y`,
|
||||
expected: []string{
|
||||
"D0, P[], (doc)::a: b_value\nb: a_value\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Use ref to reference a path repeatedly",
|
||||
document: `a: {b: thing, c: something}`,
|
||||
expression: `.a.b ref $x | $x = "new" | $x style="double"`,
|
||||
expected: []string{
|
||||
"D0, P[], (doc)::a: {b: \"new\", c: something}\n",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestVariableOperatorScenarios(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user