forked from si3nloong/go-rsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allow.go
50 lines (45 loc) · 1.21 KB
/
allow.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
package rsql
import (
"reflect"
"time"
)
var (
allowNums = []string{
"eq", "ne",
"gt", "gte", "lt", "lte",
"in", "notIn",
"between",
}
allowOperators = map[interface{}][]string{
reflect.String: {"eq", "ne", "gt", "gte", "lt", "lte", "in", "notIn", "like", "notLike"},
reflect.Bool: {"eq", "ne"},
reflect.Int: allowNums,
reflect.Int8: allowNums,
reflect.Int16: allowNums,
reflect.Int32: allowNums,
reflect.Int64: allowNums,
reflect.Uint: allowNums,
reflect.Uint8: allowNums,
reflect.Uint16: allowNums,
reflect.Uint32: allowNums,
reflect.Uint64: allowNums,
reflect.Float32: allowNums,
reflect.Float64: allowNums,
reflect.TypeOf([]byte{}): {"eq", "ne", "like", "notLike"},
reflect.TypeOf(time.Time{}): {"eq", "ne", "gt", "gte", "lt", "lte"},
}
)
func getAllows(t reflect.Type) []string {
t = indirect(t)
v, ok := allowOperators[t]
if ok {
return v
}
return allowOperators[t.Kind()]
}
func indirect(t reflect.Type) reflect.Type {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t
}