diff --git a/walk/walk.go b/walk/walk.go index 1994b84..204e5ad 100644 --- a/walk/walk.go +++ b/walk/walk.go @@ -7,8 +7,14 @@ import ( func Walk(x interface{}, fn func(string)) { val := reflect.ValueOf(x) for i := 0; i < val.NumField(); i++ { - if val.Field(i).Kind() == reflect.String { - fn(val.Field(i).String()) + field := val.Field(i) + + switch field.Kind() { + case reflect.String: + fn(field.String()) + case reflect.Struct: + // XXX: Interface() to get interface + Walk(field.Interface(), fn) } } } diff --git a/walk/walk_test.go b/walk/walk_test.go index d7aa56b..c4c5d1c 100644 --- a/walk/walk_test.go +++ b/walk/walk_test.go @@ -34,6 +34,20 @@ func TestWalk(t *testing.T) { }{"Chris", 29}, []string{"Chris"}, }, + { + "nested fields", + struct { + Name string + Profile struct { + Age int + City string + } + }{"Chris", struct { + Age int + City string + }{29, "London"}}, + []string{"Chris", "London"}, + }, } for _, test := range cases {