Skip to content

Commit 5df7107

Browse files
committed
Fixed index out of range error
1 parent cc7738b commit 5df7107

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ require (
3030
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
3131
)
3232

33-
go 1.21
33+
go 1.21.0
34+
3435
toolchain go1.22.5

pkg/yqlib/expression_parser_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ func getExpressionParser() ExpressionParserInterface {
1111
return ExpressionParser
1212
}
1313

14+
func TestParserCreateMapColonOnItsOwn(t *testing.T) {
15+
_, err := getExpressionParser().ParseExpression(":")
16+
test.AssertResultComplex(t, "':' expects 2 args but there is 0", err.Error())
17+
}
18+
1419
func TestParserNoMatchingCloseBracket(t *testing.T) {
1520
_, err := getExpressionParser().ParseExpression(".cat | with(.;.bob")
1621
test.AssertResultComplex(t, "bad expression - probably missing close bracket on WITH", err.Error())

pkg/yqlib/lexer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func handleToken(tokens []*token, index int, postProcessedTokens []*token) (toke
126126
if tokenIsOpType(currentToken, createMapOpType) {
127127
log.Debugf("tokenIsOpType: createMapOpType")
128128
// check the previous token is '[', means we are slice, but dont have a first number
129-
if tokens[index-1].TokenType == traverseArrayCollect {
129+
if index > 0 && tokens[index-1].TokenType == traverseArrayCollect {
130130
log.Debugf("previous token is : traverseArrayOpType")
131131
// need to put the number 0 before this token, as that is implied
132132
postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: createValueOperation(0, "0")})

0 commit comments

Comments
 (0)