-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
1088 lines (969 loc) · 25.3 KB
/
encode.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 [2024] [https://github.com/gnolizuh]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package amf
import (
"bytes"
"encoding/binary"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
"unicode"
)
type EncoderError struct {
Type reflect.Type
Err error
}
func (e *EncoderError) Error() string {
return "amf: error calling EncodeAMF for type " + e.Type.String() + ": " + e.Err.Error()
}
// A MarshalerError represents an error from calling a MarshalAMF method.
type MarshalerError struct {
Type reflect.Type
Err error
sourceFunc string
}
func (e *MarshalerError) Error() string {
srcFunc := e.sourceFunc
if srcFunc == StringEmpty {
srcFunc = "MarshalAMF"
}
return "amf: error calling " + srcFunc +
" for type " + e.Type.String() +
": " + e.Err.Error()
}
// An encodeState encodes AMF into a bytes.Buffer.
type encodeState struct {
bytes.Buffer // accumulated output
strReference Reference
objReference Reference
}
func (e *encodeState) ResetState() {
e.Reset()
e.strReference.reset()
e.objReference.reset()
}
var encodeStatePool sync.Pool
func newEncodeState() *encodeState {
if v := encodeStatePool.Get(); v != nil {
e := v.(*encodeState)
e.ResetState()
return e
}
return &encodeState{
strReference: Reference{m: make(map[any]int), a: make([]any, 0)},
objReference: Reference{m: make(map[any]int), a: make([]any, 0)},
}
}
// amfError is an error wrapper type for internal use only.
// Panics with errors are wrapped in amfError so that the top-level recover
// can distinguish intentional panics from this package.
type amfError struct{ error }
func (e *encodeState) marshal(v any, opts encOpts) (err error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
if s, ok := r.(string); ok {
panic(s)
}
err = r.(error)
}
}()
e.reflectValue(reflect.ValueOf(v), opts)
return nil
}
// error aborts the encoding by panicking with err wrapped in amfError.
func (e *encodeState) error(err error) {
panic(amfError{err})
}
func (e *encodeState) encodeError(err error) {
panic(err)
}
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return v.Bool() == false
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Pointer:
return v.IsNil()
default:
return false
}
}
func (e *encodeState) reflectValue(v reflect.Value, opts encOpts) {
valueEncoder(v)(e, v, opts)
}
type encOpts struct {
// v3 means AMF version, which is true for AMF3, default is AMF0.
v3 bool
// quoted causes primitive fields to be encoded inside AMF strings.
quoted bool
}
// writeObjectName AMF0 only
func (e *encodeState) writeObjectName(s string) {
e.writeUint(uint16(len(s)))
e.Write([]byte(s))
}
func (e *encodeState) writeMarker(m byte) {
e.WriteByte(m)
}
type reflectWithString struct {
v reflect.Value
s string
}
func (w *reflectWithString) resolve() error {
if w.v.Kind() == reflect.String {
w.s = w.v.String()
return nil
}
switch w.v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
w.s = strconv.FormatInt(w.v.Int(), 10)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
w.s = strconv.FormatUint(w.v.Uint(), 10)
return nil
default:
panic("unexpected map key type")
}
}
// encodeString encoding rule:
// AMF0:
// - len(s) <= 0: string-marker[1B] + length[2B] + s
// - len(s) > 0xffff: long-string-marker[1B] + length[4B] + s
//
// AMF3:
// - no cache: string-marker[1B] + length[29b] + s
// - cached: string-marker[1B] + index[29b]
func (e *encodeState) encodeString(s string, opts encOpts) {
if !opts.v3 {
l := len(s)
if l > LongStringSize {
e.writeMarker(LongStringMarker0)
e.writeUint(uint32(l))
} else {
e.writeMarker(StringMarker0)
e.writeUint(uint16(l))
}
e.Write([]byte(s))
} else {
e.writeMarker(StringMarker3)
i, ok := e.strReference.get(s)
if ok {
e.writeU29(uint32(i << 1))
return
}
l := len(s)
e.writeU29(uint32((l << 1) | 1))
_ = e.strReference.set(s)
e.Write([]byte(s))
}
}
func (e *encodeState) writeUint(u any) {
_ = binary.Write(e, binary.BigEndian, u)
}
// writeU29 AMF3 only.
func (e *encodeState) writeU29(v uint32) {
v = v & 0x1fffffff
b := make([]byte, 0, 4)
switch {
case v < 0x80:
b = append(b, byte(v))
case v < 0x4000:
b = append(b, byte((v>>7)|0x80))
b = append(b, byte(v&0x7f))
case v < 0x200000:
b = append(b, byte((v>>14)|0x80))
b = append(b, byte((v>>7)|0x80))
b = append(b, byte(v&0x7f))
case v < 0x20000000:
b = append(b, byte((v>>22)|0x80))
b = append(b, byte((v>>15)|0x80))
b = append(b, byte((v>>7)|0x80))
b = append(b, byte(v&0xff))
default:
return
}
e.Write(b)
}
// encodeBool encoding rule:
//
// AMF0:
// - bool-marker[1B] + b
//
// AMF3:
// - true: true-marker[1B]
// - false: false-marker[1B]
func (e *encodeState) encodeBool(b bool, opts encOpts) {
if !opts.v3 {
e.writeMarker(BooleanMarker0)
if b {
e.WriteByte(1)
} else {
e.WriteByte(0)
}
} else {
if b {
e.WriteByte(TrueMarker3)
} else {
e.WriteByte(FalseMarker3)
}
}
}
// encodeInt encoding rule:
//
// AMF0:
// - number-marker[1B] + i[8B]
//
// AMF3:
// - -2^28 <= i <= 2^28: integer-marker[1B] + i[29b]
// - else: encode as AMF3 double.
func (e *encodeState) encodeInt(i int64, opts encOpts) {
if !opts.v3 {
e.encodeFloat64(float64(i), opts)
} else {
if i < Int28Min && i >= Int28Max {
e.encodeFloat64(float64(i), opts)
} else {
e.writeMarker(IntegerMarker3)
e.writeU29(uint32(i))
}
}
}
// encodeUInt it's same with encodeInt.
func (e *encodeState) encodeUInt(ui uint64, opts encOpts) {
if !opts.v3 {
e.encodeFloat64(float64(ui), opts)
} else {
if ui >= UInt29Max {
e.encodeFloat64(float64(ui), opts)
} else {
e.writeMarker(IntegerMarker3)
e.writeU29(uint32(ui))
}
}
}
// encodeDate:
//
// - AMF0: date-marker[1B] DOUBLE[8B] time-zone[2B]
// - AMF3: date-marker (U29O-ref | (U29D-value date-time))
func (e *encodeState) encodeDate(v reflect.Value, opts encOpts) {
t := v.Interface().(time.Time)
if !opts.v3 {
e.writeMarker(DateMarker0)
_ = binary.Write(e, binary.BigEndian, t.UnixMilli())
_ = binary.Write(e, binary.BigEndian, 0x0000) // time-zone
} else {
e.writeMarker(DateMarker3)
i, ok := e.objReference.get(v)
if ok {
e.writeU29(uint32(i << 1))
return
}
_ = e.objReference.set(v)
e.writeU29(uint32(0x01))
e.writeUint(t.UnixMilli())
}
}
// encodeFloat64 encoding rule:
//
// AMF0:
// - number-marker[1B] + i[8B]
//
// AMF3:
// - double-marker[1B] + i[8B]
func (e *encodeState) encodeFloat64(f float64, opts encOpts) {
if !opts.v3 {
e.writeMarker(NumberMarker0)
_ = binary.Write(e, binary.BigEndian, f)
} else {
e.writeMarker(DoubleMarker3)
_ = binary.Write(e, binary.BigEndian, f)
}
}
func (e *encodeState) encodeNull(opts encOpts) {
if !opts.v3 {
e.WriteByte(NullMarker0)
} else {
e.WriteByte(NullMarker3)
}
}
type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts)
var encoderCache sync.Map
func valueEncoder(v reflect.Value) encoderFunc {
if !v.IsValid() {
return invalidValueEncoder
}
return typeEncoder(v.Type())
}
func typeEncoder(t reflect.Type) encoderFunc {
if fi, ok := encoderCache.Load(t); ok {
return fi.(encoderFunc)
}
var (
wg sync.WaitGroup
f encoderFunc
)
wg.Add(1)
fi, loaded := encoderCache.LoadOrStore(t, encoderFunc(func(e *encodeState, v reflect.Value, opts encOpts) {
wg.Wait()
f(e, v, opts)
}))
if loaded {
return fi.(encoderFunc)
}
f = newTypeEncoder(t)
wg.Done()
encoderCache.Store(t, f)
return f
}
var (
marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
)
func newTypeEncoder(t reflect.Type) encoderFunc {
if t.Implements(marshalerType) {
return marshalerEncoder
}
switch t.Kind() {
case reflect.String:
return stringEncoder
case reflect.Bool:
return boolEncoder
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return intEncoder
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return uintEncoder
case reflect.Float32, reflect.Float64:
return floatEncoder
case reflect.Interface:
return interfaceEncoder
case reflect.Struct: // struct & map are type of Object Marker.
return newStructEncoder(t)
case reflect.Map:
return newMapEncoder(t)
case reflect.Slice: // slice & array are type of Strict Array Marker.
return newSliceEncoder(t)
case reflect.Array:
return newArrayEncoder(t)
case reflect.Ptr:
return newPtrEncoder(t)
default:
return newDefaultEncoder()
}
}
func marshalerEncoder(e *encodeState, v reflect.Value, _ encOpts) {
if v.Kind() == reflect.Pointer && v.IsNil() {
e.WriteString("null")
return
}
m, ok := v.Interface().(Marshaler)
if !ok {
e.WriteString("null")
return
}
b, err := m.MarshalAMF()
if err == nil {
e.Grow(len(b))
out := e.AvailableBuffer()
e.Buffer.Write(out)
}
if err != nil {
e.error(&MarshalerError{v.Type(), err, "MarshalAMF"})
}
}
func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
e.encodeString(v.String(), opts)
}
func boolEncoder(e *encodeState, v reflect.Value, opts encOpts) {
e.encodeBool(v.Bool(), opts)
}
func invalidValueEncoder(e *encodeState, _ reflect.Value, opts encOpts) {
e.encodeNull(opts)
}
func intEncoder(e *encodeState, v reflect.Value, opts encOpts) {
e.encodeInt(v.Int(), opts)
}
func uintEncoder(e *encodeState, v reflect.Value, opts encOpts) {
e.encodeUInt(v.Uint(), opts)
}
func floatEncoder(e *encodeState, v reflect.Value, opts encOpts) {
e.encodeFloat64(v.Float(), opts)
}
func dateEncoder(e *encodeState, v reflect.Value, opts encOpts) {
e.encodeDate(v, opts)
}
func interfaceEncoder(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() {
e.encodeNull(opts)
return
}
e.reflectValue(v.Elem(), opts)
}
func isValidTag(s string) bool {
if s == StringEmpty {
return false
}
for _, c := range s {
switch {
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
default:
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
return false
}
}
}
return true
}
func typeByIndex(t reflect.Type, index []int) reflect.Type {
for _, i := range index {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
t = t.Field(i).Type
}
return t
}
type structEncoder struct {
fields structFields
}
type structFields struct {
list []field
byExactName map[string]*field
byFoldedName map[string]*field
}
// structEncoder encoding rule:
//
// AMF0:
// - object-marker[1B] + [object...] + "" + end-object-marker[1B]
//
// AMF3:
// 1. object-marker U29O-ref *value-type *dynamic-member
// 2. object-marker U29O-traits-ext class-name *(UTF-8-vr) *value-type *dynamic-member
// 3. object-marker U29O-traits-ref *value-type *dynamic-member
// 4. object-marker U29O-traits class-name *(UTF-8-vr) *value-type *dynamic-member
func (se *structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
// write start marker
if !opts.v3 {
e.writeMarker(ObjectMarker0)
} else {
e.writeMarker(ObjectMarker3)
i, ok := e.objReference.get(v)
if ok {
e.writeU29(uint32(i << 1))
return
}
_ = e.objReference.set(v)
e.writeU29(U29NoTraits)
e.encodeString(StringEmpty, opts)
}
FieldLoop:
// write object
for i := range se.fields.list {
f := &se.fields.list[i]
// Find the nested struct field by following f.index.
fv := v
for _, i := range f.index {
if fv.Kind() == reflect.Pointer {
if fv.IsNil() {
continue FieldLoop
}
fv = fv.Elem()
}
fv = fv.Field(i)
}
if f.omitEmpty && isEmptyValue(fv) {
continue
}
opts.quoted = f.quoted
if !opts.v3 {
e.writeObjectName(f.name)
} else {
e.encodeString(f.name, opts)
}
if f.xml && fv.Kind() == reflect.String {
encodeXML(e, fv, opts)
} else {
f.encoder(e, fv, opts)
}
}
// write end marker
if !opts.v3 {
e.writeObjectName(StringEmpty)
e.writeMarker(ObjectEndMarker0)
} else {
e.encodeString(StringEmpty, opts)
}
}
func newStructEncoder(t reflect.Type) encoderFunc {
se := structEncoder{fields: cachedTypeFields(t)}
return se.encode
}
type mapEncoder struct {
elemEnc encoderFunc
}
func (mae *mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() {
e.encodeNull(opts)
return
}
keys := v.MapKeys()
sv := make([]reflectWithString, len(keys))
for i, v := range keys {
sv[i].v = v
if err := sv[i].resolve(); err != nil {
e.encodeError(&EncoderError{v.Type(), err})
}
}
sort.Slice(sv, func(i, j int) bool { return sv[i].s < sv[j].s })
// write start marker
if !opts.v3 {
e.writeMarker(ObjectMarker0)
} else {
e.writeMarker(ObjectMarker3)
i, ok := e.objReference.get(v)
if ok {
e.writeU29(uint32(i << 1))
return
}
_ = e.objReference.set(v)
e.writeU29(U29NoTraits)
e.encodeString(StringEmpty, opts)
}
// write object
for _, kv := range sv {
if !opts.v3 {
e.writeObjectName(kv.s)
} else {
e.encodeString(kv.s, opts)
}
mae.elemEnc(e, v.MapIndex(kv.v), opts)
}
// write end marker
if !opts.v3 {
e.writeObjectName(StringEmpty)
e.writeMarker(ObjectEndMarker0)
} else {
e.encodeString(StringEmpty, opts)
}
}
func newMapEncoder(t reflect.Type) encoderFunc {
switch t.Key().Kind() {
case reflect.String,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
mae := &mapEncoder{elemEnc: typeEncoder(t.Elem())}
return mae.encode
default:
return unsupportedTypeEncoder
}
}
func encodeXML(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() {
e.encodeNull(opts)
return
}
s := v.Interface().(string)
if !opts.v3 {
e.writeMarker(XMLDocumentMarker0)
e.writeUint(uint32(len(s)))
e.WriteString(s)
} else {
e.writeMarker(XMLDocMarker3)
i, ok := e.objReference.get(v)
if ok {
e.writeU29(uint32(i << 1))
return
}
_ = e.objReference.set(v)
e.writeU29(uint32((i << 1) | 1))
e.WriteString(s)
}
}
type sliceEncoder struct {
arrayEnc encoderFunc
}
func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() {
e.encodeNull(opts)
return
}
se.arrayEnc(e, v, opts)
}
func newSliceEncoder(t reflect.Type) encoderFunc {
enc := &sliceEncoder{arrayEnc: newArrayEncoder(t)}
return enc.encode
}
type arrayEncoder struct {
elemEnc encoderFunc
}
// arrayEncoder
// - AMF0: strict-array-marker array-count[4B] *(value-type)
// - AMF3: array-marker (U29O-ref[29b] | (U29A-value (UTF-8-empty | *(assoc-value) UTF-8-empty) *(value-type)))
// assoc-value means value with name, value-type means value with no name.
func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() || v.Len() == 0 {
if !opts.v3 {
e.writeMarker(UndefinedMarker0)
} else {
e.writeMarker(UndefinedMarker3)
}
return
}
if !opts.v3 {
e.writeMarker(StrictArrayMarker0)
e.writeUint(uint32(v.Len()))
} else {
e.writeMarker(ArrayMarker3)
i, ok := e.objReference.get(v)
if ok {
e.writeU29(uint32(i << 1))
return
}
_ = e.objReference.set(v)
e.writeU29((uint32(v.Len()) << 1) | 1)
e.WriteByte(UTF8Empty) // no assoc-value field supported, followed by value-types.
}
for i := 0; i < v.Len(); i++ {
ae.elemEnc(e, v.Index(i), opts)
}
}
func newArrayEncoder(t reflect.Type) encoderFunc {
enc := &arrayEncoder{elemEnc: typeEncoder(t.Elem())}
return enc.encode
}
type ptrEncoder struct {
elemEnc encoderFunc
}
func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() {
e.encodeNull(opts)
return
}
pe.elemEnc(e, v.Elem(), opts)
}
func newPtrEncoder(t reflect.Type) encoderFunc {
enc := &ptrEncoder{elemEnc: typeEncoder(t.Elem())}
return enc.encode
}
type defaultEncoder struct {
elemEnc encoderFunc
}
func newDefaultEncoder() encoderFunc {
de := defaultEncoder{}
return de.encode
}
func (de *defaultEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
switch v.Interface().(type) {
case time.Time:
dateEncoder(e, v, opts)
default:
unsupportedTypeEncoder(e, v, opts)
}
}
func unsupportedTypeEncoder(e *encodeState, v reflect.Value, _ encOpts) {
e.encodeError(&UnsupportedTypeError{v.Type()})
}
type UnsupportedTypeError struct {
Type reflect.Type
}
func (e *UnsupportedTypeError) Error() string {
return "amf: unsupported type: " + e.Type.String()
}
// Marshal returns the AMF encoding of v.
//
// Marshal traverses the value v recursively.
// If an encountered value implements the Marshaler interface
// and is not a nil pointer, Marshal calls its MarshalAMF method
// to produce AMF.
//
// Otherwise, Marshal uses the following type-dependent default encodings:
//
// Boolean values encode as AMF booleans.
//
// Floating point, integer, and Number values encode as AMF numbers.
//
// String values encode as AMF strings coerced to valid UTF-8,
// replacing invalid bytes with the Unicode replacement rune.
//
// Array and slice values encode as AMF arrays, except that
// []byte encodes as a base64-encoded string, and a nil slice
// encodes as the null AMF value.
//
// Struct, Map values encode as AMF objects.
// Each exported struct field becomes a member of the object.
//
// Pointer values encode as the value pointed to.
// A nil pointer encodes as the null AMF value.
//
// Interface values encode as the value contained in the interface.
// A nil interface value encodes as the null AMF value.
//
// Channel, complex, and function values cannot be encoded in AMF.
// Attempting to encode such a value causes Marshal to return
// an UnsupportedTypeError.
//
// AMF cannot represent cyclic data structures and Marshal does not
// handle them. Passing cyclic structures to Marshal will result in
// an error.
func Marshal(vs ...any) ([]byte, error) {
e := newEncodeState()
defer encodeStatePool.Put(e)
for _, v := range vs {
err := e.marshal(v, encOpts{})
if err != nil {
return nil, err
}
}
return e.Bytes(), nil
}
// Marshaler is the interface implemented by types that
// can marshal themselves into valid AMF.
type Marshaler interface {
MarshalAMF() ([]byte, error)
}
// A field represents a single field found in a struct.
type field struct {
name string
nameBytes []byte // []byte(name)
tag bool
index []int
typ reflect.Type
omitEmpty bool
xml bool
quoted bool
encoder encoderFunc
}
// byIndex sorts field by index sequence.
type byIndex []field
func (x byIndex) Len() int { return len(x) }
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x byIndex) Less(i, j int) bool {
for k, xik := range x[i].index {
if k >= len(x[j].index) {
return false
}
if xik != x[j].index[k] {
return xik < x[j].index[k]
}
}
return len(x[i].index) < len(x[j].index)
}
// typeFields returns a list of fields that AMF should recognize for the given type.
// The algorithm is breadth-first search over the set of structs to include - the top struct
// and then any reachable anonymous structs.
func typeFields(t reflect.Type) structFields {
// Anonymous fields to explore at the current level and the next.
var current []field
next := []field{{typ: t}}
// Count of queued names for current level and the next.
var count, nextCount map[reflect.Type]int
// Types already visited at an earlier level.
visited := map[reflect.Type]bool{}
// Fields found.
var fields []field
for len(next) > 0 {
current, next = next, current[:0]
count, nextCount = nextCount, map[reflect.Type]int{}
for _, f := range current {
if visited[f.typ] {
continue
}
visited[f.typ] = true
// Scan f.typ for fields to include.
for i := 0; i < f.typ.NumField(); i++ {
sf := f.typ.Field(i)
if sf.Anonymous {
t := sf.Type
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
if !sf.IsExported() && t.Kind() != reflect.Struct {
// Ignore embedded fields of unexported non-struct types.
continue
}
// Do not ignore embedded fields of unexported struct types
// since they may have exported fields.
} else if !sf.IsExported() {
// Ignore unexported non-embedded fields.
continue
}
tag := sf.Tag.Get("amf")
if tag == "-" {
continue
}
name, opts := parseTag(tag)
if !isValidTag(name) {
name = StringEmpty
}
index := make([]int, len(f.index)+1)
copy(index, f.index)
index[len(f.index)] = i
ft := sf.Type
if ft.Name() == StringEmpty && ft.Kind() == reflect.Pointer {
// Follow pointer.
ft = ft.Elem()
}
// Only strings, floats, integers, and booleans can be quoted.
quoted := false
if opts.Contains("string") {
switch ft.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64,
reflect.String:
quoted = true
default:
// do nothing.
}
}
// Record found field and index sequence.
if name != StringEmpty || !sf.Anonymous || ft.Kind() != reflect.Struct {
tagged := name != StringEmpty
if name == StringEmpty {
name = sf.Name
}
field := field{
name: name,
tag: tagged,
index: index,
typ: ft,
omitEmpty: opts.Contains("omitempty"),
xml: opts.Contains("xml"),
quoted: quoted,
}
field.nameBytes = []byte(field.name)
fields = append(fields, field)
if count[f.typ] > 1 {
// If there were multiple instances, add a second,
// so that the annihilation code will see a duplicate.
// It only cares about the distinction between 1 or 2,
// so don't bother generating any more copies.
fields = append(fields, fields[len(fields)-1])
}
continue
}
// Record new anonymous struct to explore in next round.
nextCount[ft]++
if nextCount[ft] == 1 {
next = append(next, field{name: ft.Name(), index: index, typ: ft})
}
}
}
}
sort.Slice(fields, func(i, j int) bool {
x := fields
// sort field by name, breaking ties with depth, then
// breaking ties with "name came from amf tag", then
// breaking ties with index sequence.
if x[i].name != x[j].name {
return x[i].name < x[j].name
}
if len(x[i].index) != len(x[j].index) {
return len(x[i].index) < len(x[j].index)
}
if x[i].tag != x[j].tag {