Fixed a panic which was thrown when trying to dereference an nil object

This commit is contained in:
Leon Mika 2024-05-24 04:07:53 +00:00
parent da00d9c723
commit 505fd29032
2 changed files with 8 additions and 0 deletions

View file

@ -161,6 +161,10 @@ func fromGoValue(v any) (object, error) {
}
func fromGoReflectValue(resVal reflect.Value) (object, error) {
if !resVal.IsValid() {
return nil, nil
}
switch resVal.Kind() {
case reflect.Slice:
return listableProxyObject{resVal}, nil

View file

@ -531,6 +531,9 @@ func TestBuiltins_Index(t *testing.T) {
{desc: "go list 1", expr: `goList | index 0 This`, want: "thing 1\n"},
{desc: "go list 2", expr: `goList | index 1 This`, want: "thing 2\n"},
{desc: "go list 3", expr: `goList | index 2`, want: "(nil)\n"},
{desc: "go list 4", expr: `goList | index 2 This`, want: "(nil)\n"},
{desc: "go list 5", expr: `goList | index 30`, want: "(nil)\n"},
{desc: "go struct 1", expr: `goStruct | index Alpha`, want: "foo\n"},
{desc: "go struct 2", expr: `goStruct | index Beta`, want: "bar\n"},
@ -562,6 +565,7 @@ func TestBuiltins_Index(t *testing.T) {
return []*nest{
{This: "thing 1"},
{This: "thing 2"},
nil,
}, nil
})
inst.SetBuiltin("goStruct", func(ctx context.Context, args CallArgs) (any, error) {