Fix sliceStringNode signature and fix test descriptions/expressions

Agent-Logs-Url: https://github.com/mikefarah/yq/sessions/58726b13-68ae-4f93-971f-eb70459edcf4

Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-03-28 02:01:01 +00:00 committed by GitHub
parent 6d345ac795
commit 9a9399ad00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 15 deletions

View File

@ -110,11 +110,11 @@ country: Australia
```
then
```bash
yq '.country[4:]' sample.yml
yq '.country[0:5]' sample.yml
```
will output
```yaml
ralia
Austr
```
## Slicing strings - without the second number
@ -126,11 +126,11 @@ country: Australia
```
then
```bash
yq '.country[0:5]' sample.yml
yq '.country[5:]' sample.yml
```
will output
```yaml
Austr
alia
```
## Slicing strings - without the first number

View File

@ -16,7 +16,7 @@ func getSliceNumber(d *dataTreeNavigator, context Context, node *CandidateNode,
return parseInt(result.MatchingNodes.Front().Value.(*CandidateNode).Value)
}
func sliceStringNode(lhsNode *CandidateNode, firstNumber int, secondNumber int) (*CandidateNode, error) {
func sliceStringNode(lhsNode *CandidateNode, firstNumber int, secondNumber int) *CandidateNode {
runes := []rune(lhsNode.Value)
length := len(runes)
@ -45,7 +45,7 @@ func sliceStringNode(lhsNode *CandidateNode, firstNumber int, secondNumber int)
}
slicedString := string(runes[relativeFirstNumber:relativeSecondNumber])
return lhsNode.CreateReplacement(ScalarNode, "!!str", slicedString), nil
return lhsNode.CreateReplacement(ScalarNode, "!!str", slicedString)
}
func sliceArrayOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
@ -70,11 +70,7 @@ func sliceArrayOperator(d *dataTreeNavigator, context Context, expressionNode *E
}
if lhsNode.Kind == ScalarNode && lhsNode.guessTagFromCustomType() == "!!str" {
slicedNode, err := sliceStringNode(lhsNode, firstNumber, secondNumber)
if err != nil {
return Context{}, err
}
results.PushBack(slicedNode)
results.PushBack(sliceStringNode(lhsNode, firstNumber, secondNumber))
continue
}

View File

@ -101,18 +101,18 @@ var sliceArrayScenarios = []expressionScenario{
{
description: "Slicing strings",
document: `country: Australia`,
expression: `.country[4:]`,
expression: `.country[0:5]`,
expected: []string{
"D0, P[country], (!!str)::ralia\n",
"D0, P[country], (!!str)::Austr\n",
},
},
{
description: "Slicing strings - without the second number",
subdescription: "Finishes at the end of the string",
document: `country: Australia`,
expression: `.country[0:5]`,
expression: `.country[5:]`,
expected: []string{
"D0, P[country], (!!str)::Austr\n",
"D0, P[country], (!!str)::alia\n",
},
},
{

View File

@ -303,3 +303,4 @@ ralia
Austr
ustrali
héllo
alia