From 37f29286766d812b219322e4cf6b82ac9e5701e4 Mon Sep 17 00:00:00 2001 From: Alessandro Rinaldi Date: Fri, 25 Nov 2022 22:48:16 +0100 Subject: [PATCH] fix: support struct pointers (#370) Fix panic when handling a pointer to a struct. Fixes #369 --- bug_test.go | 28 ++++++++++++++++++++++++++++ runtime.go | 3 +++ 2 files changed, 31 insertions(+) diff --git a/bug_test.go b/bug_test.go index 604bedaf..c66771d7 100644 --- a/bug_test.go +++ b/bug_test.go @@ -773,3 +773,31 @@ func Test_issue266(t *testing.T) { }) } + +func Test_issue369 (t *testing.T) { + tt(t, func() { + test, tester := test() + + type Test struct { + Value string + } + + type PtrTest struct { + *Test + } + + testItem := Test{ + Value: "A test value", + } + + ptrTestItem := PtrTest{ + Test: &testItem, + } + + tester.Set("testVariable", ptrTestItem) + + test(` + JSON.stringify(testVariable); + `, `{"Test":{"Value":"A test value"}}`) + }) +} diff --git a/runtime.go b/runtime.go index 9705b91b..9ede7c07 100644 --- a/runtime.go +++ b/runtime.go @@ -291,6 +291,9 @@ func (self *_runtime) convertNumeric(v Value, t reflect.Type) reflect.Value { } func fieldIndexByName(t reflect.Type, name string) []int { + for t.Kind() == reflect.Ptr { + t = reflect.ValueOf(t).Elem().Type() + } for i := 0; i < t.NumField(); i++ { f := t.Field(i)