forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_ptr.go
66 lines (50 loc) · 1.46 KB
/
codec_ptr.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
package avro
import (
"errors"
"unsafe"
"github.com/modern-go/reflect2"
)
func decoderOfPtr(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValDecoder {
ptrType := typ.(*reflect2.UnsafePtrType)
elemType := ptrType.Elem()
decoder := decoderOfType(cfg, schema, elemType)
return &dereferenceDecoder{typ: elemType, decoder: decoder}
}
type dereferenceDecoder struct {
typ reflect2.Type
decoder ValDecoder
}
func (d *dereferenceDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
if *((*unsafe.Pointer)(ptr)) == nil {
// Create new instance
newPtr := d.typ.UnsafeNew()
d.decoder.Decode(newPtr, r)
*((*unsafe.Pointer)(ptr)) = newPtr
return
}
// Reuse existing instance
d.decoder.Decode(*((*unsafe.Pointer)(ptr)), r)
}
func encoderOfPtr(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValEncoder {
ptrType := typ.(*reflect2.UnsafePtrType)
elemType := ptrType.Elem()
enc := encoderOfType(cfg, schema, elemType)
return &dereferenceEncoder{typ: elemType, encoder: enc}
}
type dereferenceEncoder struct {
typ reflect2.Type
encoder ValEncoder
}
func (d *dereferenceEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
if *((*unsafe.Pointer)(ptr)) == nil {
w.Error = errors.New("avro: cannot encode nil pointer")
return
}
d.encoder.Encode(*((*unsafe.Pointer)(ptr)), w)
}
type referenceDecoder struct {
decoder ValDecoder
}
func (decoder *referenceDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
decoder.decoder.Decode(unsafe.Pointer(&ptr), r)
}