Newer
Older
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import (
"fmt"
"math"
"strconv"
"strings"
"sync"
)
// TODO add generic filters for various different kinds of matching.
// Used to filter.
// Op can be any of the following:
// The usual relative operators: <, >, <=, >=, ==, != (where applicable)
// A range parameter (see: https://help.github.com/articles/search-syntax/)
type FilterData struct {
Field string `json:"field"`
Op string `json:"op"`
Value string `json:"value"`
}
// Filters based on fields.
type Filter interface {
Match(v interface{}) bool
}
// A filter that can be configured with in-data.
type ConfigurableFilter interface {
Filter
Configure(*FilterData) error
}
// Filter made up of many filters.
type CompositeFilter struct {
filters []Filter
}
func (this *CompositeFilter) SetData(filters []Filter) {
this.filters = filters
}
func (this *CompositeFilter) Match(v interface{}) bool {
for _, f := range this.filters {
if !f.Match(v) {
return false
}
}
return true
}
// Rubberstamps everything.
type MatchAllFilter struct{}
func (this *MatchAllFilter) Match(v interface{}) bool { return true }
// Used to generate filters based on filter data.
// Keeping separate pools for "edge cases" (Composite and MatchAll)
type FilterFactory struct {
filterPools map[string]*sync.Pool
compositeFilterPool *sync.Pool
matchAllFilterPool *sync.Pool
}
func NewFilterFactory() *FilterFactory {
aff := &FilterFactory{}
// Match all.
aff.matchAllFilterPool = &sync.Pool{
New: func() interface{} {
return &MatchAllFilter{}
},
}
// Composite.
aff.compositeFilterPool = &sync.Pool{
New: func() interface{} {
return &CompositeFilter{}
},
}
// Regular.
aff.filterPools = make(map[string]*sync.Pool)
return aff
}
func (this *FilterFactory) RegisterFilterPool(fieldName string, pool *sync.Pool) {
this.filterPools[strings.ToLower(fieldName)] = pool
}
// Creates a new filter given the input data array. If the array is zero length or nil, an empty
// filter will be returned that returns true on all matches. If the array is of size 1, a regular
// filter is returned, otherwise a CompositeFieldFilter is returned, which is a special filter that
// contains a number of other filters. It implements AccountFieldFilter, and will match an account
// only if all the sub-filters matches.
func (this *FilterFactory) NewFilter(fdArr []*FilterData) (Filter, error) {
if fdArr == nil || len(fdArr) == 0 {
return &MatchAllFilter{}, nil
}
if len(fdArr) == 1 {
return this.newSingleFilter(fdArr[0])
}
filters := []Filter{}
for _, fd := range fdArr {
f, err := this.newSingleFilter(fd)
if err != nil {
return nil, err
}
filters = append(filters, f)
}
cf := this.compositeFilterPool.Get().(*CompositeFilter)
cf.filters = filters
return cf, nil
}
func (this *FilterFactory) newSingleFilter(fd *FilterData) (ConfigurableFilter, error) {
fp, ok := this.filterPools[strings.ToLower(fd.Field)]
if !ok {
return nil, fmt.Errorf("Field is not supported: " + fd.Field)
}
f := fp.Get().(ConfigurableFilter)
err := f.Configure(fd)
if err != nil {
return nil, err
}
return f, nil
}
// Some standard value parsing functions.
func ParseNumberValue(value string) (int64, error) {
var val int64
// Check for wildcards.
if value == "min" {
val = math.MinInt64
} else if value == "max" {
val = math.MaxInt64
} else {
tv, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return 0, fmt.Errorf("Wrong value type.")
}
val = tv
}
return val, nil
}
// Some standard filtering functions.
func GetRangeFilter(op, fName string) (func(a, b int64) bool, error) {
if op == "==" {
return func(a, b int64) bool {
return a == b
}, nil
} else if op == "!=" {
return func(a, b int64) bool {
return a != b
}, nil
} else if op == "<=" {
return func(a, b int64) bool {
return a <= b
}, nil
} else if op == ">=" {
return func(a, b int64) bool {
return a >= b
}, nil
} else if op == "<" {
return func(a, b int64) bool {
return a < b
}, nil
} else if op == ">" {
return func(a, b int64) bool {
return a > b
}, nil
} else {
return nil, fmt.Errorf("Op: " + op + " is not supported for '" + fName + "' filtering")
}
}
func GetStringFilter(op, fName string) (func(s0, s1 string) bool, error) {
if op == "==" {
return func(s0, s1 string) bool {
return strings.EqualFold(s0, s1)
}, nil
} else if op == "!=" {
return func(s0, s1 string) bool {
return !strings.EqualFold(s0, s1)
}, nil
} else {
return nil, fmt.Errorf("Op: " + op + " is not supported for '" + fName + "' filtering.")
}
}