@@ -153,7 +153,7 @@ func (jh *BinlogJSON) register(typ jsonDataType, Plugin jsonPlugin) {
153
153
func (jh * BinlogJSON ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
154
154
Plugin := jh .plugins [typ ]
155
155
if Plugin == nil {
156
- return nil , fmt .Errorf ("Plugin not found for type %d" , typ )
156
+ return nil , fmt .Errorf ("plugin not found for type %d" , typ )
157
157
}
158
158
return Plugin .getNode (typ , data , pos )
159
159
}
@@ -316,59 +316,157 @@ type intPlugin struct {
316
316
317
317
var _ jsonPlugin = (* intPlugin )(nil )
318
318
319
- func (ih intPlugin ) getVal (typ jsonDataType , data []byte , pos int ) (value float64 ) {
319
+ func (ipl intPlugin ) getVal (typ jsonDataType , data []byte , pos int ) (value int64 ) {
320
320
var val uint64
321
- var val2 float64
322
- size := ih .sizes [typ ]
321
+ var val2 int64
322
+ size := ipl .sizes [typ ]
323
323
for i := 0 ; i < size ; i ++ {
324
324
val = val + uint64 (data [pos + i ])<< (8 * i )
325
325
}
326
326
switch typ {
327
327
case jsonInt16 :
328
- val2 = float64 (int16 (val ))
329
- case jsonUint16 :
330
- val2 = float64 (uint16 (val ))
328
+ val2 = int64 (int16 (val ))
331
329
case jsonInt32 :
332
- val2 = float64 (int32 (val ))
333
- case jsonUint32 :
334
- val2 = float64 (uint32 (val ))
330
+ val2 = int64 (int32 (val ))
335
331
case jsonInt64 :
336
- val2 = float64 (int64 (val ))
337
- case jsonUint64 :
338
- val2 = float64 (val )
339
- case jsonDouble :
340
- val2 = math .Float64frombits (val )
332
+ val2 = int64 (val )
341
333
}
342
334
return val2
343
335
}
344
336
345
- func (ih intPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
346
- val := ih .getVal (typ , data , pos )
347
- node = ajson .NumericNode ("" , val )
337
+ func (ipl intPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
338
+ val := ipl .getVal (typ , data , pos )
339
+ node = ajson .IntegerNode ("" , val )
348
340
return node , nil
349
341
}
350
342
351
343
func newIntPlugin () * intPlugin {
352
- ih := & intPlugin {
344
+ ipl := & intPlugin {
353
345
info : & jsonPluginInfo {
354
346
name : "Int" ,
355
- types : []jsonDataType {jsonInt64 , jsonInt32 , jsonInt16 , jsonUint16 , jsonUint32 , jsonUint64 , jsonDouble },
347
+ types : []jsonDataType {jsonInt64 , jsonInt32 , jsonInt16 },
348
+ },
349
+ sizes : make (map [jsonDataType ]int ),
350
+ }
351
+ ipl .sizes = map [jsonDataType ]int {
352
+ jsonInt64 : 8 ,
353
+ jsonInt32 : 4 ,
354
+ jsonInt16 : 2 ,
355
+ }
356
+ for _ , typ := range ipl .info .types {
357
+ binlogJSON .register (typ , ipl )
358
+ }
359
+ return ipl
360
+ }
361
+
362
+ //endregion
363
+
364
+ //region uint plugin
365
+
366
+ func init () {
367
+ newUintPlugin ()
368
+ }
369
+
370
+ type uintPlugin struct {
371
+ info * jsonPluginInfo
372
+ sizes map [jsonDataType ]int
373
+ }
374
+
375
+ var _ jsonPlugin = (* uintPlugin )(nil )
376
+
377
+ func (upl uintPlugin ) getVal (typ jsonDataType , data []byte , pos int ) (value uint64 ) {
378
+ var val uint64
379
+ var val2 uint64
380
+ size := upl .sizes [typ ]
381
+ for i := 0 ; i < size ; i ++ {
382
+ val = val + uint64 (data [pos + i ])<< (8 * i )
383
+ }
384
+ switch typ {
385
+ case jsonUint16 :
386
+ val2 = uint64 (uint16 (val ))
387
+ case jsonUint32 :
388
+ val2 = uint64 (uint32 (val ))
389
+ case jsonUint64 :
390
+ val2 = val
391
+ }
392
+ return val2
393
+ }
394
+
395
+ func (upl uintPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
396
+ val := upl .getVal (typ , data , pos )
397
+ node = ajson .UnsignedIntegerNode ("" , val )
398
+ return node , nil
399
+ }
400
+
401
+ func newUintPlugin () * uintPlugin {
402
+ upl := & uintPlugin {
403
+ info : & jsonPluginInfo {
404
+ name : "Uint" ,
405
+ types : []jsonDataType {jsonUint16 , jsonUint32 , jsonUint64 },
356
406
},
357
407
sizes : make (map [jsonDataType ]int ),
358
408
}
359
- ih .sizes = map [jsonDataType ]int {
409
+ upl .sizes = map [jsonDataType ]int {
360
410
jsonUint64 : 8 ,
361
- jsonInt64 : 8 ,
362
411
jsonUint32 : 4 ,
363
- jsonInt32 : 4 ,
364
412
jsonUint16 : 2 ,
365
- jsonInt16 : 2 ,
413
+ }
414
+ for _ , typ := range upl .info .types {
415
+ binlogJSON .register (typ , upl )
416
+ }
417
+ return upl
418
+ }
419
+
420
+ //endregion
421
+
422
+ //region float plugin
423
+
424
+ func init () {
425
+ newFloatPlugin ()
426
+ }
427
+
428
+ type floatPlugin struct {
429
+ info * jsonPluginInfo
430
+ sizes map [jsonDataType ]int
431
+ }
432
+
433
+ var _ jsonPlugin = (* floatPlugin )(nil )
434
+
435
+ func (flp floatPlugin ) getVal (typ jsonDataType , data []byte , pos int ) (value float64 ) {
436
+ var val uint64
437
+ var val2 float64
438
+ size := flp .sizes [typ ]
439
+ for i := 0 ; i < size ; i ++ {
440
+ val = val + uint64 (data [pos + i ])<< (8 * i )
441
+ }
442
+ switch typ {
443
+ case jsonDouble :
444
+ val2 = math .Float64frombits (val )
445
+ }
446
+ return val2
447
+ }
448
+
449
+ func (flp floatPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
450
+ val := flp .getVal (typ , data , pos )
451
+ node = ajson .NumericNode ("" , val )
452
+ return node , nil
453
+ }
454
+
455
+ func newFloatPlugin () * floatPlugin {
456
+ fp := & floatPlugin {
457
+ info : & jsonPluginInfo {
458
+ name : "Float" ,
459
+ types : []jsonDataType {jsonDouble },
460
+ },
461
+ sizes : make (map [jsonDataType ]int ),
462
+ }
463
+ fp .sizes = map [jsonDataType ]int {
366
464
jsonDouble : 8 ,
367
465
}
368
- for _ , typ := range ih .info .types {
369
- binlogJSON .register (typ , ih )
466
+ for _ , typ := range fp .info .types {
467
+ binlogJSON .register (typ , fp )
370
468
}
371
- return ih
469
+ return fp
372
470
}
373
471
374
472
//endregion
@@ -385,7 +483,7 @@ type literalPlugin struct {
385
483
386
484
var _ jsonPlugin = (* literalPlugin )(nil )
387
485
388
- func (lh literalPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
486
+ func (lpl literalPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
389
487
val := jsonDataLiteral (data [pos ])
390
488
switch val {
391
489
case jsonNullLiteral :
@@ -401,14 +499,14 @@ func (lh literalPlugin) getNode(typ jsonDataType, data []byte, pos int) (node *a
401
499
}
402
500
403
501
func newLiteralPlugin () * literalPlugin {
404
- lh := & literalPlugin {
502
+ lpl := & literalPlugin {
405
503
info : & jsonPluginInfo {
406
504
name : "Literal" ,
407
505
types : []jsonDataType {jsonLiteral },
408
506
},
409
507
}
410
- binlogJSON .register (jsonLiteral , lh )
411
- return lh
508
+ binlogJSON .register (jsonLiteral , lpl )
509
+ return lpl
412
510
}
413
511
414
512
//endregion
@@ -427,7 +525,7 @@ var _ jsonPlugin = (*opaquePlugin)(nil)
427
525
428
526
// other types are stored as catch-all opaque types: documentation on these is scarce.
429
527
// we currently know about (and support) date/time/datetime/decimal.
430
- func (oh opaquePlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
528
+ func (opl opaquePlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
431
529
dataType := data [pos ]
432
530
start := 3 // account for length of stored value
433
531
end := start + 8 // all currently supported opaque data types are 8 bytes in size
@@ -484,14 +582,14 @@ func (oh opaquePlugin) getNode(typ jsonDataType, data []byte, pos int) (node *aj
484
582
}
485
583
486
584
func newOpaquePlugin () * opaquePlugin {
487
- oh := & opaquePlugin {
585
+ opl := & opaquePlugin {
488
586
info : & jsonPluginInfo {
489
587
name : "Opaque" ,
490
588
types : []jsonDataType {jsonOpaque },
491
589
},
492
590
}
493
- binlogJSON .register (jsonOpaque , oh )
494
- return oh
591
+ binlogJSON .register (jsonOpaque , opl )
592
+ return opl
495
593
}
496
594
497
595
//endregion
@@ -508,22 +606,22 @@ type stringPlugin struct {
508
606
509
607
var _ jsonPlugin = (* stringPlugin )(nil )
510
608
511
- func (sh stringPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
609
+ func (spl stringPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
512
610
size , pos := readVariableLength (data , pos )
513
611
node = ajson .StringNode ("" , string (data [pos :pos + size ]))
514
612
515
613
return node , nil
516
614
}
517
615
518
616
func newStringPlugin () * stringPlugin {
519
- sh := & stringPlugin {
617
+ spl := & stringPlugin {
520
618
info : & jsonPluginInfo {
521
619
name : "String" ,
522
620
types : []jsonDataType {jsonString },
523
621
},
524
622
}
525
- binlogJSON .register (jsonString , sh )
526
- return sh
623
+ binlogJSON .register (jsonString , spl )
624
+ return spl
527
625
}
528
626
529
627
//endregion
@@ -542,7 +640,7 @@ var _ jsonPlugin = (*arrayPlugin)(nil)
542
640
543
641
// arrays are stored thus:
544
642
// | type_identifier(one of [2,3]) | elem count | obj size | list of offsets+lengths of values | actual values |
545
- func (ah arrayPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
643
+ func (apl arrayPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
546
644
jlog ("JSON Array %s, len %d" , jsonDataTypeToString (uint (typ )), len (data ))
547
645
var nodes []* ajson.Node
548
646
var elem * ajson.Node
@@ -565,15 +663,15 @@ func (ah arrayPlugin) getNode(typ jsonDataType, data []byte, pos int) (node *ajs
565
663
}
566
664
567
665
func newArrayPlugin () * arrayPlugin {
568
- ah := & arrayPlugin {
666
+ apl := & arrayPlugin {
569
667
info : & jsonPluginInfo {
570
668
name : "Array" ,
571
669
types : []jsonDataType {jsonSmallArray , jsonLargeArray },
572
670
},
573
671
}
574
- binlogJSON .register (jsonSmallArray , ah )
575
- binlogJSON .register (jsonLargeArray , ah )
576
- return ah
672
+ binlogJSON .register (jsonSmallArray , apl )
673
+ binlogJSON .register (jsonLargeArray , apl )
674
+ return apl
577
675
}
578
676
579
677
//endregion
@@ -592,7 +690,7 @@ var _ jsonPlugin = (*objectPlugin)(nil)
592
690
593
691
// objects are stored thus:
594
692
// | type_identifier(0/1) | elem count | obj size | list of offsets+lengths of keys | list of offsets+lengths of values | actual keys | actual values |
595
- func (oh objectPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
693
+ func (opl objectPlugin ) getNode (typ jsonDataType , data []byte , pos int ) (node * ajson.Node , err error ) {
596
694
jlog ("JSON Type is %s, len %d" , jsonDataTypeToString (uint (typ )), len (data ))
597
695
598
696
// "large" decides number of bytes used to specify element count and total object size: 4 bytes for large, 2 for small
@@ -640,15 +738,15 @@ func (oh objectPlugin) getNode(typ jsonDataType, data []byte, pos int) (node *aj
640
738
}
641
739
642
740
func newObjectPlugin () * objectPlugin {
643
- oh := & objectPlugin {
741
+ opl := & objectPlugin {
644
742
info : & jsonPluginInfo {
645
743
name : "Object" ,
646
744
types : []jsonDataType {jsonSmallObject , jsonLargeObject },
647
745
},
648
746
}
649
- binlogJSON .register (jsonSmallObject , oh )
650
- binlogJSON .register (jsonLargeObject , oh )
651
- return oh
747
+ binlogJSON .register (jsonSmallObject , opl )
748
+ binlogJSON .register (jsonLargeObject , opl )
749
+ return opl
652
750
}
653
751
654
752
//endregion
0 commit comments