Skip to content

Conversation

@mikefarah
Copy link
Owner

No description provided.

Copy link
Contributor

Copilot AI left a 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 -1 refers to the root parent, -2 to the second from root, etc.
  • Introduced root operator as a convenient alias for parent(-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.

Comment on lines +77 to +83
{
description: "large positive",
skipDoc: true,
document: "a:\n b:\n c: cat\n",
expression: `.a.b.c | parent(10)`,
expected: []string{},
},
Copy link

Copilot AI Dec 20, 2025

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.

Copilot uses AI. Check for mistakes.
Comment on lines 68 to 76
{
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",
},
},
Copy link

Copilot AI Dec 20, 2025

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 uses AI. Check for mistakes.
@mikefarah
Copy link
Owner Author

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link
Contributor

Copilot AI commented Dec 20, 2025

@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.

@mikefarah mikefarah merged commit 9fa353b into master Dec 20, 2025
5 checks passed
@mikefarah mikefarah deleted the negative-parent-index branch December 20, 2025 04:36
temp = temp.Parent
}
// Convert negative index to positive
// -1 means last parent (root), -2 means second to last, etc.
Copy link
Contributor

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

Comment on lines +82 to +100
## 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
```
Copy link
Contributor

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

Suggested change
## 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants