github.com/aacfactory/avro@v1.2.12/internal/base/codec_dynamic.go (about) 1 package base 2 3 import ( 4 "reflect" 5 "unsafe" 6 7 "github.com/modern-go/reflect2" 8 ) 9 10 type efaceDecoder struct { 11 schema Schema 12 } 13 14 func (d *efaceDecoder) Decode(ptr unsafe.Pointer, r *Reader) { 15 pObj := (*any)(ptr) 16 obj := *pObj 17 if obj == nil { 18 *pObj = r.ReadNext(d.schema) 19 return 20 } 21 22 typ := reflect2.TypeOf(obj) 23 if typ.Kind() != reflect.Ptr { 24 *pObj = r.ReadNext(d.schema) 25 return 26 } 27 28 ptrType := typ.(*reflect2.UnsafePtrType) 29 ptrElemType := ptrType.Elem() 30 if reflect2.IsNil(obj) { 31 obj := ptrElemType.New() 32 r.ReadVal(d.schema, obj) 33 *pObj = obj 34 return 35 } 36 r.ReadVal(d.schema, obj) 37 } 38 39 type interfaceEncoder struct { 40 schema Schema 41 typ reflect2.Type 42 } 43 44 func (e *interfaceEncoder) Encode(ptr unsafe.Pointer, w *Writer) { 45 obj := e.typ.UnsafeIndirect(ptr) 46 w.WriteVal(e.schema, obj) 47 }