-
Notifications
You must be signed in to change notification settings - Fork 5
/
validator.go
466 lines (420 loc) · 9.88 KB
/
validator.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package veldt
import (
"fmt"
"github.com/unchartedsoftware/veldt/binning"
"github.com/unchartedsoftware/veldt/util/json"
)
const (
missing = "???"
expType = iota
queryType = iota
binaryType = iota
unaryType = iota
invalidType = iota
)
// validator parses a JSON query expression into its typed format. It
// ensure all types are correct and that the syntax is valid.
type validator struct {
json.Validator
pipeline *Pipeline
}
func newValidator(pipeline *Pipeline) *validator {
v := &validator{
pipeline: pipeline,
}
return v
}
func (v *validator) validateTileRequest(args map[string]interface{}) (*TileRequest, error) {
v.StartObject()
req := &TileRequest{}
// validate URI
req.URI = v.validateURI(args)
// validate coord
req.Coord = v.validateCoord(args)
// validate tile
req.Tile = v.validateTile(args)
// validate query
req.Query = v.validateQuery(args)
v.EndObject()
// check for any errors
err := v.Error()
if err != nil {
return nil, err
}
return req, nil
}
func (v *validator) validateMetaRequest(args map[string]interface{}) (*MetaRequest, error) {
v.StartObject()
req := &MetaRequest{}
// validate URI
req.URI = v.validateURI(args)
// validate meta
req.Meta = v.validateMeta(args)
v.EndObject()
// check for any errors
err := v.Error()
if err != nil {
return nil, err
}
return req, nil
}
// Parses the tile request JSON for the provided URI.
//
// Ex:
// {
// "uri": "example-uri-value0"
// }
//
func (v *validator) parseURI(args map[string]interface{}) (string, error) {
val, ok := args["uri"]
if !ok {
return missing, fmt.Errorf("`uri` not found")
}
uri, ok := val.(string)
if !ok {
return fmt.Sprintf("%v", val), fmt.Errorf("`uri` not of type `string`")
}
return uri, nil
}
func (v *validator) validateURI(args map[string]interface{}) string {
uri, err := v.parseURI(args)
v.BufferKeyValue("uri", uri, err)
return uri
}
// Parses the tile request JSON for the provided tile coordinate.
//
// Ex:
// {
// "coord": {
// "z": 4,
// "x": 12,
// "y": 3,
// }
// }
//
func (v *validator) parseCoord(args map[string]interface{}) (interface{}, *binning.TileCoord, error) {
c, ok := args["coord"]
if !ok {
return nil, nil, fmt.Errorf("`coord` not found")
}
coord, ok := c.(map[string]interface{})
if !ok {
return c, nil, fmt.Errorf("`coord` is not of correct type")
}
ix, ok := coord["x"]
if !ok {
return coord, nil, fmt.Errorf("`coord.x` not found")
}
x, ok := ix.(float64)
if !ok {
return coord, nil, fmt.Errorf("`coord.x` is not of type `number`")
}
iy, ok := coord["y"]
if !ok {
return coord, nil, fmt.Errorf("`coord.y` not found")
}
y, ok := iy.(float64)
if !ok {
return coord, nil, fmt.Errorf("`coord.y` is not of type `number`")
}
iz, ok := coord["z"]
if !ok {
return coord, nil, fmt.Errorf("`coord.z` not found")
}
z, ok := iz.(float64)
if !ok {
return coord, nil, fmt.Errorf("`coord.z` is not of type `number`")
}
return coord, &binning.TileCoord{
X: uint32(x),
Y: uint32(y),
Z: uint32(z),
}, nil
}
func (v *validator) validateCoord(args map[string]interface{}) *binning.TileCoord {
params, coord, err := v.parseCoord(args)
if params != nil {
v.BufferKeyValue("coord", params, err)
} else {
v.BufferKeyValue("coord", missing, err)
}
return coord
}
// Parses the tile request JSON for the provided tile type and parameters.
//
// Ex:
// {
// "tile": {
// "heatmap": {
// "xField": "pixel.x",
// "yField": "pixel.y",
// "left": 0,
// "right": 4294967296,
// "bottom": 0,
// "top": 4294967296,
// "resolution": 256
// }
// }
// }
//
func (v *validator) parseTile(args map[string]interface{}) (string, interface{}, Tile, error) {
id, params, ok := json.GetRandomChild(args)
if !ok {
return id, params, nil, fmt.Errorf("no tile type found")
}
tile, err := v.pipeline.GetTile(id, params)
if err != nil {
return id, params, nil, err
}
return id, params, tile, nil
}
func (v *validator) validateTile(args map[string]interface{}) Tile {
// check if the tile key exists
arg, ok := args["tile"]
if !ok {
v.BufferKeyValue("tile", missing, fmt.Errorf("`tile` not found"))
return nil
}
// check if the tile value is an object
val, ok := arg.(map[string]interface{})
if !ok {
v.BufferKeyValue("tile", arg, fmt.Errorf("`tile` is not of correct type"))
return nil
}
// check if tile is correct
v.StartSubObject("tile")
id, params, tile, err := v.parseTile(val)
if id == "" {
id = missing
params = missing
}
v.BufferKeyValue(id, params, err)
v.EndObject()
return tile
}
// Parses the meta request JSON for the provided meta type and parameters.
//
// Ex:
// {
// "meta": {
// "default": {}
// }
// }
//
func (v *validator) parseMeta(args map[string]interface{}) (string, interface{}, Meta, error) {
id, params, ok := json.GetRandomChild(args)
if !ok {
return id, params, nil, fmt.Errorf("no meta type found")
}
tile, err := v.pipeline.GetMeta(id, params)
if err != nil {
return id, params, nil, err
}
return id, params, tile, nil
}
func (v *validator) validateMeta(args map[string]interface{}) Meta {
// check if the meta key exists
arg, ok := args["meta"]
if !ok {
v.BufferKeyValue("meta", missing, fmt.Errorf("`meta` not found"))
return nil
}
// check if the meta value is an object
val, ok := arg.(map[string]interface{})
if !ok {
v.BufferKeyValue("meta", arg, fmt.Errorf("`meta` is not of correct type"))
return nil
}
// check if meta is correct
v.StartSubObject("meta")
id, params, meta, err := v.parseMeta(val)
if id == "" {
id = missing
params = missing
}
v.BufferKeyValue(id, params, err)
v.EndObject()
return meta
}
func (v *validator) validateQuery(args map[string]interface{}) Query {
val, ok := args["query"]
if !ok {
return nil
}
// nil query is valid
if val == nil {
return nil
}
// validate the query
v.StartObject()
validated := v.validateToken(val, true)
v.EndObject()
// parse the expression
query, err := newExpressionParser(v.pipeline).Parse(validated)
if err != nil {
return nil
}
return query
}
// Parses the query request JSON for the provided query expression.
//
// Ex:
// {
// "range": {
// "field": "age",
// "gte": 19
// }
// }
//
func (v *validator) parseQuery(args map[string]interface{}) (string, interface{}, Query, error) {
id, params, ok := json.GetRandomChild(args)
if !ok {
return id, params, nil, fmt.Errorf("no query type found")
}
query, err := v.pipeline.GetQuery(id, params)
if err != nil {
return id, params, nil, err
}
return id, params, query, nil
}
func (v *validator) validateQueryToken(args map[string]interface{}, first bool) Query {
id, params, query, err := v.parseQuery(args)
if id == "" {
id = missing
params = missing
}
if first {
v.StartSubObject("query")
v.BufferKeyValue(id, params, err)
v.EndObject()
} else {
v.BufferKeyValue(id, params, err)
}
return query
}
func isValidBinaryOperator(op string) bool {
return op == And || op == Or
}
func isValidUnaryOperator(op string) bool {
return op == Not
}
func isValidBoolOperator(op string) bool {
return isValidBinaryOperator(op) || isValidUnaryOperator(op)
}
func (v *validator) validateOperatorToken(op string) interface{} {
if !isValidBoolOperator(op) {
v.BufferValue(op, fmt.Errorf("invalid operator"))
return nil
}
v.BufferValue(op, nil)
return op
}
func (v *validator) validateExpressionToken(exp []interface{}, first bool) interface{} {
// open paren
if first {
v.StartSubArray("query")
} else {
v.StartArray()
}
// track last token to ensure next is valid
var last interface{}
// for each component
for i, current := range exp {
// next line
if !isTokenValid(last, current) {
v.StartError("unexpected token")
v.validateToken(current, false)
v.EndError()
last = current
continue
}
exp[i] = v.validateToken(current, false)
last = current
}
// close paren
v.EndArray()
return exp
}
func (v *validator) validateToken(arg interface{}, first bool) interface{} {
// expression
exp, ok := arg.([]interface{})
if ok {
return v.validateExpressionToken(exp, first)
}
// query
query, ok := arg.(map[string]interface{})
if ok {
return v.validateQueryToken(query, first)
}
// operator
op, ok := arg.(string)
if ok {
return v.validateOperatorToken(op)
}
// err
if first {
v.BufferKeyValue("query", fmt.Sprintf("%v", arg), fmt.Errorf("`query` is not of correct type"))
} else {
v.BufferValue(arg, fmt.Errorf("unrecognized symbol"))
}
return arg
}
func getTokenType(token interface{}) int {
_, ok := token.([]interface{})
if ok {
return expType
}
op, ok := token.(string)
if ok {
if isValidBinaryOperator(op) {
return binaryType
} else if isValidUnaryOperator(op) {
return unaryType
} else {
return invalidType
}
}
_, ok = token.(map[string]interface{})
if ok {
return queryType
}
return invalidType
}
func isTokenValid(c interface{}, n interface{}) bool {
if c == nil {
return firstTokenIsValid(n)
}
return nextTokenIsValid(c, n)
}
func nextTokenIsValid(c interface{}, n interface{}) bool {
current := getTokenType(c)
next := getTokenType(n)
if current == invalidType || next == invalidType {
// NOTE: consider unrecognized tokens as valid to allow the parsing to
// continue correctly
return true
}
switch current {
case expType:
return next == binaryType
case queryType:
return next == binaryType
case binaryType:
return next == unaryType || next == queryType || next == expType
case unaryType:
return next == queryType || next == expType
}
return false
}
func firstTokenIsValid(n interface{}) bool {
next := getTokenType(n)
switch next {
case expType:
return true
case queryType:
return true
case unaryType:
return true
}
return false
}