waik: work with pointer

This commit is contained in:
Muyao CHEN 2024-09-20 20:19:49 +02:00
parent 30a408c9e4
commit 80959f822b
2 changed files with 22 additions and 7 deletions

View File

@ -6,6 +6,11 @@ import (
func Walk(x interface{}, fn func(string)) {
val := reflect.ValueOf(x)
if val.Kind() == reflect.Pointer {
val = val.Elem() // XXX: the object from the pointer
}
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)

View File

@ -5,6 +5,14 @@ import (
"testing"
)
type Person struct {
Name string
Profile struct {
Age int
City string
}
}
func TestWalk(t *testing.T) {
cases := []struct {
Name string
@ -36,13 +44,15 @@ func TestWalk(t *testing.T) {
},
{
"nested fields",
struct {
Name string
Profile struct {
Person{"Chris", struct {
Age int
City string
}
}{"Chris", struct {
}{29, "London"}},
[]string{"Chris", "London"},
},
{
"pointer to things",
&Person{"Chris", struct {
Age int
City string
}{29, "London"}},