Skip to content

Commit 64ec1f4

Browse files
committed
Adding negative parent example
1 parent 4973c35 commit 64ec1f4

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

pkg/yqlib/doc/operators/parent.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ will output
8080
```
8181
8282
## Get the top (root) parent
83-
Use negative numbers to get the top parents
83+
Use negative numbers to get the top parents. You can think of this as indexing into the 'parents' array above
8484
8585
Given a sample.yml file of:
8686
```yaml
@@ -156,6 +156,25 @@ a:
156156
c: cat
157157
```
158158
159+
## N-th negative
160+
Similarly, use negative numbers to index backwards from the parents array
161+
162+
Given a sample.yml file of:
163+
```yaml
164+
a:
165+
b:
166+
c: cat
167+
```
168+
then
169+
```bash
170+
yq '.a.b.c | parent(-2)' sample.yml
171+
```
172+
will output
173+
```yaml
174+
b:
175+
c: cat
176+
```
177+
159178
## No parent
160179
Given a sample.yml file of:
161180
```yaml

pkg/yqlib/operator_parent_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var parentOperatorScenarios = []expressionScenario{
4040
},
4141
{
4242
description: "Get the top (root) parent",
43-
subdescription: "Use negative numbers to get the top parents",
43+
subdescription: "Use negative numbers to get the top parents. You can think of this as indexing into the 'parents' array above",
4444
document: "a:\n b:\n c: cat\n",
4545
expression: `.a.b.c | parent(-1)`,
4646
expected: []string{
@@ -56,15 +56,6 @@ var parentOperatorScenarios = []expressionScenario{
5656
"D0, P[], (!!map)::a:\n b:\n c: cat\n",
5757
},
5858
},
59-
{
60-
description: "N-th negative",
61-
skipDoc: true,
62-
document: "a:\n b:\n c: cat\n",
63-
expression: `.a.b.c | parent(-2)`,
64-
expected: []string{
65-
"D0, P[a], (!!map)::b:\n c: cat\n",
66-
},
67-
},
6859
{
6960
description: "boundary negative",
7061
skipDoc: true,
@@ -116,6 +107,15 @@ var parentOperatorScenarios = []expressionScenario{
116107
"D0, P[], (!!map)::a:\n b:\n c: cat\n",
117108
},
118109
},
110+
{
111+
description: "N-th negative",
112+
subdescription: "Similarly, use negative numbers to index backwards from the parents array",
113+
document: "a:\n b:\n c: cat\n",
114+
expression: `.a.b.c | parent(-2)`,
115+
expected: []string{
116+
"D0, P[a], (!!map)::b:\n c: cat\n",
117+
},
118+
},
119119
{
120120
description: "No parent",
121121
document: `{}`,

0 commit comments

Comments
 (0)