7
7
"fmt"
8
8
"io"
9
9
"reflect"
10
+ "strconv"
10
11
"strings"
11
12
)
12
13
@@ -231,8 +232,11 @@ func DecodeJSONSchema(buf []byte) (Schema, error) {
231
232
return nil , err
232
233
}
233
234
234
- // TODO: Check for error during type assertion to avoid panic
235
- schemaType := strings .ToLower (fields ["type" ].(string ))
235
+ tmpStr , ok := fields ["type" ].(string )
236
+ if ! ok {
237
+ return nil , fmt .Errorf ("expected element 'type' not present in JSON data" )
238
+ }
239
+ schemaType := strings .ToLower (tmpStr )
236
240
237
241
switch schemaType {
238
242
@@ -264,7 +268,7 @@ func DecodeJSONSchema(buf []byte) (Schema, error) {
264
268
case 64 :
265
269
s .Bits = int (numBits )
266
270
default :
267
- return nil , fmt .Errorf ("invalid int bits : %d" , int (numBits ))
271
+ return nil , fmt .Errorf ("invalid int bit size encountered in JSON data : %d" , int (numBits ))
268
272
}
269
273
270
274
return s , nil
@@ -281,14 +285,18 @@ func DecodeJSONSchema(buf []byte) (Schema, error) {
281
285
282
286
case "float" :
283
287
s := & FloatSchema {}
288
+ numBits , ok := fields ["bits" ].(float64 )
289
+
290
+ if ! ok {
291
+ return nil , fmt .Errorf ("bits not present for float type in JSON data" )
292
+ }
284
293
285
- if fields [ "bits" ].( float64 ) == 64 {
294
+ if numBits == 64 {
286
295
s .Bits = 64
287
- } else if fields [ "bits" ].( float64 ) == 32 {
296
+ } else if numBits == 32 {
288
297
s .Bits = 32
289
298
} else {
290
- // TODO: Improve error message
291
- return nil , fmt .Errorf ("invalid floating point bit size encountered in JSON data" )
299
+ return nil , fmt .Errorf ("invalid float bit size encountered in JSON data: %d" , int (numBits ))
292
300
}
293
301
294
302
b , _ := fields ["nullable" ].(bool )
@@ -298,14 +306,18 @@ func DecodeJSONSchema(buf []byte) (Schema, error) {
298
306
299
307
case "complex" :
300
308
s := & ComplexSchema {}
309
+ numBits , ok := fields ["bits" ].(float64 )
310
+
311
+ if ! ok {
312
+ return nil , fmt .Errorf ("bits not present for complex type in JSON data" )
313
+ }
301
314
302
- if fields [ "bits" ].( float64 ) == 128 {
315
+ if numBits == 128 {
303
316
s .Bits = 128
304
- } else if fields [ "bits" ].( float64 ) == 64 {
317
+ } else if numBits == 64 {
305
318
s .Bits = 64
306
319
} else {
307
- // TODO: Improve error message
308
- return nil , fmt .Errorf ("invalid floating point bit size encountered in JSON data" )
320
+ return nil , fmt .Errorf ("invalid complex bit size encountered in JSON data: %d" , int (numBits ))
309
321
}
310
322
b , _ := fields ["nullable" ].(bool )
311
323
s .SchemaOptions .nullable = b
@@ -321,7 +333,12 @@ func DecodeJSONSchema(buf []byte) (Schema, error) {
321
333
322
334
b , _ := fields ["nullable" ].(bool )
323
335
s .SchemaOptions .nullable = b
324
- // TODO: Validate `tmpLen` (i.e. >= 0, no decimal part)
336
+
337
+ // validate that `tmpLen` is greater than 0 and is an integer
338
+ if (tmpLen < 0 ) || (tmpLen - float64 (int (tmpLen )) != 0 ) {
339
+ return nil , fmt .Errorf ("invalid string length encountered in JSON data: %d" , int (tmpLen ))
340
+ }
341
+
325
342
s .Length = int (tmpLen )
326
343
327
344
return s , nil
@@ -337,22 +354,37 @@ func DecodeJSONSchema(buf []byte) (Schema, error) {
337
354
s := & EnumSchema {}
338
355
b , _ := fields ["nullable" ].(bool )
339
356
s .SchemaOptions .nullable = b
357
+ tmp , ok := fields ["values" ]
358
+ if ok {
359
+
360
+ s .Values = make (map [int ]string )
361
+ for key , value := range tmp .(map [string ]interface {}) {
362
+ x , err := strconv .Atoi (key )
363
+ if err == nil {
364
+ s .Values [x ] = value .(string )
365
+ }
366
+ }
340
367
341
- // TODO: Parse enum values
368
+ }
342
369
343
370
return s , nil
344
371
345
372
case "array" :
346
- l , ok := fields ["length" ].(float64 )
373
+ tmpLen , ok := fields ["length" ].(float64 )
347
374
348
375
// if length is present, then we are dealing with a fixed length array
349
376
if ok {
350
377
s := & FixedArraySchema {}
351
378
352
379
b , _ := fields ["nullable" ].(bool )
353
380
s .SchemaOptions .nullable = b
354
- // TODO: Validate length
355
- s .Length = int (l )
381
+
382
+ // validate that `tmpLen` is greater than 0 and is an integer
383
+ if (tmpLen < 0 ) || (tmpLen - float64 (int (tmpLen )) != 0 ) {
384
+ return nil , fmt .Errorf ("invalid string length encountered in JSON data: %d" , int (tmpLen ))
385
+ }
386
+
387
+ s .Length = int (tmpLen )
356
388
357
389
// process the array element
358
390
tmp , err := json .Marshal (fields ["element" ])
@@ -403,14 +435,20 @@ func DecodeJSONSchema(buf []byte) (Schema, error) {
403
435
404
436
// fill in the name of this field...
405
437
// (the json encoded data only includes the name, not a list of aliases)
406
- // TODO: Validate type assertion to avoid panic; throw error instead
407
- tmpMap := objectFields [i ].(map [string ]interface {})
438
+ tmpMap , ok := objectFields [i ].(map [string ]interface {})
439
+
440
+ if ! ok {
441
+ return nil , fmt .Errorf ("invalid field definition encountered in JSON data" )
442
+ }
408
443
409
- // TODO: name could either be `string` or `[]string`
410
- // TODO: I'd recommend using a type switch here
411
- name := tmpMap ["name" ].(string )
444
+ name , ok := tmpMap ["name" ]
445
+ if ! ok {
446
+ return nil , fmt .Errorf ("missing name field encountered in JSON data" )
447
+ }
412
448
413
- of .Aliases = []string {name }
449
+ for j := 0 ; j < len (name .([]interface {})); j ++ {
450
+ of .Aliases = append (of .Aliases , name .([]interface {})[j ].(string ))
451
+ }
414
452
415
453
tmp , err := json .Marshal (objectFields [i ])
416
454
if err != nil {
0 commit comments