walk: handle channels
This commit is contained in:
parent
574532f484
commit
ab7818e9ea
@ -27,6 +27,14 @@ func Walk(x interface{}, fn func(string)) {
|
|||||||
for _, key := range val.MapKeys() {
|
for _, key := range val.MapKeys() {
|
||||||
walkValue(val.MapIndex(key))
|
walkValue(val.MapIndex(key))
|
||||||
}
|
}
|
||||||
|
case reflect.Chan:
|
||||||
|
for {
|
||||||
|
if v, ok := val.Recv(); ok {
|
||||||
|
walkValue(v)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,6 +98,23 @@ func TestWalk(t *testing.T) {
|
|||||||
assertContains(t, got, "meuh")
|
assertContains(t, got, "meuh")
|
||||||
assertContains(t, got, "meh")
|
assertContains(t, got, "meh")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("with channels", func(t *testing.T) {
|
||||||
|
aChannel := make(chan Profile)
|
||||||
|
go func() {
|
||||||
|
aChannel <- Profile{21, "Berlin"}
|
||||||
|
aChannel <- Profile{28, "Beijing"}
|
||||||
|
close(aChannel)
|
||||||
|
}()
|
||||||
|
var got []string
|
||||||
|
want := []string{"Berlin", "Beijing"}
|
||||||
|
Walk(aChannel, 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) {
|
func assertContains(t testing.TB, haystack []string, needle string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user