forked from AFusco/golang-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
66 lines (54 loc) · 1.13 KB
/
filters.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
package gtb
// Filter is some thing that does filtering for
// incoming updates.
//
// Return false if you wish to sieve the update out.
type Filter interface {
Filter(*Update) bool
}
// FilterFunc is basically a lightweight version of Filter.
type FilterFunc func(*Update) bool
func NewChain(parent Poller) *Chain {
c := &Chain{}
c.Poller = parent
c.Filter = func(upd *Update) bool {
for _, filter := range c.Filters {
switch f := filter.(type) {
case Filter:
if !f.Filter(upd) {
return false
}
case FilterFunc:
if !f(upd) {
return false
}
case func(*Update) bool:
if !f(upd) {
return false
}
}
}
return true
}
return c
}
// Chain is a chain of middle
type Chain struct {
MiddlewarePoller
// (Filter | FilterFunc | func(*Update) bool)
Filters []interface{}
}
// Add accepts either Filter interface or FilterFunc
func (c *Chain) Add(filter interface{}) {
switch filter.(type) {
case Filter:
break
case FilterFunc:
break
case func(*Update) bool:
break
default:
panic("golang-telegram-bot: unsupported filter type")
}
c.Filters = append(c.Filters, filter)
}