diff --git a/pkg/yqlib/doc/operators/parent.md b/pkg/yqlib/doc/operators/parent.md index 7870aa3f..fc474404 100644 --- a/pkg/yqlib/doc/operators/parent.md +++ b/pkg/yqlib/doc/operators/parent.md @@ -80,7 +80,7 @@ will output ``` ## 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: ```yaml @@ -156,6 +156,25 @@ a: 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 Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/operator_parent_test.go b/pkg/yqlib/operator_parent_test.go index 8a1eb700..b58d603c 100644 --- a/pkg/yqlib/operator_parent_test.go +++ b/pkg/yqlib/operator_parent_test.go @@ -40,7 +40,7 @@ var parentOperatorScenarios = []expressionScenario{ }, { 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", expression: `.a.b.c | parent(-1)`, expected: []string{ @@ -56,15 +56,6 @@ var parentOperatorScenarios = []expressionScenario{ "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", skipDoc: true, @@ -116,6 +107,15 @@ var parentOperatorScenarios = []expressionScenario{ "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", document: `{}`,