forked from Oskang09/goloquent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qson.go
278 lines (246 loc) · 5.51 KB
/
qson.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package qson
import (
"fmt"
"reflect"
"strings"
"time"
"cloud.google.com/go/datastore"
)
func isBaseType(t reflect.Type) bool {
return t == typeOfTime || t == typeOfPtrKey
}
// Property :
type Property struct {
key string
name string
typeOf reflect.Type
tag reflect.StructTag
}
func (p *Property) getName(name string) string {
n := strings.Split(p.tag.Get(name), ",")[0]
n = strings.TrimSpace(n)
if n == "" {
return p.key
}
return n
}
// JSON :
func (p *Property) JSON() string {
return p.getName("json")
}
// QSON :
func (p *Property) QSON() string {
return p.getName("qson")
}
// Tag :
func (p *Property) Tag() reflect.StructTag {
return p.tag
}
// Name :
func (p *Property) Name() string {
return p.name
}
// Field :
type Field struct {
name string
operator string
value interface{}
}
// Name :
func (f Field) Name() string {
return f.name
}
// Operator :
func (f Field) Operator() string {
return f.operator
}
// Value :
func (f Field) Value() interface{} {
return f.value
}
const (
eq = "$eq"
ne = "$ne"
not = "$not"
gt = "$gt"
gte = "$gte"
lt = "$lt"
lte = "$lte"
like = "$like"
nlike = "$nlike"
in = "$in"
nin = "$nin"
)
func validOperator(op string) (isOk bool) {
return op == eq || op == ne || op == not ||
op == gt || op == gte || op == lt || op == lte ||
op == like || op == nlike ||
op == in || op == nin
}
var (
typeOfByte = reflect.TypeOf([]byte(nil))
typeOfTime = reflect.TypeOf(time.Time{})
typeOfPtrKey = reflect.TypeOf(new(datastore.Key))
)
func convertToInterface(t reflect.Type, v interface{}) (interface{}, error) {
var it interface{}
switch t {
case typeOfPtrKey:
x, isOk := v.(string)
if !isOk {
return nil, unmatchDataType(t, x)
}
var err error
it, err = datastore.DecodeKey(x)
if err != nil {
return nil, fmt.Errorf("qson: unable to decode %s to %v", x, t)
}
case typeOfByte:
x, isOk := v.(string)
if !isOk {
return nil, unmatchDataType(t, x)
}
it = []byte(x)
case typeOfTime:
x, isOk := v.(string)
if !isOk {
return nil, unmatchDataType(t, x)
}
vv, err := time.Parse(time.RFC3339, x)
if err != nil {
return nil, fmt.Errorf("qson: unable to convert %s to %v", x, t)
}
it = vv
default:
switch t.Kind() {
case reflect.Ptr:
if v == nil {
return reflect.Zero(t).Interface(), nil
}
x, err := convertToInterface(t.Elem(), v)
if err != nil {
return nil, err
}
vv := reflect.ValueOf(x)
vi := reflect.New(vv.Type())
vi.Elem().Set(vv)
it = vi.Interface()
case reflect.String:
x, isOk := v.(string)
if !isOk {
return nil, unmatchDataType(t, x)
}
it = x
case reflect.Bool:
x, isOk := v.(bool)
if !isOk {
return nil, unmatchDataType(t, x)
}
it = x
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
x, isOk := v.(float64)
if !isOk {
return nil, unmatchDataType(t, v)
}
if x < 0 {
return nil, fmt.Errorf("qson: %s value has negative value, %v", t.Kind(), x)
}
v := reflect.New(t).Elem()
if v.OverflowUint(uint64(x)) {
return nil, fmt.Errorf("qson: %s value overflow, %v", t.Kind(), x)
}
v.SetUint(uint64(x))
it = v.Interface()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
x, isOk := v.(float64)
if !isOk {
return nil, unmatchDataType(t, v)
}
v := reflect.New(t).Elem()
if v.OverflowInt(int64(x)) {
return nil, fmt.Errorf("qson: %s value overflow, %v", t.Kind(), x)
}
v.SetInt(int64(x))
it = v.Interface()
case reflect.Float32, reflect.Float64:
x, isOk := v.(float64)
if !isOk {
return nil, unmatchDataType(t, x)
}
v := reflect.New(t).Elem()
if v.OverflowFloat(x) {
return nil, fmt.Errorf("qson: %s value overflow, %v", t.Kind(), x)
}
v.SetFloat(x)
it = v.Interface()
case reflect.Slice, reflect.Array:
x, isOk := v.([]interface{})
if !isOk {
return nil, unmatchDataType(t, x)
}
for i, xx := range x {
var err error
x[i], err = convertToInterface(t.Elem(), xx)
if err != nil {
return nil, err
}
}
it = x
default:
return nil, fmt.Errorf("qson: unsupported data type %v", t)
}
}
return it, nil
}
func unmatchDataType(o reflect.Type, p interface{}) error {
return fmt.Errorf("qson: unmatched data type of original, %v versus %v", o, reflect.TypeOf(p))
}
type structScan struct {
name []string
typeOf reflect.Type
}
func getProperty(t reflect.Type) map[string]*Property {
scans := append(make([]*structScan, 0), &structScan{nil, t})
props := make(map[string]*Property)
for len(scans) > 0 {
first := scans[0]
t := first.typeOf
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
// Skip if not anonymous private property
isExported := (f.PkgPath == "")
if !isExported && !f.Anonymous {
continue
}
name := strings.Split(f.Tag.Get("json"), ",")[0]
qson := strings.Split(f.Tag.Get("qson"), ",")[0]
if name == "-" || qson == "-" {
continue
}
if name == "" {
name = f.Name
}
if f.Type.Kind() == reflect.Struct && !isBaseType(f.Type) {
if f.Anonymous {
if !isExported {
continue
}
scans = append(scans, &structScan{first.name, f.Type})
continue
}
scans = append(scans, &structScan{append(first.name, name), f.Type})
continue
}
name = strings.Join(append(first.name, name), ".")
p := &Property{
key: f.Name,
name: name,
typeOf: f.Type,
tag: f.Tag,
}
props[name] = p
}
scans = scans[1:] // unshift
}
return props
}