mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-04 19:35:38 +00:00
Adding negative parent example
This commit is contained in:
parent
4973c355e6
commit
64ec1f4aa7
@ -80,7 +80,7 @@ will output
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Get the top (root) parent
|
## Get the top (root) parent
|
||||||
Use negative numbers to get the top parents
|
Use negative numbers to get the top parents. You can think of this as indexing into the 'parents' array above
|
||||||
|
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
@ -156,6 +156,25 @@ a:
|
|||||||
c: cat
|
c: cat
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## N-th negative
|
||||||
|
Similarly, use negative numbers to index backwards from the parents array
|
||||||
|
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
a:
|
||||||
|
b:
|
||||||
|
c: cat
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq '.a.b.c | parent(-2)' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
b:
|
||||||
|
c: cat
|
||||||
|
```
|
||||||
|
|
||||||
## No parent
|
## No parent
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
@ -40,7 +40,7 @@ var parentOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Get the top (root) parent",
|
description: "Get the top (root) parent",
|
||||||
subdescription: "Use negative numbers to get the top parents",
|
subdescription: "Use negative numbers to get the top parents. You can think of this as indexing into the 'parents' array above",
|
||||||
document: "a:\n b:\n c: cat\n",
|
document: "a:\n b:\n c: cat\n",
|
||||||
expression: `.a.b.c | parent(-1)`,
|
expression: `.a.b.c | parent(-1)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -56,15 +56,6 @@ var parentOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!map)::a:\n b:\n c: cat\n",
|
"D0, P[], (!!map)::a:\n b:\n c: cat\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
description: "N-th negative",
|
|
||||||
skipDoc: true,
|
|
||||||
document: "a:\n b:\n c: cat\n",
|
|
||||||
expression: `.a.b.c | parent(-2)`,
|
|
||||||
expected: []string{
|
|
||||||
"D0, P[a], (!!map)::b:\n c: cat\n",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
description: "boundary negative",
|
description: "boundary negative",
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
@ -116,6 +107,15 @@ var parentOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!map)::a:\n b:\n c: cat\n",
|
"D0, P[], (!!map)::a:\n b:\n c: cat\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "N-th negative",
|
||||||
|
subdescription: "Similarly, use negative numbers to index backwards from the parents array",
|
||||||
|
document: "a:\n b:\n c: cat\n",
|
||||||
|
expression: `.a.b.c | parent(-2)`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[a], (!!map)::b:\n c: cat\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "No parent",
|
description: "No parent",
|
||||||
document: `{}`,
|
document: `{}`,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user