walk: handle functions...

This commit is contained in:
Muyao CHEN 2024-09-20 21:04:25 +02:00
parent ab7818e9ea
commit a0bad4d6e9
2 changed files with 20 additions and 0 deletions

View File

@ -35,6 +35,11 @@ func Walk(x interface{}, fn func(string)) {
break
}
}
case reflect.Func:
valFnResult := val.Call(nil)
for _, res := range valFnResult {
walkValue(res)
}
}
}

View File

@ -115,6 +115,21 @@ func TestWalk(t *testing.T) {
t.Errorf("got %v want %v", got, want)
}
})
t.Run("with functions", func(t *testing.T) {
aFunction := func() (Profile, Profile) {
return Profile{21, "Berlin"}, Profile{28, "Beijing"}
}
var got []string
want := []string{"Berlin", "Beijing"}
Walk(aFunction, func(input string) {
got = append(got, input)
})
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
func assertContains(t testing.TB, haystack []string, needle string) {