From 3b84c031314b554db9401f9bdbc8cb954a8f8522 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 13 Jan 2023 13:45:19 +1100 Subject: [PATCH] Fixed bug in splice operator #1511 --- pkg/yqlib/operator_slice.go | 2 ++ pkg/yqlib/operator_slice_test.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pkg/yqlib/operator_slice.go b/pkg/yqlib/operator_slice.go index e20311df..e496cf93 100644 --- a/pkg/yqlib/operator_slice.go +++ b/pkg/yqlib/operator_slice.go @@ -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) diff --git a/pkg/yqlib/operator_slice_test.go b/pkg/yqlib/operator_slice_test.go index 9785410b..29a821eb 100644 --- a/pkg/yqlib/operator_slice_test.go +++ b/pkg/yqlib/operator_slice_test.go @@ -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]]`,