Skip to content

Commit

Permalink
Fixed bug in splice operator #1511
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Jan 13, 2023
1 parent d7da0cc commit 3b84c03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/yqlib/operator_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func sliceArrayOperator(d *dataTreeNavigator, context Context, expressionNode *E
relativeSecondNumber := secondNumber
if relativeSecondNumber < 0 {
relativeSecondNumber = len(original.Content) + secondNumber
} else if relativeSecondNumber > len(original.Content) {
relativeSecondNumber = len(original.Content)
}

log.Debug("calculateIndicesToTraverse: slice from %v to %v", relativeFirstNumber, relativeSecondNumber)
Expand Down
18 changes: 18 additions & 0 deletions pkg/yqlib/operator_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ var sliceArrayScenarios = []expressionScenario{
"D0, P[1], (!!seq)::- banana\n- grape\n",
},
},
{
skipDoc: true,
description: "second index beyond array clamps",
document: `[cat]`,
expression: `.[:3]`,
expected: []string{
"D0, P[], (!!seq)::- cat\n",
},
},
{
skipDoc: true,
description: "first index beyond array returns nothing",
document: `[cat]`,
expression: `.[3:]`,
expected: []string{
"D0, P[], (!!seq)::[]\n",
},
},
{
skipDoc: true,
document: `[[cat, dog, frog, cow], [apple, banana, grape, mango]]`,
Expand Down

0 comments on commit 3b84c03

Please sign in to comment.