-
Notifications
You must be signed in to change notification settings - Fork 40
/
filter.go
160 lines (132 loc) · 2.68 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package producer
import "context"
// Filter selects/rejects items received from a source.
type Filter interface {
// Count corrects the number of total items to test
Count(ctx context.Context, in <-chan int) <-chan int
// Select filters the items
Select(ctx context.Context, in <-chan []string) <-chan []string
}
// FilterSkip skips the first n values sent over the channel.
type FilterSkip struct {
Skip int
}
// ensure Filter Skip implements Filter
var _ Filter = &FilterSkip{}
// Count filters the number of values.
func (f *FilterSkip) Count(ctx context.Context, in <-chan int) <-chan int {
out := make(chan int, 1)
go func() {
defer close(out)
var total int
select {
case total = <-in:
case <-ctx.Done():
}
// calculate the correct total count
if total < f.Skip {
total = 0
} else {
total -= f.Skip
}
select {
case out <- total:
case <-ctx.Done():
}
}()
return out
}
// Select filters values sent over ch.
func (f *FilterSkip) Select(ctx context.Context, in <-chan []string) <-chan []string {
out := make(chan []string)
go func() {
defer close(out)
var cur int
for {
var v []string
var ok bool
select {
case <-ctx.Done():
return
case v, ok = <-in:
// when the input channel is closed we're done
if !ok {
return
}
}
if cur < f.Skip {
cur++
// drop value, receive next
continue
}
select {
case <-ctx.Done():
return
case out <- v:
}
}
}()
return out
}
// FilterLimit passes through at most Max values.
type FilterLimit struct {
Max int
CancelProducer func()
}
// ensure FilterLimit implements Filter.
var _ Filter = &FilterLimit{}
// Count filters the number of values.
func (f *FilterLimit) Count(ctx context.Context, in <-chan int) <-chan int {
out := make(chan int, 1)
go func() {
defer close(out)
var total int
select {
case total = <-in:
case <-ctx.Done():
}
// calculate the correct total count
if total > f.Max {
total = f.Max
}
select {
case out <- total:
case <-ctx.Done():
}
}()
return out
}
// Select filters values sent over ch.
func (f *FilterLimit) Select(ctx context.Context, in <-chan []string) <-chan []string {
out := make(chan []string)
go func() {
defer close(out)
defer f.CancelProducer()
var cur int
for {
var v []string
var ok bool
select {
case <-ctx.Done():
return
case v, ok = <-in:
// when the input channel is closed we're done
if !ok {
return
}
}
if cur >= f.Max {
// cancel producer, drop value, receive next
f.CancelProducer()
continue
}
cur++
select {
case <-ctx.Done():
return
case out <- v:
}
}
}()
return out
}