-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglint.go
More file actions
1528 lines (1293 loc) · 38.2 KB
/
glint.go
File metadata and controls
1528 lines (1293 loc) · 38.2 KB
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
// Package glint implements a hierarchical binary serialization format
package glint
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"unsafe"
)
// iface represents the memory layout of interface{} to bypass reflect.ValueOf overhead
type iface struct {
_, Data unsafe.Pointer
}
// sliceHeader replaces reflect.SliceHeader with inline pointer conversion
// for compatibility with vet and unsafe pointer rules
type sliceHeader struct {
Data unsafe.Pointer
Len int
Cap int
}
type WireType uint
const (
WireBool WireType = 1
WireInt WireType = 2
WireInt8 WireType = 3
WireInt16 WireType = 4
WireInt32 WireType = 5
WireInt64 WireType = 6
WireUint WireType = 7
WireUint8 WireType = 8
WireUint16 WireType = 9
WireUint32 WireType = 10
WireUint64 WireType = 11
WireFloat32 WireType = 12
WireFloat64 WireType = 13
WireString WireType = 14
WireBytes WireType = 15
WireStruct WireType = 16
WireMap WireType = 17
WireTime WireType = 18
// maximum value 31 (5-bit limit)
WireTypeMask = 0b00011111
WireSliceFlag WireType = 1 << 5 // marks slice fields
WirePtrFlag WireType = 1 << 6 // marks nullable fields
WireDeltaFlag WireType = 1 << 7 // delta encoding for numeric slices
wireSkip WireType = 1 << 8 // internal only
)
func (w WireType) String() string {
switch w {
case WireBool:
return "WireBool"
case WireInt:
return "WireInt"
case WireInt8:
return "WireInt8"
case WireInt16:
return "WireInt16"
case WireInt32:
return "WireInt32"
case WireInt64:
return "WireInt64"
case WireUint:
return "WireUint"
case WireUint8:
return "WireUint8"
case WireUint16:
return "WireUint16"
case WireUint32:
return "WireUint32"
case WireUint64:
return "WireUint64"
case WireFloat32:
return "WireFloat32"
case WireFloat64:
return "WireFloat64"
case WireString:
return "WireString"
case WireBytes:
return "WireBytes"
case WireStruct:
return "WireStruct"
case WireMap:
return "WireMap"
case WireTime:
return "WireTime"
default:
var prefix string
if w&wireSkip > 0 {
prefix += "(skip)"
}
if w&WireSliceFlag > 0 {
prefix += "[]"
}
if w&WirePtrFlag > 0 {
prefix += "*"
}
if w&WireDeltaFlag > 0 {
prefix += "(delta)"
}
if prefix != "" {
return prefix + (w & WireTypeMask).String()
}
return "invalid WireType"
}
}
// ReflectKindToWireType converts Go reflection types to glint wire types
func ReflectKindToWireType(k reflect.Type) WireType {
switch k.Kind() {
case reflect.Bool:
return WireBool
case reflect.Int:
return WireInt
case reflect.Int8:
return WireInt8
case reflect.Int16:
return WireInt16
case reflect.Int32:
return WireInt32
case reflect.Int64:
return WireInt64
case reflect.Uint:
return WireUint
case reflect.Uint8:
return WireUint8
case reflect.Uint16:
return WireUint16
case reflect.Uint32:
return WireUint32
case reflect.Uint64:
return WireUint64
case reflect.Float32:
return WireFloat32
case reflect.Float64:
return WireFloat64
case reflect.String:
return WireString
case reflect.Slice:
if k.Elem().Kind() == reflect.Uint8 {
return WireBytes
}
return WireSliceFlag | ReflectKindToWireType(k.Elem())
case reflect.Pointer:
return WirePtrFlag | ReflectKindToWireType(k.Elem())
case reflect.Struct:
if k == timeType {
return WireTime
}
return WireStruct
case reflect.Map:
return WireMap
}
panic(fmt.Sprintf("unable to create a wire type for %v", k))
}
// WireTypeToReflectType converts glint wire types back to Go reflection types
func WireTypeToReflectType(k WireType) reflect.Type {
switch {
case k&WirePtrFlag > 0:
return reflect.PointerTo(WireTypeToReflectType(k ^ WirePtrFlag))
case k&WireSliceFlag > 0:
return reflect.SliceOf(WireTypeToReflectType(k ^ WireSliceFlag))
}
switch k {
case WireBool:
return reflect.TypeOf(false)
case WireInt:
return reflect.TypeOf(int(0))
case WireInt8:
return reflect.TypeOf(int8(0))
case WireInt16:
return reflect.TypeOf(int16(0))
case WireInt32:
return reflect.TypeOf(int32(0))
case WireInt64:
return reflect.TypeOf(int64(0))
case WireUint:
return reflect.TypeOf(uint(0))
case WireUint8:
return reflect.TypeOf(uint8(0))
case WireUint16:
return reflect.TypeOf(uint16(0))
case WireUint32:
return reflect.TypeOf(uint32(0))
case WireUint64:
return reflect.TypeOf(uint64(0))
case WireFloat32:
return reflect.TypeOf(float32(0))
case WireFloat64:
return reflect.TypeOf(float64(0))
case WireString:
return reflect.TypeOf("")
case WireStruct:
return reflect.StructOf([]reflect.StructField{})
case WireTime:
return reflect.TypeOf(time.Time{})
case WireMap:
panic("use mapWireTypesToReflectKind")
}
panic(fmt.Sprintf("unable to create a reflect.Type for %v", k))
}
// mapWireTypesToReflectKind builds a map type from key and value wire types
func mapWireTypesToReflectKind(key, value WireType) reflect.Type {
k := WireTypeToReflectType(key)
v := WireTypeToReflectType(value)
return reflect.MapOf(k, v)
}
func derefAppend(f func(unsafe.Pointer, *Buffer)) func(unsafe.Pointer, *Buffer) {
// pointer fields require wrapping in a dereference function that handles nil values.
// A leading byte indicates presence: 1 for value present, 0 for nil.
return func(p unsafe.Pointer, b *Buffer) {
p = *(*unsafe.Pointer)(p)
if p == unsafe.Pointer(nil) {
b.AppendUint8(0)
return
}
b.AppendUint8(1)
f(p, b)
}
}
// decodeInstruction specifies how to decode and store a field value
type decodeInstruction struct {
fun func(unsafe.Pointer, Reader) Reader // fallback decoder when fast path unavailable
offset uintptr // field location in struct
kind WireType // wire type for fast path selection
subdec decoder // nested decoder for struct fields
subType reflect.Type // type information for nested decoder
tag string // field name from struct tag
subinstr []decodeInstruction // nested instructions for inlined decoding
optimizable bool // true if this slice-of-structs can use fast path
}
// TrustHeader enables HTTP-based trusted schema mode.
// Implements the KVP interface for HTTP headers.
type TrustHeader struct {
key string
value string
}
// Key provides the header name
func (t TrustHeader) Key() string {
return t.key
}
// Value provides the header content
func (t TrustHeader) Value() string {
return t.value
}
// NewTrustHeader creates an HTTP header for schema trust negotiation.
// Use with NewBufferWithTrust to skip schema transmission when both sides have matching schemas.
func NewTrustHeader(d *decoderImpl) TrustHeader {
return TrustHeader{"X-Glint-Trust", strconv.FormatUint(uint64(d.lastHash), 10)}
}
// deref creates a wrapper that dereferences pointers and handles nil checks.
// Reads the presence byte (1=value, 0=nil) before processing.
func deref(wrap func(unsafe.Pointer, Reader) Reader, _ WireType, t reflect.Type) func(unsafe.Pointer, Reader) Reader {
return func(p unsafe.Pointer, r Reader) Reader {
if r.ReadByte() == 0 {
return r
}
if *(*unsafe.Pointer)(p) == unsafe.Pointer(nil) {
*(*unsafe.Pointer)(p) = reflect.New(t.Elem()).UnsafePointer()
}
return wrap(*(*unsafe.Pointer)(p), r)
}
}
type assigner struct {
subDecoder decoder // decoder for nested types
fun func(unsafe.Pointer, Reader) Reader // field assignment function
rkind reflect.Kind // final type after pointer unwrapping
wire WireType // glint type for this field
pointer bool // indicates pointer field
}
func reflectKindToAssigner(k reflect.Type, usingTagName string, opts tagOptions, limits DecodeLimits) assigner {
pointerWrap := false
var fun func(unsafe.Pointer, Reader) Reader
var sub decoder
var wire WireType
kind := k.Kind()
if kind == reflect.Pointer {
pointerWrap = true
kind = k.Elem().Kind()
}
switch kind {
case reflect.Uint8:
wire = WireUint8
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*uint8)(p) = r.ReadUint8()
return r
}
case reflect.Uint16:
wire = WireUint16
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*uint16)(p) = r.ReadUint16()
return r
}
case reflect.Uint32:
wire = WireUint32
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*uint32)(p) = r.ReadUint32()
return r
}
case reflect.Uint64:
wire = WireUint64
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*uint64)(p) = r.ReadUint64()
return r
}
case reflect.Uint:
wire = WireUint
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*uint)(p) = r.ReadUint()
return r
}
case reflect.Int8:
wire = WireInt8
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*int8)(p) = r.ReadInt8()
return r
}
case reflect.Int16:
wire = WireInt16
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*int16)(p) = r.ReadInt16()
return r
}
case reflect.Int32:
wire = WireInt32
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*int32)(p) = r.ReadInt32()
return r
}
case reflect.Int64:
wire = WireInt64
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*int64)(p) = r.ReadInt64()
return r
}
case reflect.Float32:
wire = WireFloat32
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*float32)(p) = r.ReadFloat32()
return r
}
case reflect.Float64:
wire = WireFloat64
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*float64)(p) = r.ReadFloat64()
return r
}
case reflect.Bool:
wire = WireBool
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*bool)(p) = r.ReadBool()
return r
}
case reflect.Int:
wire = WireInt
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*int)(p) = r.ReadInt()
return r
}
case reflect.String:
wire = WireString
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*string)(p) = r.ReadString()
return r
}
case reflect.Map:
// map fields require specialized decoder
wire = WireMap
mpDec := newMapDecoderUsingTagAndOptsWithLimits(reflect.New(k).Elem().Interface(), usingTagName, opts, limits)
mpDec.subType = k
fun = func(p unsafe.Pointer, r Reader) Reader {
var em any = p
return mpDec.unmarshal(r, nil, em)
}
sub = mpDec
case reflect.Slice:
// slice fields need custom handling
slDec := newSliceDecoderUsingTagAndOptsWithLimits(reflect.New(k).Elem().Interface(), usingTagName, opts, limits)
slDec.subType = k
fun = func(p unsafe.Pointer, r Reader) Reader {
var em any = p
return slDec.unmarshal(r, nil, em)
}
sub = slDec
wire = slDec.kind
case reflect.Struct:
if k == timeType || (k.Kind() == reflect.Pointer && k.Elem() == timeType) {
wire = WireTime
fun = func(p unsafe.Pointer, r Reader) Reader {
*(*time.Time)(p) = r.ReadTime()
return r
}
break
}
if opts.Contains("encoder") {
wire = WireBytes
ft := k
if ft.Kind() == reflect.Pointer {
ft = ft.Elem()
}
if !reflect.PointerTo(ft).Implements(reflect.TypeOf((*binaryDecoder)(nil)).Elem()) {
panic("usage of encoder option with a Decoder requires an UnmarshalBinary method on the target field type struct. see glint.binaryDecoder")
}
fun = func(p unsafe.Pointer, r Reader) Reader {
// consume bytes even if reflection fails
bytes := r.ReadUint8Slice()
e, ok := reflect.NewAt(ft, p).Interface().(binaryDecoder)
if !ok {
return r
}
e.UnmarshalBinary(bytes)
return r
}
break
}
wire = WireStruct
// nested structs use recursive decoder
var inf = reflect.New(k).Elem().Interface() // create instance for schema
dec := newDecoderUsingTag(inf, usingTagName)
sub = dec // store for schema parsing phase
fun = func(p unsafe.Pointer, r Reader) Reader {
return dec.unmarshal(r, dec.instr, p)
}
}
if fun == nil && sub == nil {
panic(fmt.Sprintf("unsupported tye passed to reflectKindToAssigner: %v", k))
}
if wire == 0 {
panic(fmt.Sprintf("unsupported wire passed to reflectKindToAssigner: %v", k))
}
if pointerWrap {
wire |= WirePtrFlag
fun = deref(fun, 0, k)
}
return assigner{fun: fun, wire: wire, pointer: pointerWrap, subDecoder: sub, rkind: kind}
}
type reflectAssigner struct {
fun func(Reader) (reflect.Value, Reader)
subDecoder decoder
assigner assigner
}
func reflectKindToReflectValue(k reflect.Type, usingTagName string, opts tagOptions, limits DecodeLimits) reflectAssigner {
assigner := reflectKindToAssigner(k, usingTagName, opts, limits)
var fun func(Reader) (reflect.Value, Reader)
switch k.Kind() {
case reflect.Ptr:
reflectKindToReflectValue(k.Elem(), usingTagName, opts, limits)
a := reflectKindToReflectValue(k.Elem(), usingTagName, opts, limits)
assigner = a.assigner
fun = func(r Reader) (reflect.Value, Reader) {
if r.ReadByte() == 0 { // read the nil check byte
return reflect.ValueOf(nil), r
}
vv, re := a.fun(r)
return vv.Addr(), re
}
case reflect.Uint8:
fun = func(r Reader) (reflect.Value, Reader) {
var val uint8
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Uint16:
fun = func(r Reader) (reflect.Value, Reader) {
var val uint16
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Uint32:
fun = func(r Reader) (reflect.Value, Reader) {
var val uint32
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Uint64:
fun = func(r Reader) (reflect.Value, Reader) {
var val uint64
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Uint:
fun = func(r Reader) (reflect.Value, Reader) {
var val uint
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Int8:
fun = func(r Reader) (reflect.Value, Reader) {
var val int8
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Int16:
fun = func(r Reader) (reflect.Value, Reader) {
var val int16
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Int32:
fun = func(r Reader) (reflect.Value, Reader) {
var val int32
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Int64:
fun = func(r Reader) (reflect.Value, Reader) {
var val int64
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Float32:
fun = func(r Reader) (reflect.Value, Reader) {
var val float32
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Float64:
fun = func(r Reader) (reflect.Value, Reader) {
var val float64
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Bool:
fun = func(r Reader) (reflect.Value, Reader) {
var val bool
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Int:
fun = func(r Reader) (reflect.Value, Reader) {
var val int
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.String:
fun = func(r Reader) (reflect.Value, Reader) {
var val string
r = assigner.fun(unsafe.Pointer(&val), r)
return reflect.ValueOf(val), r
}
case reflect.Map:
fun = func(r Reader) (reflect.Value, Reader) {
m := reflect.MakeMap(k)
p := m.Pointer()
r = assigner.fun(unsafe.Pointer(&p), r)
return m, r
}
case reflect.Slice:
fun = func(r Reader) (reflect.Value, Reader) {
b := r.position
sl := int(r.ReadVarint())
r.Unread(r.position - b)
a := reflect.MakeSlice(k, sl, sl)
p := a.Interface()
pp := (*(*iface)(unsafe.Pointer(&p))).Data
sh := *(*sliceHeader)(unsafe.Pointer(pp))
r = assigner.fun(unsafe.Pointer(&sh), r)
return a, r
}
case reflect.Struct:
fun = func(r Reader) (reflect.Value, Reader) {
a := reflect.New(k)
r = assigner.fun(unsafe.Pointer(a.Pointer()), r)
return reflect.Indirect(a), r
}
}
if fun == nil {
panic(fmt.Sprintf("reflectKindToReflectValue could not resolve type %v", k))
}
return reflectAssigner{
fun: fun,
subDecoder: assigner.subDecoder,
assigner: assigner,
}
}
// AppendDynamicValue encodes a value with type prefix into the buffer
func AppendDynamicValue(v any, b *Buffer) {
switch val := v.(type) {
case string:
appendVarint(b, uint64(WireString))
b.AppendString(val)
case int:
appendVarint(b, uint64(WireInt))
b.AppendInt(val)
case int8:
appendVarint(b, uint64(WireInt8))
b.AppendInt8(val)
case int16:
appendVarint(b, uint64(WireInt16))
b.AppendInt16(val)
case int32:
appendVarint(b, uint64(WireInt32))
b.AppendInt32(val)
case int64:
appendVarint(b, uint64(WireInt64))
b.AppendInt64(val)
case uint:
appendVarint(b, uint64(WireUint))
b.AppendUint(val)
case uint8:
appendVarint(b, uint64(WireUint8))
b.AppendUint8(val)
case uint16:
appendVarint(b, uint64(WireUint16))
b.AppendUint16(val)
case uint32:
appendVarint(b, uint64(WireUint32))
b.AppendUint32(val)
case uint64:
appendVarint(b, uint64(WireUint64))
b.AppendUint64(val)
case float32:
appendVarint(b, uint64(WireFloat32))
b.AppendFloat32(val)
case float64:
appendVarint(b, uint64(WireFloat64))
b.AppendFloat64(val)
case bool:
appendVarint(b, uint64(WireBool))
b.AppendBool(val)
case time.Time:
appendVarint(b, uint64(WireTime))
b.AppendTime(val)
case *string:
appendVarint(b, uint64(WireString|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendString(*val)
case *int:
appendVarint(b, uint64(WireInt|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendInt(*val)
case *int8:
appendVarint(b, uint64(WireInt8|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendInt8(*val)
case *int16:
appendVarint(b, uint64(WireInt16|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendInt16(*val)
case *int32:
appendVarint(b, uint64(WireInt32|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendInt32(*val)
case *int64:
appendVarint(b, uint64(WireInt64|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendInt64(*val)
case *uint:
appendVarint(b, uint64(WireUint|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendUint(*val)
case *uint8:
appendVarint(b, uint64(WireUint8|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendUint8(*val)
case *uint16:
appendVarint(b, uint64(WireUint16|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendUint16(*val)
case *uint32:
appendVarint(b, uint64(WireUint32|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendUint32(*val)
case *uint64:
appendVarint(b, uint64(WireUint64|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendUint64(*val)
case *float32:
appendVarint(b, uint64(WireFloat32|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendFloat32(*val)
case *float64:
appendVarint(b, uint64(WireFloat64|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendFloat64(*val)
case *bool:
appendVarint(b, uint64(WireBool|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendBool(*val)
case *time.Time:
appendVarint(b, uint64(WireTime|WirePtrFlag))
if appendNil(val, b) {
return
}
b.AppendTime(*val)
case []string:
appendVarint(b, uint64(WireString|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendString(val[i])
}
case []int:
appendVarint(b, uint64(WireInt|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendInt(val[i])
}
case []int8:
appendVarint(b, uint64(WireInt8|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendInt8(val[i])
}
case []int16:
appendVarint(b, uint64(WireInt16|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendInt16(val[i])
}
case []int32:
appendVarint(b, uint64(WireInt32|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendInt32(val[i])
}
case []int64:
appendVarint(b, uint64(WireInt64|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendInt64(val[i])
}
case []uint:
appendVarint(b, uint64(WireUint|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendUint(val[i])
}
case []uint8:
appendVarint(b, uint64(WireUint8|WireSliceFlag))
b.AppendUint(uint(len(val)))
b.AppendBytes(val)
case []uint16:
appendVarint(b, uint64(WireUint16|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendUint16(val[i])
}
case []uint32:
appendVarint(b, uint64(WireUint32|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendUint32(val[i])
}
case []uint64:
appendVarint(b, uint64(WireUint64|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendUint64(val[i])
}
case []float32:
appendVarint(b, uint64(WireFloat32|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendFloat32(val[i])
}
case []float64:
appendVarint(b, uint64(WireFloat64|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendFloat64(val[i])
}
case []bool:
appendVarint(b, uint64(WireBool|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendBool(val[i])
}
case []time.Time:
appendVarint(b, uint64(WireTime|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendTime(val[i])
}
case [][]byte:
appendVarint(b, uint64(WireBytes|WireSliceFlag))
b.AppendUint(uint(len(val)))
for i := 0; i < len(val); i++ {
b.AppendBytes(val[i])
}
default:
panic(fmt.Sprintf("unsupported type %T", val))
// Handle other types as needed
}
}
// appendNil writes a nil marker (0) or presence marker (1), returning true for nil
func appendNil(val any, b *Buffer) bool {
if val == nil {
b.Bytes = append(b.Bytes, 0)
return true
}
b.Bytes = append(b.Bytes, 1)
return false
}
// DynamicValue encodes any value to bytes with type information
func DynamicValue(value any) []byte {
b := Buffer{}
AppendDynamicValue(value, &b)
return b.Bytes
}
// ReadDynamicValue decodes a value using the wire type from the first varint
func ReadDynamicValue(b []byte) any {
r := NewReader(b)
return ReadDynamicValueFromReader(&r)
}
// ReadDynamicValueFromReader extracts a typed value after reading the wire type indicator
func ReadDynamicValueFromReader(r *Reader) any {
wire := WireType(r.ReadVarint())
if wire&WirePtrFlag > 0 {
p := r.ReadByte()
if p == 0 {
return nil
}
wire &= ^WirePtrFlag
}
if wire&WireSliceFlag > 0 {
return ReadDynamicSlice(r, wire)
}
switch wire {
case WireString:
return r.ReadString()
case WireInt:
return r.ReadInt()
case WireInt8:
return r.ReadInt8()
case WireInt16:
return r.ReadInt16()
case WireInt32:
return r.ReadInt32()
case WireInt64:
return r.ReadInt64()
case WireUint:
return r.ReadUint()
case WireUint8: