diff --git a/pkg/yqlib/doc/operators/slice-array.md b/pkg/yqlib/doc/operators/slice-array.md index 800108d2..aee24811 100644 --- a/pkg/yqlib/doc/operators/slice-array.md +++ b/pkg/yqlib/doc/operators/slice-array.md @@ -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 diff --git a/pkg/yqlib/operator_slice.go b/pkg/yqlib/operator_slice.go index c17ed259..d5191b54 100644 --- a/pkg/yqlib/operator_slice.go +++ b/pkg/yqlib/operator_slice.go @@ -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 } diff --git a/pkg/yqlib/operator_slice_test.go b/pkg/yqlib/operator_slice_test.go index 16e796eb..c4a6b783 100644 --- a/pkg/yqlib/operator_slice_test.go +++ b/pkg/yqlib/operator_slice_test.go @@ -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", }, }, { diff --git a/project-words.txt b/project-words.txt index ed57a37c..8f2fcf9e 100644 --- a/project-words.txt +++ b/project-words.txt @@ -303,3 +303,4 @@ ralia Austr ustrali héllo +alia