-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
61 lines (51 loc) · 1.18 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
package fork
import (
"net/http"
"reflect"
)
type Filterer interface {
Filter(string, *http.Request) *Value
Filters(...interface{}) []reflect.Value
}
func NewFilterer(f ...interface{}) Filterer {
return &filterer{filters: reflectFilters(f...)}
}
var nilFilterer = &filterer{}
type filterer struct {
filters []reflect.Value
}
func (fr *filterer) Filter(k string, r *http.Request) *Value {
var v interface{}
v = r.FormValue(k)
for _, fn := range fr.filters {
v = Filter(fn, v)
}
return NewValue(v)
}
func (fr *filterer) Filters(fns ...interface{}) []reflect.Value {
fr.filters = append(fr.filters, reflectFilters(fns...)...)
return fr.filters
}
func reflectFilters(fns ...interface{}) []reflect.Value {
var ret []reflect.Value
for _, fn := range fns {
ret = append(ret, valueFn(fn, isFilter, `1 value, or 1 value and 1 error value`))
}
return ret
}
func isFilter(typ reflect.Type) bool {
switch {
case typ.NumOut() == 1:
return true
case typ.NumOut() == 2 && typ.Out(1) == errorType:
return true
}
return false
}
func Filter(fn reflect.Value, args ...interface{}) interface{} {
filtered, err := call(fn, args...)
if err != nil {
return err
}
return filtered
}