-
Notifications
You must be signed in to change notification settings - Fork 0
/
selector.go
186 lines (166 loc) · 4.21 KB
/
selector.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
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
package netmap
import (
"strconv"
// used by protoc
_ "github.com/gogo/protobuf/proto"
)
// Check checks is Bucket satisfies filter f.
func (f Filter) Check(b Bucket) bool {
if sf := f.GetF(); sf != nil {
return f.Key == b.Key && sf.Check(b.Value)
}
return false
}
// Check returns result of applying sf to value.
// For numeric comparisons, value is parsed to int64.
func (sf SimpleFilter) Check(value string) bool {
switch sf.Op {
case Operation_OR:
if args := sf.GetFArgs(); args != nil {
result := false
for _, f := range args.Filters {
if result = result || f.Check(value); result {
return result
}
}
return result
}
return true
case Operation_AND:
if args := sf.GetFArgs(); args != nil {
result := true
for _, f := range args.Filters {
if result = result && f.Check(value); !result {
return result
}
}
return result
}
return true
case Operation_NP:
return true
case Operation_EQ:
return value == sf.GetValue()
case Operation_NE:
return value != sf.GetValue()
}
var (
exp, val int64
err error
)
if val, err = strconv.ParseInt(value, 10, 64); err != nil {
return true
}
if exp, err = strconv.ParseInt(sf.GetValue(), 10, 64); err != nil {
return true
}
switch sf.Op {
case Operation_GT:
return val > exp
case Operation_GE:
return val >= exp
case Operation_LT:
return val < exp
case Operation_LE:
return val <= exp
default:
return true
}
}
// Filter returns sublist of bs, satisfying f.
func (f Filter) Filter(bs ...Bucket) []Bucket {
result := make([]Bucket, 0, len(bs))
for _, b := range bs {
if f.Check(b) {
result = append(result, b)
}
}
return result
}
// NewFilter constructs SimpleFilter.
func NewFilter(op Operation, value string) *SimpleFilter {
return &SimpleFilter{
Op: op,
Args: &SimpleFilter_Value{Value: value},
}
}
// FilterIn returns filter, which checks if value is in specified list.
func FilterIn(values ...string) *SimpleFilter {
fs := make([]*SimpleFilter, 0, len(values))
for _, v := range values {
fs = append(fs, FilterEQ(v))
}
return FilterOR(fs...)
}
// FilterNotIn returns filter, which checks if value is not in specified list.
func FilterNotIn(values ...string) *SimpleFilter {
fs := make([]*SimpleFilter, 0, len(values))
for _, v := range values {
fs = append(fs, FilterNE(v))
}
return FilterAND(fs...)
}
// FilterOR returns OR combination of filters.
func FilterOR(fs ...*SimpleFilter) *SimpleFilter {
args := make([]SimpleFilter, 0, len(fs))
for _, f := range fs {
args = append(args, *f)
}
return &SimpleFilter{
Op: Operation_OR,
Args: &SimpleFilter_FArgs{FArgs: &SimpleFilters{Filters: args}},
}
}
// FilterAND returns AND combination of filters.
func FilterAND(fs ...*SimpleFilter) *SimpleFilter {
args := make([]SimpleFilter, 0, len(fs))
for _, f := range fs {
args = append(args, *f)
}
return &SimpleFilter{
Op: Operation_AND,
Args: &SimpleFilter_FArgs{FArgs: &SimpleFilters{Filters: args}},
}
}
// FilterEQ returns filter, which checks if value is equal to v.
func FilterEQ(v string) *SimpleFilter {
return &SimpleFilter{
Op: Operation_EQ,
Args: &SimpleFilter_Value{Value: v},
}
}
// FilterNE returns filter, which checks if value is not equal to v.
func FilterNE(v string) *SimpleFilter {
return &SimpleFilter{
Op: Operation_NE,
Args: &SimpleFilter_Value{Value: v},
}
}
// FilterGT returns filter, which checks if value is greater than v.
func FilterGT(v int64) *SimpleFilter {
return &SimpleFilter{
Op: Operation_GT,
Args: &SimpleFilter_Value{Value: strconv.FormatInt(v, 10)},
}
}
// FilterGE returns filter, which checks if value is greater or equal than v.
func FilterGE(v int64) *SimpleFilter {
return &SimpleFilter{
Op: Operation_GE,
Args: &SimpleFilter_Value{Value: strconv.FormatInt(v, 10)},
}
}
// FilterLT returns filter, which checks if value is less than v.
func FilterLT(v int64) *SimpleFilter {
return &SimpleFilter{
Op: Operation_LT,
Args: &SimpleFilter_Value{Value: strconv.FormatInt(v, 10)},
}
}
// FilterLE returns filter, which checks if value is less or equal than v.
func FilterLE(v int64) *SimpleFilter {
return &SimpleFilter{
Op: Operation_LE,
Args: &SimpleFilter_Value{Value: strconv.FormatInt(v, 10)},
}
}