forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
247 lines (212 loc) · 6.32 KB
/
codec.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
package avro
import (
"fmt"
"math/big"
"reflect"
"time"
"unsafe"
"github.com/modern-go/reflect2"
)
var (
timeType = reflect.TypeOf(time.Time{})
ratType = reflect.TypeOf(big.Rat{})
durType = reflect.TypeOf(LogicalDuration{})
)
type null struct{}
// ValDecoder represents an internal value decoder.
//
// You should never use ValDecoder directly.
type ValDecoder interface {
Decode(ptr unsafe.Pointer, r *Reader)
}
// ValEncoder represents an internal value encoder.
//
// You should never use ValEncoder directly.
type ValEncoder interface {
Encode(ptr unsafe.Pointer, w *Writer)
}
// ReadVal parses Avro value and stores the result in the value pointed to by obj.
func (r *Reader) ReadVal(schema Schema, obj any) {
decoder := r.cfg.getDecoderFromCache(schema.CacheFingerprint(), reflect2.RTypeOf(obj))
if decoder == nil {
typ := reflect2.TypeOf(obj)
if typ.Kind() != reflect.Ptr {
r.ReportError("ReadVal", "can only unmarshal into pointer")
return
}
decoder = r.cfg.DecoderOf(schema, typ)
}
ptr := reflect2.PtrOf(obj)
if ptr == nil {
r.ReportError("ReadVal", "can not read into nil pointer")
return
}
decoder.Decode(ptr, r)
}
// WriteVal writes the Avro encoding of obj.
func (w *Writer) WriteVal(schema Schema, val any) {
encoder := w.cfg.getEncoderFromCache(schema.Fingerprint(), reflect2.RTypeOf(val))
if encoder == nil {
typ := reflect2.TypeOf(val)
encoder = w.cfg.EncoderOf(schema, typ)
}
encoder.Encode(reflect2.PtrOf(val), w)
}
func (c *frozenConfig) DecoderOf(schema Schema, typ reflect2.Type) ValDecoder {
rtype := typ.RType()
decoder := c.getDecoderFromCache(schema.CacheFingerprint(), rtype)
if decoder != nil {
return decoder
}
ptrType := typ.(*reflect2.UnsafePtrType)
decoder = decoderOfType(newDecoderContext(c), schema, ptrType.Elem())
c.addDecoderToCache(schema.CacheFingerprint(), rtype, decoder)
return decoder
}
type deferDecoder struct {
decoder ValDecoder
}
func (d *deferDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
d.decoder.Decode(ptr, r)
}
type deferEncoder struct {
encoder ValEncoder
}
func (d *deferEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
d.encoder.Encode(ptr, w)
}
type decoderContext struct {
cfg *frozenConfig
decoders map[cacheKey]ValDecoder
}
func newDecoderContext(cfg *frozenConfig) *decoderContext {
return &decoderContext{
cfg: cfg,
decoders: make(map[cacheKey]ValDecoder),
}
}
type encoderContext struct {
cfg *frozenConfig
encoders map[cacheKey]ValEncoder
}
func newEncoderContext(cfg *frozenConfig) *encoderContext {
return &encoderContext{
cfg: cfg,
encoders: make(map[cacheKey]ValEncoder),
}
}
func decoderOfType(d *decoderContext, schema Schema, typ reflect2.Type) ValDecoder {
if dec := createDecoderOfMarshaler(schema, typ); dec != nil {
return dec
}
// Handle eface (empty interface) case when it isn't a union
if typ.Kind() == reflect.Interface && schema.Type() != Union {
if _, ok := typ.(*reflect2.UnsafeIFaceType); !ok {
return newEfaceDecoder(d, schema)
}
}
switch schema.Type() {
case String, Bytes, Int, Long, Float, Double, Boolean:
return createDecoderOfNative(schema.(*PrimitiveSchema), typ)
case Record:
key := cacheKey{fingerprint: schema.CacheFingerprint(), rtype: typ.RType()}
defDec := &deferDecoder{}
d.decoders[key] = defDec
defDec.decoder = createDecoderOfRecord(d, schema.(*RecordSchema), typ)
return defDec.decoder
case Ref:
key := cacheKey{fingerprint: schema.(*RefSchema).Schema().CacheFingerprint(), rtype: typ.RType()}
if dec, f := d.decoders[key]; f {
return dec
}
return decoderOfType(d, schema.(*RefSchema).Schema(), typ)
case Enum:
return createDecoderOfEnum(schema.(*EnumSchema), typ)
case Array:
return createDecoderOfArray(d, schema.(*ArraySchema), typ)
case Map:
return createDecoderOfMap(d, schema.(*MapSchema), typ)
case Union:
return createDecoderOfUnion(d, schema.(*UnionSchema), typ)
case Fixed:
return createDecoderOfFixed(schema.(*FixedSchema), typ)
default:
// It is impossible to get here with a valid schema
return &errorDecoder{err: fmt.Errorf("avro: schema type %s is unsupported", schema.Type())}
}
}
func (c *frozenConfig) EncoderOf(schema Schema, typ reflect2.Type) ValEncoder {
if typ == nil {
typ = reflect2.TypeOf((*null)(nil))
}
rtype := typ.RType()
encoder := c.getEncoderFromCache(schema.Fingerprint(), rtype)
if encoder != nil {
return encoder
}
encoder = encoderOfType(newEncoderContext(c), schema, typ)
if typ.LikePtr() {
encoder = &onePtrEncoder{encoder}
}
c.addEncoderToCache(schema.Fingerprint(), rtype, encoder)
return encoder
}
type onePtrEncoder struct {
enc ValEncoder
}
func (e *onePtrEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
e.enc.Encode(noescape(unsafe.Pointer(&ptr)), w)
}
func encoderOfType(e *encoderContext, schema Schema, typ reflect2.Type) ValEncoder {
if enc := createEncoderOfMarshaler(schema, typ); enc != nil {
return enc
}
if typ.Kind() == reflect.Interface {
return &interfaceEncoder{schema: schema, typ: typ}
}
switch schema.Type() {
case String, Bytes, Int, Long, Float, Double, Boolean, Null:
return createEncoderOfNative(schema, typ)
case Record:
key := cacheKey{fingerprint: schema.Fingerprint(), rtype: typ.RType()}
defEnc := &deferEncoder{}
e.encoders[key] = defEnc
defEnc.encoder = createEncoderOfRecord(e, schema.(*RecordSchema), typ)
return defEnc.encoder
case Ref:
key := cacheKey{fingerprint: schema.(*RefSchema).Schema().Fingerprint(), rtype: typ.RType()}
if enc, f := e.encoders[key]; f {
return enc
}
return encoderOfType(e, schema.(*RefSchema).Schema(), typ)
case Enum:
return createEncoderOfEnum(schema.(*EnumSchema), typ)
case Array:
return createEncoderOfArray(e, schema.(*ArraySchema), typ)
case Map:
return createEncoderOfMap(e, schema.(*MapSchema), typ)
case Union:
return createEncoderOfUnion(e, schema.(*UnionSchema), typ)
case Fixed:
return createEncoderOfFixed(schema.(*FixedSchema), typ)
default:
// It is impossible to get here with a valid schema
return &errorEncoder{err: fmt.Errorf("avro: schema type %s is unsupported", schema.Type())}
}
}
type errorDecoder struct {
err error
}
func (d *errorDecoder) Decode(_ unsafe.Pointer, r *Reader) {
if r.Error == nil {
r.Error = d.err
}
}
type errorEncoder struct {
err error
}
func (e *errorEncoder) Encode(_ unsafe.Pointer, w *Writer) {
if w.Error == nil {
w.Error = e.err
}
}