From 5df71072f5c299cedf687de17c69929259b568cc Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 29 Sep 2024 15:06:34 +1000 Subject: [PATCH] Fixed index out of range error --- go.mod | 3 ++- pkg/yqlib/expression_parser_test.go | 5 +++++ pkg/yqlib/lexer.go | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index f6170872..fd90c31a 100644 --- a/go.mod +++ b/go.mod @@ -30,5 +30,6 @@ require ( golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect ) -go 1.21 +go 1.21.0 + toolchain go1.22.5 diff --git a/pkg/yqlib/expression_parser_test.go b/pkg/yqlib/expression_parser_test.go index 2718a16a..7356762a 100644 --- a/pkg/yqlib/expression_parser_test.go +++ b/pkg/yqlib/expression_parser_test.go @@ -11,6 +11,11 @@ func getExpressionParser() ExpressionParserInterface { return ExpressionParser } +func TestParserCreateMapColonOnItsOwn(t *testing.T) { + _, err := getExpressionParser().ParseExpression(":") + test.AssertResultComplex(t, "':' expects 2 args but there is 0", err.Error()) +} + func TestParserNoMatchingCloseBracket(t *testing.T) { _, err := getExpressionParser().ParseExpression(".cat | with(.;.bob") test.AssertResultComplex(t, "bad expression - probably missing close bracket on WITH", err.Error()) diff --git a/pkg/yqlib/lexer.go b/pkg/yqlib/lexer.go index 904ce756..cc6841b3 100644 --- a/pkg/yqlib/lexer.go +++ b/pkg/yqlib/lexer.go @@ -126,7 +126,7 @@ func handleToken(tokens []*token, index int, postProcessedTokens []*token) (toke if tokenIsOpType(currentToken, createMapOpType) { log.Debugf("tokenIsOpType: createMapOpType") // check the previous token is '[', means we are slice, but dont have a first number - if tokens[index-1].TokenType == traverseArrayCollect { + if index > 0 && tokens[index-1].TokenType == traverseArrayCollect { log.Debugf("previous token is : traverseArrayOpType") // need to put the number 0 before this token, as that is implied postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: createValueOperation(0, "0")})