diff --git a/salix.go b/salix.go index 4892522..d41114a 100644 --- a/salix.go +++ b/salix.go @@ -415,7 +415,7 @@ func (t *Template) getIndex(i ast.Index, local map[string]any) (any, error) { return nil, ast.PosError(i, "%s: cannot get index of nil value", valueToString(i)) } rindex := reflect.ValueOf(index) - if !rval.IsValid() { + if !rindex.IsValid() { return nil, ast.PosError(i, "%s: cannot use nil value as an index", valueToString(i)) } diff --git a/structs_test.go b/structs_test.go index 588c2ee..6ce6573 100644 --- a/structs_test.go +++ b/structs_test.go @@ -107,6 +107,23 @@ func TestGetIndexNil(t *testing.T) { } } +func TestGetIndexNilIndex(t *testing.T) { + testMap := map[string]any{} + tmpl := testTmpl(t) + + // test[index] + ast := ast.Index{ + Value: ast.Ident{Value: "test", Position: testPos(t)}, + Index: ast.Ident{Value: "index", Position: testPos(t)}, + Position: testPos(t), + } + + _, err := tmpl.getIndex(ast, map[string]any{"test": testMap, "index": nil}) + if err == nil { + t.Error("Expected error, got nil") + } +} + func TestGetField(t *testing.T) { testStruct := struct { X int