-
-
Notifications
You must be signed in to change notification settings - Fork 727
Support negative parent indices #2553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for negative parent indices in the yq parent operator, allowing users to navigate to parents counting from the root instead of from the current node. It also introduces a root operator as an alias for parent(-1).
- Added negative index support where
-1refers to the root parent,-2to the second from root, etc. - Introduced
rootoperator as a convenient alias forparent(-1) - Updated lexer patterns and parameter extraction to accept negative numbers in parent operator syntax
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/yqlib/operator_parent.go | Implements negative index logic by counting total parents and converting negative indices to positive levels |
| pkg/yqlib/operator_parent_test.go | Adds comprehensive test cases for negative indices, root operator, and boundary conditions |
| pkg/yqlib/lexer_participle.go | Updates regex pattern to accept negative numbers and adds root operator as alias for parent(-1) |
| pkg/yqlib/lexer.go | Updates parameter extraction regex to support negative integers |
| pkg/yqlib/doc/operators/parent.md | Documents the new negative index functionality and root operator with examples |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| { | ||
| description: "large positive", | ||
| skipDoc: true, | ||
| document: "a:\n b:\n c: cat\n", | ||
| expression: `.a.b.c | parent(10)`, | ||
| expected: []string{}, | ||
| }, |
Copilot
AI
Dec 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing test coverage for parent(0) edge case. While the implementation appears to handle this correctly (returns the current node when level is 0), there should be a test case to verify this behavior explicitly.
| { | ||
| description: "large negative", | ||
| skipDoc: true, | ||
| document: "a:\n b:\n c: cat\n", | ||
| expression: `.a.b.c | parent(-10)`, | ||
| expected: []string{ | ||
| "D0, P[a b c], (!!str)::cat\n", | ||
| }, | ||
| }, |
Copilot
AI
Dec 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing test coverage for boundary case parent(-3). Given that the path .a.b.c has exactly 3 parents, testing parent(-3) would verify that the boundary case (where the negative index equals the total number of parents) is handled correctly. This should return the immediate parent (c).
|
@copilot open a new pull request to apply changes based on the comments in this thread |
|
@mikefarah I've opened a new pull request, #2555, to work on those changes. Once the pull request is ready, I'll request review from you. |
Co-authored-by: mikefarah <[email protected]>
| temp = temp.Parent | ||
| } | ||
| // Convert negative index to positive | ||
| // -1 means last parent (root), -2 means second to last, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-2 the option is interesting, it could be documented maybe. I feel like you only documented the -1, right ?
I might be wrong
| ## Get the top (root) parent | ||
| Use negative numbers to get the top parents | ||
| Given a sample.yml file of: | ||
| ```yaml | ||
| a: | ||
| b: | ||
| c: cat | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '.a.b.c | parent(-1)' sample.yml | ||
| ``` | ||
| will output | ||
| ```yaml | ||
| a: | ||
| b: | ||
| c: cat | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #2553 (comment)
Maybe this
| ## Get the top (root) parent | |
| Use negative numbers to get the top parents | |
| Given a sample.yml file of: | |
| ```yaml | |
| a: | |
| b: | |
| c: cat | |
| ``` | |
| then | |
| ```bash | |
| yq '.a.b.c | parent(-1)' sample.yml | |
| ``` | |
| will output | |
| ```yaml | |
| a: | |
| b: | |
| c: cat | |
| ``` | |
| ## Get the top (root) parent | |
| Use negative numbers to get the top parents | |
| Given a sample.yml file of: | |
| ```yaml | |
| a: | |
| b: | |
| c: | |
| d: cat | |
| ``` | |
| then | |
| ```bash | |
| yq '.a.b.c | parent(-2)' sample.yml | |
| ``` | |
| will output | |
| ```yaml | |
| b: | |
| c: | |
| d: cat | |
| ``` |
I might be wrong in what -2 would bring, but the usage of parent with negative number would be clearer
This way root usage is clearer
No description provided.