-
Notifications
You must be signed in to change notification settings - Fork 25
/
pack.go
817 lines (772 loc) · 20.7 KB
/
pack.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
package msgpack
import (
"io"
"os"
"reflect"
"unsafe"
)
const (
NIL = 0xc0
FALSE = 0xc2
TRUE = 0xc3
FLOAT = 0xca
DOUBLE = 0xcb
UINT8 = 0xcc
UINT16 = 0xcd
UINT32 = 0xce
UINT64 = 0xcf
INT8 = 0xd0
INT16 = 0xd1
INT32 = 0xd2
INT64 = 0xd3
RAW16 = 0xda
RAW32 = 0xdb
ARRAY16 = 0xdc
ARRAY32 = 0xdd
MAP16 = 0xde
MAP32 = 0xdf
FIXMAP = 0x80
FIXARRAY = 0x90
FIXRAW = 0xa0
MAXFIXMAP = 16
MAXFIXARRAY = 16
MAXFIXRAW = 32
LEN_INT32 = 4
LEN_INT64 = 8
MAX16BIT = 2 << (16 - 1)
REGULAR_UINT7_MAX = 2 << (7 - 1)
REGULAR_UINT8_MAX = 2 << (8 - 1)
REGULAR_UINT16_MAX = 2 << (16 - 1)
REGULAR_UINT32_MAX = 2 << (32 - 1)
SPECIAL_INT8 = 32
SPECIAL_INT16 = 2 << (8 - 2)
SPECIAL_INT32 = 2 << (16 - 2)
SPECIAL_INT64 = 2 << (32 - 2)
)
type Bytes []byte
// Packs a given value and writes it into the specified writer.
func PackUint8(writer io.Writer, value uint8) (n int, err error) {
// Assume the numbers outside of range is the least common case
if value >= REGULAR_UINT7_MAX {
return writer.Write(Bytes{UINT8, value})
}
return writer.Write(Bytes{value})
}
// Packs a given value and writes it into the specified writer.
func PackUint16(writer io.Writer, value uint16) (n int, err error) {
// Assume the numbers outside of range is the least common case
if value >= REGULAR_UINT8_MAX {
return writer.Write(Bytes{UINT16, byte(value >> 8), byte(value)})
}
return PackUint8(writer, uint8(value))
}
// Packs a given value and writes it into the specified writer.
func PackUint32(writer io.Writer, value uint32) (n int, err error) {
// Assume the numbers outside of range is the least common case
if value >= REGULAR_UINT16_MAX {
return writer.Write(Bytes{UINT32, byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value)})
}
return PackUint16(writer, uint16(value))
}
// Packs a given value and writes it into the specified writer.
func PackUint64(writer io.Writer, value uint64) (n int, err error) {
// Assume the numbers outside of range is the least common case
if value >= REGULAR_UINT32_MAX {
return writer.Write(Bytes{UINT64, byte(value >> 56), byte(value >> 48), byte(value >> 40), byte(value >> 32), byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value)})
}
return PackUint32(writer, uint32(value))
}
// Packs a given value and writes it into the specified writer.
func PackUint(writer io.Writer, value uint) (n int, err error) {
switch unsafe.Sizeof(value) {
case LEN_INT32:
return PackUint32(writer, *(*uint32)(unsafe.Pointer(&value)))
case LEN_INT64:
return PackUint64(writer, *(*uint64)(unsafe.Pointer(&value)))
}
return 0, os.ErrNotExist // never get here
}
// Packs a given value and writes it into the specified writer.
func PackInt8(writer io.Writer, value int8) (n int, err error) {
// Assume the numbers outside of range is the least common case
if value < -SPECIAL_INT8 {
return writer.Write(Bytes{INT8, byte(value)})
}
return writer.Write(Bytes{byte(value)})
}
// Packs a given value and writes it into the specified writer.
func PackInt16(writer io.Writer, value int16) (n int, err error) {
// Assume the numbers outside of range is the least common case
if value < -SPECIAL_INT16 || value >= SPECIAL_INT16 {
return writer.Write(Bytes{INT16, byte(uint16(value) >> 8), byte(value)})
}
return PackInt8(writer, int8(value))
}
// Packs a given value and writes it into the specified writer.
func PackInt32(writer io.Writer, value int32) (n int, err error) {
// Assume the numbers outside of range is the least common case
if value < -SPECIAL_INT32 || value >= SPECIAL_INT32 {
return writer.Write(Bytes{INT32, byte(uint32(value) >> 24), byte(uint32(value) >> 16), byte(uint32(value) >> 8), byte(value)})
}
return PackInt16(writer, int16(value))
}
// Packs a given value and writes it into the specified writer.
func PackInt64(writer io.Writer, value int64) (n int, err error) {
// Assume the numbers outside of range is the least common case
if value < -SPECIAL_INT64 || value >= SPECIAL_INT64 {
return writer.Write(Bytes{INT64, byte(uint64(value) >> 56), byte(uint64(value) >> 48), byte(uint64(value) >> 40), byte(uint64(value) >> 32), byte(uint64(value) >> 24), byte(uint64(value) >> 16), byte(uint64(value) >> 8), byte(value)})
}
return PackInt32(writer, int32(value))
}
// Packs a given value and writes it into the specified writer.
func PackInt(writer io.Writer, value int) (n int, err error) {
switch unsafe.Sizeof(value) {
case LEN_INT32:
return PackInt32(writer, *(*int32)(unsafe.Pointer(&value)))
case LEN_INT64:
return PackInt64(writer, *(*int64)(unsafe.Pointer(&value)))
}
return 0, os.ErrNotExist // never get here
}
// Packs a given value and writes it into the specified writer.
func PackNil(writer io.Writer) (n int, err error) {
return writer.Write(Bytes{NIL})
}
// Packs a given value and writes it into the specified writer.
func PackBool(writer io.Writer, value bool) (n int, err error) {
if value {
return writer.Write(Bytes{TRUE})
}
return writer.Write(Bytes{FALSE})
}
// Packs a given value and writes it into the specified writer.
func PackFloat32(writer io.Writer, value float32) (n int, err error) {
return PackUint32(writer, *(*uint32)(unsafe.Pointer(&value)))
}
// Packs a given value and writes it into the specified writer.
func PackFloat64(writer io.Writer, value float64) (n int, err error) {
return PackUint64(writer, *(*uint64)(unsafe.Pointer(&value)))
}
// Packs a given value and writes it into the specified writer.
func PackBytes(writer io.Writer, value []byte) (n int, err error) {
length := len(value)
if length < MAXFIXRAW {
n1, err := writer.Write(Bytes{FIXRAW | uint8(length)})
if err != nil {
return n1, err
}
n2, err := writer.Write(value)
return n1 + n2, err
} else if length < MAX16BIT {
n1, err := writer.Write(Bytes{RAW16, byte(length >> 16), byte(length)})
if err != nil {
return n1, err
}
n2, err := writer.Write(value)
return n1 + n2, err
}
n1, err := writer.Write(Bytes{RAW32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n1, err
}
n2, err := writer.Write(value)
return n1 + n2, err
}
// Packs a given value and writes it into the specified writer.
func PackUint16Array(writer io.Writer, value []uint16) (n int, err error) {
length := len(value)
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackUint16(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackUint16(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackUint16(writer, i)
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackUint32Array(writer io.Writer, value []uint32) (n int, err error) {
length := len(value)
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackUint32(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackUint32(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackUint32(writer, i)
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackUint64Array(writer io.Writer, value []uint64) (n int, err error) {
length := len(value)
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackUint64(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackUint64(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackUint64(writer, i)
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackUintArray(writer io.Writer, value []uint) (n int, err error) {
switch unsafe.Sizeof(0) {
case 4:
return PackUint32Array(writer, *(*[]uint32)(unsafe.Pointer(&value)))
case 8:
return PackUint64Array(writer, *(*[]uint64)(unsafe.Pointer(&value)))
}
return 0, os.ErrNotExist // never get here
}
// Packs a given value and writes it into the specified writer.
func PackInt8Array(writer io.Writer, value []int8) (n int, err error) {
length := len(value)
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt8(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt8(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt8(writer, i)
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackInt16Array(writer io.Writer, value []int16) (n int, err error) {
length := len(value)
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt16(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt16(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt16(writer, i)
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackInt32Array(writer io.Writer, value []int32) (n int, err error) {
length := len(value)
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt32(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt32(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt32(writer, i)
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackInt64Array(writer io.Writer, value []int64) (n int, err error) {
length := len(value)
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt64(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt64(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackInt64(writer, i)
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackIntArray(writer io.Writer, value []int) (n int, err error) {
switch unsafe.Sizeof(0) {
case LEN_INT32:
return PackInt32Array(writer, *(*[]int32)(unsafe.Pointer(&value)))
case LEN_INT64:
return PackInt64Array(writer, *(*[]int64)(unsafe.Pointer(&value)))
}
return 0, os.ErrNotExist // never get here
}
// Packs a given value and writes it into the specified writer.
func PackFloat32Array(writer io.Writer, value []float32) (n int, err error) {
length := len(value)
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackFloat32(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackFloat32(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackFloat32(writer, i)
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackFloat64Array(writer io.Writer, value []float64) (n int, err error) {
length := len(value)
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackFloat64(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackFloat64(writer, i)
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, i := range value {
_n, err := PackFloat64(writer, i)
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackArray(writer io.Writer, value reflect.Value) (n int, err error) {
{
elemType := value.Type().Elem()
if (elemType.Kind() == reflect.Uint || elemType.Kind() == reflect.Uint8 || elemType.Kind() == reflect.Uint16 || elemType.Kind() == reflect.Uint32 || elemType.Kind() == reflect.Uint64 || elemType.Kind() == reflect.Uintptr) &&
elemType.Kind() == reflect.Uint8 {
return PackBytes(writer, value.Interface().([]byte))
}
}
length := value.Len()
if length < MAXFIXARRAY {
n, err := writer.Write(Bytes{FIXARRAY | byte(length)})
if err != nil {
return n, err
}
for i := 0; i < length; i++ {
_n, err := PackValue(writer, value.Index(i))
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{ARRAY16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for i := 0; i < length; i++ {
_n, err := PackValue(writer, value.Index(i))
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{ARRAY32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for i := 0; i < length; i++ {
_n, err := PackValue(writer, value.Index(i))
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackMap(writer io.Writer, value reflect.Value) (n int, err error) {
keys := value.MapKeys()
length := len(keys)
if length < MAXFIXMAP {
n, err := writer.Write(Bytes{FIXMAP | byte(length)})
if err != nil {
return n, err
}
for _, k := range keys {
_n, err := PackValue(writer, k)
if err != nil {
return n, err
}
n += _n
_n, err = PackValue(writer, value.MapIndex(k))
if err != nil {
return n, err
}
n += _n
}
} else if length < MAX16BIT {
n, err := writer.Write(Bytes{MAP16, byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, k := range keys {
_n, err := PackValue(writer, k)
if err != nil {
return n, err
}
n += _n
_n, err = PackValue(writer, value.MapIndex(k))
if err != nil {
return n, err
}
n += _n
}
} else {
n, err := writer.Write(Bytes{MAP32, byte(length >> 24), byte(length >> 16), byte(length >> 8), byte(length)})
if err != nil {
return n, err
}
for _, k := range keys {
_n, err := PackValue(writer, k)
if err != nil {
return n, err
}
n += _n
_n, err = PackValue(writer, value.MapIndex(k))
if err != nil {
return n, err
}
n += _n
}
}
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackValue(writer io.Writer, value reflect.Value) (n int, err error) {
if !value.IsValid() || value.Type() == nil {
return PackNil(writer)
}
switch _value := value; _value.Kind() {
case reflect.Bool:
return PackBool(writer, _value.Bool())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return PackUint64(writer, _value.Uint())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return PackInt64(writer, _value.Int())
case reflect.Float32, reflect.Float64:
return PackFloat64(writer, _value.Float())
case reflect.Array:
return PackArray(writer, _value)
case reflect.Slice:
return PackArray(writer, _value)
case reflect.Map:
return PackMap(writer, _value)
case reflect.String:
return PackBytes(writer, []byte(_value.String()))
case reflect.Interface:
__value := reflect.ValueOf(_value.Interface())
if __value.Kind() != reflect.Interface {
return PackValue(writer, __value)
}
}
panic("unsupported type: " + value.Type().String())
}
// Packs a given value and writes it into the specified writer.
func Pack(writer io.Writer, value interface{}) (n int, err error) {
if value == nil {
return PackNil(writer)
}
switch _value := value.(type) {
case bool:
return PackBool(writer, _value)
case uint8:
return PackUint8(writer, _value)
case uint16:
return PackUint16(writer, _value)
case uint32:
return PackUint32(writer, _value)
case uint64:
return PackUint64(writer, _value)
case uint:
return PackUint(writer, _value)
case int8:
return PackInt8(writer, _value)
case int16:
return PackInt16(writer, _value)
case int32:
return PackInt32(writer, _value)
case int64:
return PackInt64(writer, _value)
case int:
return PackInt(writer, _value)
case float32:
return PackFloat32(writer, _value)
case float64:
return PackFloat64(writer, _value)
case []byte:
return PackBytes(writer, _value)
case []uint16:
return PackUint16Array(writer, _value)
case []uint32:
return PackUint32Array(writer, _value)
case []uint64:
return PackUint64Array(writer, _value)
case []uint:
return PackUintArray(writer, _value)
case []int8:
return PackInt8Array(writer, _value)
case []int16:
return PackInt16Array(writer, _value)
case []int32:
return PackInt32Array(writer, _value)
case []int64:
return PackInt64Array(writer, _value)
case []int:
return PackIntArray(writer, _value)
case []float32:
return PackFloat32Array(writer, _value)
case []float64:
return PackFloat64Array(writer, _value)
case string:
return PackBytes(writer, Bytes(_value))
default:
return PackValue(writer, reflect.ValueOf(value))
}
return 0, nil // never get here
}