forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec_union.go
366 lines (293 loc) · 7.65 KB
/
codec_union.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
package avro
import (
"errors"
"fmt"
"reflect"
"strings"
"unsafe"
"github.com/modern-go/reflect2"
)
func createDecoderOfUnion(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValDecoder {
switch typ.Kind() {
case reflect.Map:
if typ.(reflect2.MapType).Key().Kind() != reflect.String ||
typ.(reflect2.MapType).Elem().Kind() != reflect.Interface {
break
}
return decoderOfMapUnion(cfg, schema, typ)
case reflect.Ptr:
if !schema.(*UnionSchema).Nullable() {
break
}
return decoderOfPtrUnion(cfg, schema, typ)
case reflect.Interface:
if _, ok := typ.(*reflect2.UnsafeIFaceType); !ok {
return decoderOfResolvedUnion(cfg, schema)
}
}
return &errorDecoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s", typ.String(), schema.Type())}
}
func createEncoderOfUnion(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValEncoder {
switch typ.Kind() {
case reflect.Map:
if typ.(reflect2.MapType).Key().Kind() != reflect.String ||
typ.(reflect2.MapType).Elem().Kind() != reflect.Interface {
break
}
return encoderOfMapUnion(cfg, schema, typ)
case reflect.Ptr:
if !schema.(*UnionSchema).Nullable() {
break
}
return encoderOfPtrUnion(cfg, schema, typ)
}
return encoderOfResolverUnion(cfg, schema, typ)
}
func decoderOfMapUnion(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValDecoder {
union := schema.(*UnionSchema)
mapType := typ.(*reflect2.UnsafeMapType)
return &mapUnionDecoder{
cfg: cfg,
schema: union,
mapType: mapType,
elemType: mapType.Elem(),
}
}
type mapUnionDecoder struct {
cfg *frozenConfig
schema *UnionSchema
mapType *reflect2.UnsafeMapType
elemType reflect2.Type
}
func (d *mapUnionDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
_, resSchema := getUnionSchema(d.schema, r)
if resSchema == nil {
return
}
// In a null case, just return
if resSchema.Type() == Null {
return
}
if d.mapType.UnsafeIsNil(ptr) {
d.mapType.UnsafeSet(ptr, d.mapType.UnsafeMakeMap(0))
}
key := schemaTypeName(resSchema)
keyPtr := reflect2.PtrOf(key)
elemPtr := d.elemType.UnsafeNew()
decoderOfType(d.cfg, resSchema, d.elemType).Decode(elemPtr, r)
d.mapType.UnsafeSetIndex(ptr, keyPtr, elemPtr)
}
func encoderOfMapUnion(cfg *frozenConfig, schema Schema, _ reflect2.Type) ValEncoder {
union := schema.(*UnionSchema)
return &mapUnionEncoder{
cfg: cfg,
schema: union,
}
}
type mapUnionEncoder struct {
cfg *frozenConfig
schema *UnionSchema
}
func (e *mapUnionEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
m := *((*map[string]interface{})(ptr))
if len(m) > 1 {
w.Error = errors.New("avro: cannot encode union map with multiple entries")
return
}
name := "null"
val := interface{}(nil)
for k, v := range m {
name = k
val = v
break
}
schema, pos := e.schema.Types().Get(name)
if schema == nil {
w.Error = fmt.Errorf("avro: unknown union type %s", name)
return
}
w.WriteLong(int64(pos))
if schema.Type() == Null && val == nil {
return
}
elemType := reflect2.TypeOf(val)
elemPtr := reflect2.PtrOf(val)
encoderOfType(e.cfg, schema, elemType).Encode(elemPtr, w)
}
func decoderOfPtrUnion(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValDecoder {
union := schema.(*UnionSchema)
ptrType := typ.(*reflect2.UnsafePtrType)
elemType := ptrType.Elem()
decoder := decoderOfType(cfg, union.Types()[1], elemType)
return &unionPtrDecoder{
schema: union,
typ: elemType,
decoder: decoder,
}
}
type unionPtrDecoder struct {
schema *UnionSchema
typ reflect2.Type
decoder ValDecoder
}
func (d *unionPtrDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
_, schema := getUnionSchema(d.schema, r)
if schema == nil {
return
}
if schema.Type() == Null {
*((*unsafe.Pointer)(ptr)) = nil
return
}
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 encoderOfPtrUnion(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValEncoder {
union := schema.(*UnionSchema)
ptrType := typ.(*reflect2.UnsafePtrType)
encoder := encoderOfType(cfg, union.Types()[1], ptrType.Elem())
return &unionPtrEncoder{
schema: union,
encoder: encoder,
}
}
type unionPtrEncoder struct {
schema *UnionSchema
encoder ValEncoder
}
func (e *unionPtrEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
if *((*unsafe.Pointer)(ptr)) == nil {
w.WriteLong(0)
return
}
w.WriteLong(1)
e.encoder.Encode(*((*unsafe.Pointer)(ptr)), w)
}
func decoderOfResolvedUnion(cfg *frozenConfig, schema Schema) ValDecoder {
union := schema.(*UnionSchema)
types := make([]reflect2.Type, len(union.Types()))
decoders := make([]ValDecoder, len(union.Types()))
for i, schema := range union.Types() {
name := unionResolutionName(schema)
if typ, err := cfg.resolver.Type(name); err == nil {
decoder := decoderOfType(cfg, schema, typ)
decoders[i] = decoder
types[i] = typ
continue
}
decoders = []ValDecoder{}
types = []reflect2.Type{}
break
}
return &unionResolvedDecoder{
cfg: cfg,
schema: union,
types: types,
decoders: decoders,
}
}
type unionResolvedDecoder struct {
cfg *frozenConfig
schema *UnionSchema
types []reflect2.Type
decoders []ValDecoder
}
func (d *unionResolvedDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
i, schema := getUnionSchema(d.schema, r)
if schema == nil {
return
}
pObj := (*interface{})(ptr)
if schema.Type() == Null {
*pObj = nil
return
}
if i >= len(d.decoders) {
if d.cfg.config.UnionResolutionError {
r.ReportError("decode union type", "unknown union type")
return
}
// We cannot resolve this, set it to the map type
name := schemaTypeName(schema)
obj := map[string]interface{}{}
obj[name] = r.ReadNext(schema)
*pObj = obj
return
}
typ := d.types[i]
var newPtr unsafe.Pointer
switch typ.Kind() {
case reflect.Map:
mapType := typ.(*reflect2.UnsafeMapType)
newPtr = mapType.UnsafeMakeMap(1)
case reflect.Slice:
mapType := typ.(*reflect2.UnsafeSliceType)
newPtr = mapType.UnsafeMakeSlice(1, 1)
case reflect.Ptr:
elemType := typ.(*reflect2.UnsafePtrType).Elem()
newPtr = elemType.UnsafeNew()
default:
newPtr = typ.UnsafeNew()
}
d.decoders[i].Decode(newPtr, r)
*pObj = typ.UnsafeIndirect(newPtr)
}
func unionResolutionName(schema Schema) string {
name := schemaTypeName(schema)
switch schema.Type() {
case Map:
name += ":"
valSchema := schema.(*MapSchema).Values()
valName := schemaTypeName(valSchema)
name += valName
case Array:
name += ":"
itemSchema := schema.(*ArraySchema).Items()
itemName := schemaTypeName(itemSchema)
name += itemName
}
return name
}
func encoderOfResolverUnion(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValEncoder {
union := schema.(*UnionSchema)
name, err := cfg.resolver.Name(typ)
if err != nil {
return &errorEncoder{err: err}
}
if idx := strings.Index(name, ":"); idx > 0 {
name = name[:idx]
}
schema, pos := union.Types().Get(name)
if schema == nil {
return &errorEncoder{err: fmt.Errorf("avro: unknown union type %s", name)}
}
encoder := encoderOfType(cfg, schema, typ)
return &unionResolverEncoder{
pos: pos,
encoder: encoder,
}
}
type unionResolverEncoder struct {
pos int
encoder ValEncoder
}
func (e *unionResolverEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
w.WriteLong(int64(e.pos))
e.encoder.Encode(ptr, w)
}
func getUnionSchema(schema *UnionSchema, r *Reader) (int, Schema) {
types := schema.Types()
idx := int(r.ReadLong())
if idx < 0 || idx > len(types)-1 {
r.ReportError("decode union type", "unknown union type")
return 0, nil
}
return idx, types[idx]
}