forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
2727 lines (2461 loc) · 68.4 KB
/
marshal.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The gocql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocql
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
"math/big"
"math/bits"
"net"
"reflect"
"strconv"
"strings"
"time"
"gopkg.in/inf.v0"
)
var (
bigOne = big.NewInt(1)
emptyValue reflect.Value
)
var (
ErrorUDTUnavailable = errors.New("UDT are not available on protocols less than 3, please update config")
)
// Marshaler is the interface implemented by objects that can marshal
// themselves into values understood by Cassandra.
type Marshaler interface {
MarshalCQL(info TypeInfo) ([]byte, error)
}
// Unmarshaler is the interface implemented by objects that can unmarshal
// a Cassandra specific description of themselves.
type Unmarshaler interface {
UnmarshalCQL(info TypeInfo, data []byte) error
}
// Marshal returns the CQL encoding of the value for the Cassandra
// internal type described by the info parameter.
//
// nil is serialized as CQL null.
// If value implements Marshaler, its MarshalCQL method is called to marshal the data.
// If value is a pointer, the pointed-to value is marshaled.
//
// Supported conversions are as follows, other type combinations may be added in the future:
//
// CQL type | Go type (value) | Note
// varchar, ascii, blob, text | string, []byte |
// boolean | bool |
// tinyint, smallint, int | integer types |
// tinyint, smallint, int | string | formatted as base 10 number
// bigint, counter | integer types |
// bigint, counter | big.Int |
// bigint, counter | string | formatted as base 10 number
// float | float32 |
// double | float64 |
// decimal | inf.Dec |
// time | int64 | nanoseconds since start of day
// time | time.Duration | duration since start of day
// timestamp | int64 | milliseconds since Unix epoch
// timestamp | time.Time |
// list, set | slice, array |
// list, set | map[X]struct{} |
// map | map[X]Y |
// uuid, timeuuid | gocql.UUID |
// uuid, timeuuid | [16]byte | raw UUID bytes
// uuid, timeuuid | []byte | raw UUID bytes, length must be 16 bytes
// uuid, timeuuid | string | hex representation, see ParseUUID
// varint | integer types |
// varint | big.Int |
// varint | string | value of number in decimal notation
// inet | net.IP |
// inet | string | IPv4 or IPv6 address string
// tuple | slice, array |
// tuple | struct | fields are marshaled in order of declaration
// user-defined type | gocql.UDTMarshaler | MarshalUDT is called
// user-defined type | map[string]interface{} |
// user-defined type | struct | struct fields' cql tags are used for column names
// date | int64 | milliseconds since Unix epoch to start of day (in UTC)
// date | time.Time | start of day (in UTC)
// date | string | parsed using "2006-01-02" format
// duration | int64 | duration in nanoseconds
// duration | time.Duration |
// duration | gocql.Duration |
// duration | string | parsed with time.ParseDuration
func Marshal(info TypeInfo, value interface{}) ([]byte, error) {
if info.Version() < protoVersion1 {
panic("protocol version not set")
}
if valueRef := reflect.ValueOf(value); valueRef.Kind() == reflect.Ptr {
if valueRef.IsNil() {
return nil, nil
} else if v, ok := value.(Marshaler); ok {
return v.MarshalCQL(info)
} else {
return Marshal(info, valueRef.Elem().Interface())
}
}
if v, ok := value.(Marshaler); ok {
return v.MarshalCQL(info)
}
switch info.Type() {
case TypeVarchar, TypeAscii, TypeBlob, TypeText:
return marshalVarchar(info, value)
case TypeBoolean:
return marshalBool(info, value)
case TypeTinyInt:
return marshalTinyInt(info, value)
case TypeSmallInt:
return marshalSmallInt(info, value)
case TypeInt:
return marshalInt(info, value)
case TypeBigInt, TypeCounter:
return marshalBigInt(info, value)
case TypeFloat:
return marshalFloat(info, value)
case TypeDouble:
return marshalDouble(info, value)
case TypeDecimal:
return marshalDecimal(info, value)
case TypeTime:
return marshalTime(info, value)
case TypeTimestamp:
return marshalTimestamp(info, value)
case TypeList, TypeSet:
return marshalList(info, value)
case TypeMap:
return marshalMap(info, value)
case TypeUUID, TypeTimeUUID:
return marshalUUID(info, value)
case TypeVarint:
return marshalVarint(info, value)
case TypeInet:
return marshalInet(info, value)
case TypeTuple:
return marshalTuple(info, value)
case TypeUDT:
return marshalUDT(info, value)
case TypeDate:
return marshalDate(info, value)
case TypeDuration:
return marshalDuration(info, value)
}
// detect protocol 2 UDT
if strings.HasPrefix(info.Custom(), "org.apache.cassandra.db.marshal.UserType") && info.Version() < 3 {
return nil, ErrorUDTUnavailable
}
// TODO(tux21b): add the remaining types
return nil, fmt.Errorf("can not marshal %T into %s", value, info)
}
// Unmarshal parses the CQL encoded data based on the info parameter that
// describes the Cassandra internal data type and stores the result in the
// value pointed by value.
//
// If value implements Unmarshaler, it's UnmarshalCQL method is called to
// unmarshal the data.
// If value is a pointer to pointer, it is set to nil if the CQL value is
// null. Otherwise, nulls are unmarshalled as zero value.
//
// Supported conversions are as follows, other type combinations may be added in the future:
//
// CQL type | Go type (value) | Note
// varchar, ascii, blob, text | *string |
// varchar, ascii, blob, text | *[]byte | non-nil buffer is reused
// bool | *bool |
// tinyint, smallint, int, bigint, counter | *integer types |
// tinyint, smallint, int, bigint, counter | *big.Int |
// tinyint, smallint, int, bigint, counter | *string | formatted as base 10 number
// float | *float32 |
// double | *float64 |
// decimal | *inf.Dec |
// time | *int64 | nanoseconds since start of day
// time | *time.Duration |
// timestamp | *int64 | milliseconds since Unix epoch
// timestamp | *time.Time |
// list, set | *slice, *array |
// map | *map[X]Y |
// uuid, timeuuid | *string | see UUID.String
// uuid, timeuuid | *[]byte | raw UUID bytes
// uuid, timeuuid | *gocql.UUID |
// timeuuid | *time.Time | timestamp of the UUID
// inet | *net.IP |
// inet | *string | IPv4 or IPv6 address string
// tuple | *slice, *array |
// tuple | *struct | struct fields are set in order of declaration
// user-defined types | gocql.UDTUnmarshaler | UnmarshalUDT is called
// user-defined types | *map[string]interface{} |
// user-defined types | *struct | cql tag is used to determine field name
// date | *time.Time | time of beginning of the day (in UTC)
// date | *string | formatted with 2006-01-02 format
// duration | *gocql.Duration |
func Unmarshal(info TypeInfo, data []byte, value interface{}) error {
if v, ok := value.(Unmarshaler); ok {
return v.UnmarshalCQL(info, data)
}
if isNullableValue(value) {
return unmarshalNullable(info, data, value)
}
switch info.Type() {
case TypeVarchar, TypeAscii, TypeBlob, TypeText:
return unmarshalVarchar(info, data, value)
case TypeBoolean:
return unmarshalBool(info, data, value)
case TypeInt:
return unmarshalInt(info, data, value)
case TypeBigInt, TypeCounter:
return unmarshalBigInt(info, data, value)
case TypeVarint:
return unmarshalVarint(info, data, value)
case TypeSmallInt:
return unmarshalSmallInt(info, data, value)
case TypeTinyInt:
return unmarshalTinyInt(info, data, value)
case TypeFloat:
return unmarshalFloat(info, data, value)
case TypeDouble:
return unmarshalDouble(info, data, value)
case TypeDecimal:
return unmarshalDecimal(info, data, value)
case TypeTime:
return unmarshalTime(info, data, value)
case TypeTimestamp:
return unmarshalTimestamp(info, data, value)
case TypeList, TypeSet:
return unmarshalList(info, data, value)
case TypeMap:
return unmarshalMap(info, data, value)
case TypeTimeUUID:
return unmarshalTimeUUID(info, data, value)
case TypeUUID:
return unmarshalUUID(info, data, value)
case TypeInet:
return unmarshalInet(info, data, value)
case TypeTuple:
return unmarshalTuple(info, data, value)
case TypeUDT:
return unmarshalUDT(info, data, value)
case TypeDate:
return unmarshalDate(info, data, value)
case TypeDuration:
return unmarshalDuration(info, data, value)
}
// detect protocol 2 UDT
if strings.HasPrefix(info.Custom(), "org.apache.cassandra.db.marshal.UserType") && info.Version() < 3 {
return ErrorUDTUnavailable
}
// TODO(tux21b): add the remaining types
return fmt.Errorf("can not unmarshal %s into %T", info, value)
}
func isNullableValue(value interface{}) bool {
v := reflect.ValueOf(value)
return v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Ptr
}
func isNullData(info TypeInfo, data []byte) bool {
return data == nil
}
func unmarshalNullable(info TypeInfo, data []byte, value interface{}) error {
valueRef := reflect.ValueOf(value)
if isNullData(info, data) {
nilValue := reflect.Zero(valueRef.Type().Elem())
valueRef.Elem().Set(nilValue)
return nil
}
newValue := reflect.New(valueRef.Type().Elem().Elem())
valueRef.Elem().Set(newValue)
return Unmarshal(info, data, newValue.Interface())
}
func marshalVarchar(info TypeInfo, value interface{}) ([]byte, error) {
switch v := value.(type) {
case Marshaler:
return v.MarshalCQL(info)
case unsetColumn:
return nil, nil
case string:
return []byte(v), nil
case []byte:
return v, nil
}
if value == nil {
return nil, nil
}
rv := reflect.ValueOf(value)
t := rv.Type()
k := t.Kind()
switch {
case k == reflect.String:
return []byte(rv.String()), nil
case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
return rv.Bytes(), nil
}
return nil, marshalErrorf("can not marshal %T into %s", value, info)
}
func unmarshalVarchar(info TypeInfo, data []byte, value interface{}) error {
switch v := value.(type) {
case Unmarshaler:
return v.UnmarshalCQL(info, data)
case *string:
*v = string(data)
return nil
case *[]byte:
if data != nil {
*v = append((*v)[:0], data...)
} else {
*v = nil
}
return nil
}
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Ptr {
return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
}
rv = rv.Elem()
t := rv.Type()
k := t.Kind()
switch {
case k == reflect.String:
rv.SetString(string(data))
return nil
case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
var dataCopy []byte
if data != nil {
dataCopy = make([]byte, len(data))
copy(dataCopy, data)
}
rv.SetBytes(dataCopy)
return nil
}
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
}
func marshalSmallInt(info TypeInfo, value interface{}) ([]byte, error) {
switch v := value.(type) {
case Marshaler:
return v.MarshalCQL(info)
case unsetColumn:
return nil, nil
case int16:
return encShort(v), nil
case uint16:
return encShort(int16(v)), nil
case int8:
return encShort(int16(v)), nil
case uint8:
return encShort(int16(v)), nil
case int:
if v > math.MaxInt16 || v < math.MinInt16 {
return nil, marshalErrorf("marshal smallint: value %d out of range", v)
}
return encShort(int16(v)), nil
case int32:
if v > math.MaxInt16 || v < math.MinInt16 {
return nil, marshalErrorf("marshal smallint: value %d out of range", v)
}
return encShort(int16(v)), nil
case int64:
if v > math.MaxInt16 || v < math.MinInt16 {
return nil, marshalErrorf("marshal smallint: value %d out of range", v)
}
return encShort(int16(v)), nil
case uint:
if v > math.MaxUint16 {
return nil, marshalErrorf("marshal smallint: value %d out of range", v)
}
return encShort(int16(v)), nil
case uint32:
if v > math.MaxUint16 {
return nil, marshalErrorf("marshal smallint: value %d out of range", v)
}
return encShort(int16(v)), nil
case uint64:
if v > math.MaxUint16 {
return nil, marshalErrorf("marshal smallint: value %d out of range", v)
}
return encShort(int16(v)), nil
case string:
n, err := strconv.ParseInt(v, 10, 16)
if err != nil {
return nil, marshalErrorf("can not marshal %T into %s: %v", value, info, err)
}
return encShort(int16(n)), nil
}
if value == nil {
return nil, nil
}
switch rv := reflect.ValueOf(value); rv.Type().Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
v := rv.Int()
if v > math.MaxInt16 || v < math.MinInt16 {
return nil, marshalErrorf("marshal smallint: value %d out of range", v)
}
return encShort(int16(v)), nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
v := rv.Uint()
if v > math.MaxUint16 {
return nil, marshalErrorf("marshal smallint: value %d out of range", v)
}
return encShort(int16(v)), nil
case reflect.Ptr:
if rv.IsNil() {
return nil, nil
}
}
return nil, marshalErrorf("can not marshal %T into %s", value, info)
}
func marshalTinyInt(info TypeInfo, value interface{}) ([]byte, error) {
switch v := value.(type) {
case Marshaler:
return v.MarshalCQL(info)
case unsetColumn:
return nil, nil
case int8:
return []byte{byte(v)}, nil
case uint8:
return []byte{byte(v)}, nil
case int16:
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case uint16:
if v > math.MaxUint8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case int:
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case int32:
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case int64:
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case uint:
if v > math.MaxUint8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case uint32:
if v > math.MaxUint8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case uint64:
if v > math.MaxUint8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case string:
n, err := strconv.ParseInt(v, 10, 8)
if err != nil {
return nil, marshalErrorf("can not marshal %T into %s: %v", value, info, err)
}
return []byte{byte(n)}, nil
}
if value == nil {
return nil, nil
}
switch rv := reflect.ValueOf(value); rv.Type().Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
v := rv.Int()
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
v := rv.Uint()
if v > math.MaxUint8 {
return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
}
return []byte{byte(v)}, nil
case reflect.Ptr:
if rv.IsNil() {
return nil, nil
}
}
return nil, marshalErrorf("can not marshal %T into %s", value, info)
}
func marshalInt(info TypeInfo, value interface{}) ([]byte, error) {
switch v := value.(type) {
case Marshaler:
return v.MarshalCQL(info)
case unsetColumn:
return nil, nil
case int:
if v > math.MaxInt32 || v < math.MinInt32 {
return nil, marshalErrorf("marshal int: value %d out of range", v)
}
return encInt(int32(v)), nil
case uint:
if v > math.MaxUint32 {
return nil, marshalErrorf("marshal int: value %d out of range", v)
}
return encInt(int32(v)), nil
case int64:
if v > math.MaxInt32 || v < math.MinInt32 {
return nil, marshalErrorf("marshal int: value %d out of range", v)
}
return encInt(int32(v)), nil
case uint64:
if v > math.MaxUint32 {
return nil, marshalErrorf("marshal int: value %d out of range", v)
}
return encInt(int32(v)), nil
case int32:
return encInt(v), nil
case uint32:
return encInt(int32(v)), nil
case int16:
return encInt(int32(v)), nil
case uint16:
return encInt(int32(v)), nil
case int8:
return encInt(int32(v)), nil
case uint8:
return encInt(int32(v)), nil
case string:
i, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return nil, marshalErrorf("can not marshal string to int: %s", err)
}
return encInt(int32(i)), nil
}
if value == nil {
return nil, nil
}
switch rv := reflect.ValueOf(value); rv.Type().Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
v := rv.Int()
if v > math.MaxInt32 || v < math.MinInt32 {
return nil, marshalErrorf("marshal int: value %d out of range", v)
}
return encInt(int32(v)), nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
v := rv.Uint()
if v > math.MaxInt32 {
return nil, marshalErrorf("marshal int: value %d out of range", v)
}
return encInt(int32(v)), nil
case reflect.Ptr:
if rv.IsNil() {
return nil, nil
}
}
return nil, marshalErrorf("can not marshal %T into %s", value, info)
}
func encInt(x int32) []byte {
return []byte{byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}
}
func decInt(x []byte) int32 {
if len(x) != 4 {
return 0
}
return int32(x[0])<<24 | int32(x[1])<<16 | int32(x[2])<<8 | int32(x[3])
}
func encShort(x int16) []byte {
p := make([]byte, 2)
p[0] = byte(x >> 8)
p[1] = byte(x)
return p
}
func decShort(p []byte) int16 {
if len(p) != 2 {
return 0
}
return int16(p[0])<<8 | int16(p[1])
}
func decTiny(p []byte) int8 {
if len(p) != 1 {
return 0
}
return int8(p[0])
}
func marshalBigInt(info TypeInfo, value interface{}) ([]byte, error) {
switch v := value.(type) {
case Marshaler:
return v.MarshalCQL(info)
case unsetColumn:
return nil, nil
case int:
return encBigInt(int64(v)), nil
case uint:
if uint64(v) > math.MaxInt64 {
return nil, marshalErrorf("marshal bigint: value %d out of range", v)
}
return encBigInt(int64(v)), nil
case int64:
return encBigInt(v), nil
case uint64:
return encBigInt(int64(v)), nil
case int32:
return encBigInt(int64(v)), nil
case uint32:
return encBigInt(int64(v)), nil
case int16:
return encBigInt(int64(v)), nil
case uint16:
return encBigInt(int64(v)), nil
case int8:
return encBigInt(int64(v)), nil
case uint8:
return encBigInt(int64(v)), nil
case big.Int:
return encBigInt2C(&v), nil
case string:
i, err := strconv.ParseInt(value.(string), 10, 64)
if err != nil {
return nil, marshalErrorf("can not marshal string to bigint: %s", err)
}
return encBigInt(i), nil
}
if value == nil {
return nil, nil
}
rv := reflect.ValueOf(value)
switch rv.Type().Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
v := rv.Int()
return encBigInt(v), nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
v := rv.Uint()
if v > math.MaxInt64 {
return nil, marshalErrorf("marshal bigint: value %d out of range", v)
}
return encBigInt(int64(v)), nil
}
return nil, marshalErrorf("can not marshal %T into %s", value, info)
}
func encBigInt(x int64) []byte {
return []byte{byte(x >> 56), byte(x >> 48), byte(x >> 40), byte(x >> 32),
byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}
}
func bytesToInt64(data []byte) (ret int64) {
for i := range data {
ret |= int64(data[i]) << (8 * uint(len(data)-i-1))
}
return ret
}
func bytesToUint64(data []byte) (ret uint64) {
for i := range data {
ret |= uint64(data[i]) << (8 * uint(len(data)-i-1))
}
return ret
}
func unmarshalBigInt(info TypeInfo, data []byte, value interface{}) error {
return unmarshalIntlike(info, decBigInt(data), data, value)
}
func unmarshalInt(info TypeInfo, data []byte, value interface{}) error {
return unmarshalIntlike(info, int64(decInt(data)), data, value)
}
func unmarshalSmallInt(info TypeInfo, data []byte, value interface{}) error {
return unmarshalIntlike(info, int64(decShort(data)), data, value)
}
func unmarshalTinyInt(info TypeInfo, data []byte, value interface{}) error {
return unmarshalIntlike(info, int64(decTiny(data)), data, value)
}
func unmarshalVarint(info TypeInfo, data []byte, value interface{}) error {
switch v := value.(type) {
case *big.Int:
return unmarshalIntlike(info, 0, data, value)
case *uint64:
if len(data) == 9 && data[0] == 0 {
*v = bytesToUint64(data[1:])
return nil
}
}
if len(data) > 8 {
return unmarshalErrorf("unmarshal int: varint value %v out of range for %T (use big.Int)", data, value)
}
int64Val := bytesToInt64(data)
if len(data) > 0 && len(data) < 8 && data[0]&0x80 > 0 {
int64Val -= (1 << uint(len(data)*8))
}
return unmarshalIntlike(info, int64Val, data, value)
}
func marshalVarint(info TypeInfo, value interface{}) ([]byte, error) {
var (
retBytes []byte
err error
)
switch v := value.(type) {
case unsetColumn:
return nil, nil
case uint64:
if v > uint64(math.MaxInt64) {
retBytes = make([]byte, 9)
binary.BigEndian.PutUint64(retBytes[1:], v)
} else {
retBytes = make([]byte, 8)
binary.BigEndian.PutUint64(retBytes, v)
}
default:
retBytes, err = marshalBigInt(info, value)
}
if err == nil {
// trim down to most significant byte
i := 0
for ; i < len(retBytes)-1; i++ {
b0 := retBytes[i]
if b0 != 0 && b0 != 0xFF {
break
}
b1 := retBytes[i+1]
if b0 == 0 && b1 != 0 {
if b1&0x80 == 0 {
i++
}
break
}
if b0 == 0xFF && b1 != 0xFF {
if b1&0x80 > 0 {
i++
}
break
}
}
retBytes = retBytes[i:]
}
return retBytes, err
}
func unmarshalIntlike(info TypeInfo, int64Val int64, data []byte, value interface{}) error {
switch v := value.(type) {
case *int:
if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) {
return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
}
*v = int(int64Val)
return nil
case *uint:
unitVal := uint64(int64Val)
switch info.Type() {
case TypeInt:
*v = uint(unitVal) & 0xFFFFFFFF
case TypeSmallInt:
*v = uint(unitVal) & 0xFFFF
case TypeTinyInt:
*v = uint(unitVal) & 0xFF
default:
if ^uint(0) == math.MaxUint32 && (int64Val < 0 || int64Val > math.MaxUint32) {
return unmarshalErrorf("unmarshal int: value %d out of range for %T", unitVal, *v)
}
*v = uint(unitVal)
}
return nil
case *int64:
*v = int64Val
return nil
case *uint64:
switch info.Type() {
case TypeInt:
*v = uint64(int64Val) & 0xFFFFFFFF
case TypeSmallInt:
*v = uint64(int64Val) & 0xFFFF
case TypeTinyInt:
*v = uint64(int64Val) & 0xFF
default:
*v = uint64(int64Val)
}
return nil
case *int32:
if int64Val < math.MinInt32 || int64Val > math.MaxInt32 {
return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
}
*v = int32(int64Val)
return nil
case *uint32:
switch info.Type() {
case TypeInt:
*v = uint32(int64Val) & 0xFFFFFFFF
case TypeSmallInt:
*v = uint32(int64Val) & 0xFFFF
case TypeTinyInt:
*v = uint32(int64Val) & 0xFF
default:
if int64Val < 0 || int64Val > math.MaxUint32 {
return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
}
*v = uint32(int64Val) & 0xFFFFFFFF
}
return nil
case *int16:
if int64Val < math.MinInt16 || int64Val > math.MaxInt16 {
return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
}
*v = int16(int64Val)
return nil
case *uint16:
switch info.Type() {
case TypeSmallInt:
*v = uint16(int64Val) & 0xFFFF
case TypeTinyInt:
*v = uint16(int64Val) & 0xFF
default:
if int64Val < 0 || int64Val > math.MaxUint16 {
return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
}
*v = uint16(int64Val) & 0xFFFF
}
return nil
case *int8:
if int64Val < math.MinInt8 || int64Val > math.MaxInt8 {
return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
}
*v = int8(int64Val)
return nil
case *uint8:
if info.Type() != TypeTinyInt && (int64Val < 0 || int64Val > math.MaxUint8) {
return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
}
*v = uint8(int64Val) & 0xFF
return nil
case *big.Int:
decBigInt2C(data, v)
return nil
case *string:
*v = strconv.FormatInt(int64Val, 10)
return nil
}
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Ptr {
return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
}
rv = rv.Elem()
switch rv.Type().Kind() {
case reflect.Int:
if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) {
return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
}
rv.SetInt(int64Val)
return nil
case reflect.Int64:
rv.SetInt(int64Val)
return nil
case reflect.Int32:
if int64Val < math.MinInt32 || int64Val > math.MaxInt32 {
return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
}
rv.SetInt(int64Val)
return nil
case reflect.Int16:
if int64Val < math.MinInt16 || int64Val > math.MaxInt16 {
return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
}
rv.SetInt(int64Val)
return nil
case reflect.Int8:
if int64Val < math.MinInt8 || int64Val > math.MaxInt8 {
return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
}
rv.SetInt(int64Val)
return nil
case reflect.Uint:
unitVal := uint64(int64Val)
switch info.Type() {
case TypeInt:
rv.SetUint(unitVal & 0xFFFFFFFF)
case TypeSmallInt:
rv.SetUint(unitVal & 0xFFFF)
case TypeTinyInt:
rv.SetUint(unitVal & 0xFF)
default:
if ^uint(0) == math.MaxUint32 && (int64Val < 0 || int64Val > math.MaxUint32) {
return unmarshalErrorf("unmarshal int: value %d out of range for %s", unitVal, rv.Type())
}
rv.SetUint(unitVal)
}
return nil
case reflect.Uint64:
unitVal := uint64(int64Val)
switch info.Type() {
case TypeInt:
rv.SetUint(unitVal & 0xFFFFFFFF)
case TypeSmallInt:
rv.SetUint(unitVal & 0xFFFF)
case TypeTinyInt:
rv.SetUint(unitVal & 0xFF)
default:
rv.SetUint(unitVal)
}
return nil
case reflect.Uint32:
unitVal := uint64(int64Val)
switch info.Type() {
case TypeInt:
rv.SetUint(unitVal & 0xFFFFFFFF)
case TypeSmallInt:
rv.SetUint(unitVal & 0xFFFF)
case TypeTinyInt:
rv.SetUint(unitVal & 0xFF)
default:
if int64Val < 0 || int64Val > math.MaxUint32 {
return unmarshalErrorf("unmarshal int: value %d out of range for %s", int64Val, rv.Type())
}
rv.SetUint(unitVal & 0xFFFFFFFF)
}
return nil
case reflect.Uint16:
unitVal := uint64(int64Val)
switch info.Type() {
case TypeSmallInt:
rv.SetUint(unitVal & 0xFFFF)
case TypeTinyInt:
rv.SetUint(unitVal & 0xFF)
default:
if int64Val < 0 || int64Val > math.MaxUint16 {
return unmarshalErrorf("unmarshal int: value %d out of range for %s", int64Val, rv.Type())
}
rv.SetUint(unitVal & 0xFFFF)
}
return nil
case reflect.Uint8:
if info.Type() != TypeTinyInt && (int64Val < 0 || int64Val > math.MaxUint8) {
return unmarshalErrorf("unmarshal int: value %d out of range for %s", int64Val, rv.Type())
}
rv.SetUint(uint64(int64Val) & 0xff)
return nil
}
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
}
func decBigInt(data []byte) int64 {
if len(data) != 8 {
return 0
}
return int64(data[0])<<56 | int64(data[1])<<48 |
int64(data[2])<<40 | int64(data[3])<<32 |
int64(data[4])<<24 | int64(data[5])<<16 |
int64(data[6])<<8 | int64(data[7])
}