From 3849255f52e019e4dfeb9b2ed969bdce4d3ec539 Mon Sep 17 00:00:00 2001 From: Cleiton Marques Souza Date: Thu, 21 May 2020 15:33:13 -0700 Subject: [PATCH 1/4] Adds ToChannelT function --- example_test.go | 18 ++++++++++++++++++ result.go | 16 ++++++++++++++++ result_test.go | 18 ++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/example_test.go b/example_test.go index 4471957..c261c4e 100644 --- a/example_test.go +++ b/example_test.go @@ -1354,6 +1354,24 @@ func ExampleQuery_ToChannel() { // 10 } +// The following code example demonstrates how to use ToChannelT +// to send a slice to a typed channel. +func ExampleQuery_ToChannelT() { + c := make(chan string) + + go func() { + Repeat("ten", 3).ToChannelT(c) + }() + + for i := range c { + fmt.Println(i) + } + // Output: + // ten + // ten + // ten +} + // The following code example demonstrates how to use ToMap to populate a map. func ExampleQuery_ToMap() { type Product struct { diff --git a/result.go b/result.go index 34e47ab..588cbd5 100644 --- a/result.go +++ b/result.go @@ -544,6 +544,22 @@ func (q Query) ToChannel(result chan<- interface{}) { close(result) } +// ToChannelT is the typed version of ToChannel. +// +// - result is of type "chan TSource" +// +// NOTE: ToChannel has better performance than ToChannelT. +func (q Query) ToChannelT(result interface{}) { + r := reflect.ValueOf(result) + next := q.Iterate() + + for item, ok := next(); ok; item, ok = next() { + r.Send(reflect.ValueOf(item)) + } + + r.Close() +} + // ToMap iterates over a collection and populates result map with elements. // Collection elements have to be of KeyValue type to use this method. To // populate a map with elements of different type use ToMapBy method. ToMap diff --git a/result_test.go b/result_test.go index c4899e2..f154452 100644 --- a/result_test.go +++ b/result_test.go @@ -464,6 +464,24 @@ func TestToChannel(t *testing.T) { } } +func TestToChannelT(t *testing.T) { + c := make(chan string) + input := []string{"1", "2", "3", "4", "5"} + + go func() { + From(input).ToChannelT(c) + }() + + result := []string{} + for value := range c { + result = append(result, value) + } + + if !reflect.DeepEqual(result, input) { + t.Errorf("From(%v).ToChannelT()=%v expected %v", input, result, input) + } +} + func TestToMap(t *testing.T) { input := make(map[int]bool) input[1] = true From 3c9e455ede08241a7ab7ecd2f64db30d439f3dc9 Mon Sep 17 00:00:00 2001 From: Cleiton Marques Souza Date: Fri, 5 Jun 2020 09:48:29 -0700 Subject: [PATCH 2/4] Improves ChannelT test --- result_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/result_test.go b/result_test.go index f154452..8faeb6d 100644 --- a/result_test.go +++ b/result_test.go @@ -468,9 +468,7 @@ func TestToChannelT(t *testing.T) { c := make(chan string) input := []string{"1", "2", "3", "4", "5"} - go func() { - From(input).ToChannelT(c) - }() + go From(input).ToChannelT(c) result := []string{} for value := range c { From 3c2e363cb0dd9d156e7e3464ad494cc48710c8f9 Mon Sep 17 00:00:00 2001 From: Cleiton Marques Souza Date: Fri, 5 Jun 2020 09:50:37 -0700 Subject: [PATCH 3/4] Improves ToChannelT example --- example_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/example_test.go b/example_test.go index c261c4e..a1fa225 100644 --- a/example_test.go +++ b/example_test.go @@ -1359,9 +1359,7 @@ func ExampleQuery_ToChannel() { func ExampleQuery_ToChannelT() { c := make(chan string) - go func() { - Repeat("ten", 3).ToChannelT(c) - }() + go Repeat("ten", 3).ToChannelT(c) for i := range c { fmt.Println(i) From bfa7369888af44f4b9df3cc7f832eb99016a1ee3 Mon Sep 17 00:00:00 2001 From: Cleiton Marques Souza Date: Fri, 5 Jun 2020 10:35:19 -0700 Subject: [PATCH 4/4] Forces travis build --- example_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/example_test.go b/example_test.go index a1fa225..41e6c00 100644 --- a/example_test.go +++ b/example_test.go @@ -1296,7 +1296,6 @@ func ExampleQuery_Take() { fmt.Printf("The top three grades are: %v", topThreeGrades) // Output: // The top three grades are: [98 92 85] - } // The following code example demonstrates how to use TakeWhile