-
Notifications
You must be signed in to change notification settings - Fork 98
/
codec_dynamic.go
59 lines (49 loc) · 1.05 KB
/
codec_dynamic.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
package avro
import (
"reflect"
"unsafe"
"github.com/modern-go/reflect2"
)
type efaceDecoder struct {
schema Schema
typ reflect2.Type
dec ValDecoder
}
func newEfaceDecoder(d *decoderContext, schema Schema) *efaceDecoder {
typ, _ := genericReceiver(schema)
dec := decoderOfType(d, schema, typ)
return &efaceDecoder{
schema: schema,
typ: typ,
dec: dec,
}
}
func (d *efaceDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
pObj := (*any)(ptr)
if *pObj == nil {
*pObj = genericDecode(d.typ, d.dec, r)
return
}
typ := reflect2.TypeOf(*pObj)
if typ.Kind() != reflect.Ptr {
*pObj = genericDecode(d.typ, d.dec, r)
return
}
ptrType := typ.(*reflect2.UnsafePtrType)
ptrElemType := ptrType.Elem()
if reflect2.IsNil(*pObj) {
obj := ptrElemType.New()
r.ReadVal(d.schema, obj)
*pObj = obj
return
}
r.ReadVal(d.schema, *pObj)
}
type interfaceEncoder struct {
schema Schema
typ reflect2.Type
}
func (e *interfaceEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
obj := e.typ.UnsafeIndirect(ptr)
w.WriteVal(e.schema, obj)
}