forked from si3nloong/go-rsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
207 lines (175 loc) · 3.74 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package rsql
import (
"encoding/base64"
"errors"
"fmt"
"io"
"reflect"
"strconv"
"strings"
"time"
"github.com/timtadh/lexmachine"
)
var (
typeOfTime = reflect.TypeOf(time.Time{})
typeOfByte = reflect.TypeOf([]byte(nil))
)
// Filter :
type Filter struct {
Name string
Operator Expr
Value interface{}
}
func (p *RSQL) parseFilter(values map[string]string, params *Params) error {
val, ok := values[p.FilterTag]
if !ok || len(val) < 1 {
return nil
}
scan, err := p.lexer.Scanner([]byte(val))
if err != nil {
return err
}
loop:
for {
tkn1, err := nextToken(scan)
if err != nil {
if err == io.EOF {
break loop
}
return err
}
switch tkn1.Value {
case "(", ")":
continue
}
f, ok := p.codec.Names[tkn1.Value]
if !ok {
return fmt.Errorf("invalid field to filter")
}
if _, ok := f.Tag.Lookup("filter"); !ok {
return fmt.Errorf("invalid field to filter")
}
name := tkn1.Value
if v, ok := f.Tag.Lookup("column"); ok {
name = v
}
allows := getAllows(f.Type)
if v, ok := f.Tag.Lookup("allow"); ok {
allows = strings.Split(v, "|")
}
tkn2, err := nextToken(scan)
if err != nil {
return err
}
op := operators[tkn2.Value]
if Strings(allows).IndexOf(op.String()) < 0 {
return errors.New("operator not support for this field")
}
tkn3, err := nextToken(scan)
if err != nil {
return err
}
v := reflect.New(f.Type).Elem()
tkn3.Value = strings.Trim(tkn3.Value, `"'`)
value, err := convertValue(v, tkn3.Value)
if err != nil {
return err
}
params.Filters = append(params.Filters, &Filter{
Name: name,
Operator: op,
Value: value,
})
tkn, err := nextToken(scan)
if err != nil {
if err == io.EOF {
break loop
}
return err
}
switch tkn.Value {
case ";", ",":
case "(", ")":
default:
return errors.New("unexpected char")
}
}
// for _, f := range params.Filters {
// log.Println("Each :", f, reflect.TypeOf(f.Value), reflect.TypeOf(f))
// }
return nil
}
func nextToken(scan *lexmachine.Scanner) (*Token, error) {
it, err, eof := scan.Next()
if eof {
return nil, io.EOF
}
if err != nil {
return nil, err
}
return it.(*Token), nil
}
func convertValue(v reflect.Value, value string) (interface{}, error) {
value = strings.TrimSpace(value)
switch v.Type() {
case typeOfTime:
t, err := time.Parse(time.RFC3339, value)
if err != nil {
return nil, err
}
v.Set(reflect.ValueOf(t))
case typeOfByte:
x, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return nil, err
}
v.SetBytes(x)
default:
switch v.Kind() {
case reflect.String:
v.SetString(value)
case reflect.Bool:
x, err := strconv.ParseBool(value)
if err != nil {
return nil, err
}
v.SetBool(x)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
x, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, err
}
if v.OverflowInt(x) {
return nil, errors.New("int overflow")
}
v.SetInt(x)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
x, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return nil, err
}
if v.OverflowUint(x) {
return nil, errors.New("unsigned int overflow")
}
v.SetUint(x)
case reflect.Float32, reflect.Float64:
x, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, err
}
if v.OverflowFloat(x) {
return nil, errors.New("float overflow")
}
v.SetFloat(x)
case reflect.Ptr:
if value == "null" {
zero := reflect.Zero(v.Type())
return zero.Interface(), nil
}
return convertValue(v.Elem(), value)
default:
return nil, fmt.Errorf("unsupported data type %v", v.Type())
}
}
return v.Interface(), nil
}