Conversation
Agent-Logs-Url: https://github.com/mikefarah/yq/sessions/a8525fbb-77a7-4bb0-a3a7-b24f99ae8710 Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
| ``` | ||
| then | ||
| ```bash | ||
| yq '.country[0:5]' sample.yml |
There was a problem hiding this comment.
Test says without the second number, but both numbers are present
pkg/yqlib/operator_slice_test.go
Outdated
| description: "Slicing strings - without the second number", | ||
| subdescription: "Finishes at the end of the string", | ||
| document: `country: Australia`, | ||
| expression: `.country[0:5]`, |
There was a problem hiding this comment.
yep this has the second number...should this be .country[5:] ?
There was a problem hiding this comment.
@copilot fix this ^;
also the build is failing with:
Error: pkg/yqlib/operator_slice.go:19:98: sliceStringNode - result 1 (error) is always nil (unparam)
func sliceStringNode(lhsNode *CandidateNode, firstNumber int, secondNumber int) (*CandidateNode, error) {
There was a problem hiding this comment.
Fixed in 9a9399a:
- Changed "Slicing strings - without the second number" expression from
.country[0:5]to.country[5:](now actually omits the second number, givingalia) - Changed the first "Slicing strings" test to use both numbers:
.country[0:5]→Austr - Removed the always-nil error return from
sliceStringNodeto fix the build warning
Agent-Logs-Url: https://github.com/mikefarah/yq/sessions/58726b13-68ae-4f93-971f-eb70459edcf4 Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds correct string-slicing behavior to yq’s slice operator and fixes traversal so slice operations apply to the evaluated LHS value (e.g. .country[4:] slices the country string, not the root document).
Changes:
- Add rune-based string slicing support and route
!!strscalar nodes through it. - Fix
traverseArrayOperatorto evaluate/pipeline the LHS before applying slice logic. - Extend lexer post-processing to support omitted-start string slicing syntax like
.field[:N].
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| project-words.txt | Adds new words used by tests/docs to the project dictionary. |
| pkg/yqlib/operator_traverse_path.go | Ensures slice operations receive the evaluated LHS context. |
| pkg/yqlib/operator_slice.go | Introduces sliceStringNode and routes string scalars through it. |
| pkg/yqlib/lexer.go | Enables implicit 0 insertion for [:N] after [ (not just after .[). |
| pkg/yqlib/operator_slice_test.go | Adds coverage for string slicing (including Unicode and bounds cases). |
| pkg/yqlib/doc/operators/slice-array.md | Updates generated operator docs to include string slicing examples. |
| pkg/yqlib/doc/operators/headers/slice-array.md | Updates the slice operator header to reflect string support. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| relativeFirstNumber := firstNumber | ||
| if relativeFirstNumber < 0 { | ||
| relativeFirstNumber = len(lhsNode.Content) + firstNumber | ||
| } |
There was a problem hiding this comment.
Array slicing can still panic when the start index is more negative than the array length (e.g. .[-100:]): relativeFirstNumber is adjusted by len(lhsNode.Content)+firstNumber but is not clamped back to 0, so the loop can index lhsNode.Content with a negative i. Clamp relativeFirstNumber (and similarly relativeSecondNumber) into [0,len] before iterating to avoid runtime panics and match the existing out-of-bounds clamping behavior for other cases.
There was a problem hiding this comment.
Fixed in 341e252. Array slice now clamps both relativeFirstNumber and relativeSecondNumber into [0, len(Content)] after adjusting for negative indices, preventing a panic on expressions like .[-100:]. Also added test cases for both directions of out-of-bounds negative indices.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Agent-Logs-Url: https://github.com/mikefarah/yq/sessions/7c146762-d251-45fd-8555-2488f59fc57b Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
yq supported array slicing (
.[1:3]) but not string slicing —.field[4:]on a string value silently returned the wrong result or panicked.Changes
operator_slice.go— AddedsliceStringNode(rune-based for Unicode correctness);sliceArrayOperatornow routes!!strscalar nodes to it instead of the array path.operator_traverse_path.go— FixedtraverseArrayOperatorto evaluate the LHS before callingsliceArrayOperator. Previously it passed the raw document context, so.country[4:]would operate on the whole map rather than thecountryvalue.lexer.go— Extended the implicit-0insertion (for omitted start index in[:N]) to fire onopenCollect([) in addition totraverseArrayCollect(.[), enabling.field[:N]syntax.operator_slice_test.go— Added string slicing scenarios: start-only, end-only, both, negative indices, out-of-bounds clamping, full[:], and Unicode.doc/operators/headers/slice-array.md— Updated header to reflect string support.Example
📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.