forked from elodina/go-avro
-
Notifications
You must be signed in to change notification settings - Fork 4
/
datum_writer.go
568 lines (484 loc) · 14.3 KB
/
datum_writer.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package avro
import (
"errors"
"fmt"
"reflect"
)
// Writer is an interface that may be implemented to avoid using runtime reflection during serialization.
// Implementing it is optional and may be used as an optimization. Falls back to using reflection if not implemented.
type Writer interface {
Write(enc Encoder) error
}
// DatumWriter is an interface that is responsible for writing structured data according to schema to an encoder.
type DatumWriter interface {
// Write writes a single entry using this DatumWriter according to provided Schema.
// Accepts a value to write and Encoder to write to.
// May return an error indicating a write failure.
Write(interface{}, Encoder) error
// Sets the schema for this DatumWriter to know the data structure.
// Note that it must be called before calling Write.
SetSchema(Schema)
}
// SpecificDatumWriter implements DatumWriter and is used for writing Go structs in Avro format.
type SpecificDatumWriter struct {
schema Schema
}
// NewSpecificDatumWriter creates a new SpecificDatumWriter.
func NewSpecificDatumWriter() *SpecificDatumWriter {
return &SpecificDatumWriter{}
}
// SetSchema sets the provided schema for this SpecificDatumWriter to know the data structure.
// Note that it must be called before calling Write.
func (writer *SpecificDatumWriter) SetSchema(schema Schema) {
writer.schema = schema
}
// Write writes a single Go struct using this SpecificDatumWriter according to provided Schema.
// Accepts a value to write and Encoder to write to. Field names should match field names in Avro schema but be exported
// (e.g. "some_value" in Avro schema is expected to be Some_value in struct) or you may provide Go struct tags to
// explicitly show how to map fields (e.g. if you want to map "some_value" field of type int to SomeValue in Go struct
// you should define your struct field as follows: SomeValue int32 `avro:"some_field"`).
// May return an error indicating a write failure.
func (writer *SpecificDatumWriter) Write(obj interface{}, enc Encoder) error {
if writer, ok := obj.(Writer); ok {
return writer.Write(enc)
}
rv := reflect.ValueOf(obj)
if writer.schema == nil {
return SchemaNotSet
}
return writer.write(rv, enc, writer.schema)
}
func (writer *SpecificDatumWriter) write(v reflect.Value, enc Encoder, s Schema) error {
switch s.Type() {
case Null:
case Boolean:
return writer.writeBoolean(v, enc, s)
case Int:
return writer.writeInt(v, enc, s)
case Long:
return writer.writeLong(v, enc, s)
case Float:
return writer.writeFloat(v, enc, s)
case Double:
return writer.writeDouble(v, enc, s)
case Bytes:
return writer.writeBytes(v, enc, s)
case String:
return writer.writeString(v, enc, s)
case Array:
return writer.writeArray(v, enc, s)
case Map:
return writer.writeMap(v, enc, s)
case Enum:
return writer.writeEnum(v, enc, s)
case Union:
return writer.writeUnion(v, enc, s)
case Fixed:
return writer.writeFixed(v, enc, s)
case Record:
return writer.writeRecord(v, enc, s)
case Recursive:
return writer.writeRecord(v, enc, s.(*RecursiveSchema).Actual)
case Alias:
return writer.write(v, enc, s.(*AliasSchema).RefSchema)
}
return nil
}
func (writer *SpecificDatumWriter) writeBoolean(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid boolean value: %v", v.Interface())
}
enc.WriteBoolean(v.Interface().(bool))
return nil
}
func (writer *SpecificDatumWriter) writeInt(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid int value: %v", v.Interface())
}
enc.WriteInt(v.Interface().(int32))
return nil
}
func (writer *SpecificDatumWriter) writeLong(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid long value: %v", v.Interface())
}
enc.WriteLong(v.Interface().(int64))
return nil
}
func (writer *SpecificDatumWriter) writeFloat(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid float value: %v", v.Interface())
}
enc.WriteFloat(v.Interface().(float32))
return nil
}
func (writer *SpecificDatumWriter) writeDouble(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid double value: %v", v.Interface())
}
enc.WriteDouble(v.Interface().(float64))
return nil
}
func (writer *SpecificDatumWriter) writeBytes(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid bytes value: %v", v.Interface())
}
enc.WriteBytes(v.Interface().([]byte))
return nil
}
func (writer *SpecificDatumWriter) writeString(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid string value: %v", v.Interface())
}
enc.WriteString(v.Interface().(string))
return nil
}
func (writer *SpecificDatumWriter) writeArray(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid array value: %v", v.Interface())
}
if v.Len() == 0 {
enc.WriteArrayNext(0)
return nil
}
//TODO should probably write blocks of some length
enc.WriteArrayStart(int64(v.Len()))
for i := 0; i < v.Len(); i++ {
if err := writer.write(v.Index(i), enc, s.(*ArraySchema).Items); err != nil {
return err
}
}
enc.WriteArrayNext(0)
return nil
}
func (writer *SpecificDatumWriter) writeMap(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid map value: %v", v.Interface())
}
if v.Len() == 0 {
enc.WriteMapNext(0)
return nil
}
//TODO should probably write blocks of some length
enc.WriteMapStart(int64(v.Len()))
for _, key := range v.MapKeys() {
err := writer.writeString(key, enc, &StringSchema{})
if err != nil {
return err
}
if err = writer.write(v.MapIndex(key), enc, s.(*MapSchema).Values); err != nil {
return err
}
}
enc.WriteMapNext(0)
return nil
}
func (writer *SpecificDatumWriter) writeEnum(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid enum value: %v", v.Interface())
}
enc.WriteInt(v.Interface().(*GenericEnum).GetIndex())
return nil
}
func (writer *SpecificDatumWriter) writeUnion(v reflect.Value, enc Encoder, s Schema) error {
unionSchema := s.(*UnionSchema)
index := unionSchema.GetType(v)
if unionSchema.Types == nil || index < 0 || index >= len(unionSchema.Types) {
return fmt.Errorf("Invalid union value: %v, %s", v.Interface(), s.String())
}
enc.WriteLong(int64(index))
return writer.write(v, enc, unionSchema.Types[index])
}
func (writer *SpecificDatumWriter) writeFixed(v reflect.Value, enc Encoder, s Schema) error {
fs := s.(*FixedSchema)
if !fs.Validate(v) {
return fmt.Errorf("Invalid fixed value: %v", v.Interface())
}
// Write the raw bytes. The length is known by the schema
enc.WriteRaw(v.Interface().([]byte))
return nil
}
func (writer *SpecificDatumWriter) writeRecord(v reflect.Value, enc Encoder, s Schema) error {
if !s.Validate(v) {
return fmt.Errorf("Invalid record value: %v", v.Interface())
}
rs := assertRecordSchema(s)
for i := range rs.Fields {
schemaField := rs.Fields[i]
field, err := findField(v, schemaField.Name)
if err != nil {
return err
}
if err := writer.write(field, enc, schemaField.Type); err != nil {
return err
}
}
return nil
}
// GenericDatumWriter implements DatumWriter and is used for writing GenericRecords or other Avro supported types
// (full list is: interface{}, bool, int32, int64, float32, float64, string, slices of any type, maps with string keys
// and any values, GenericEnums) to a given Encoder.
type GenericDatumWriter struct {
schema Schema
}
// NewGenericDatumWriter creates a new GenericDatumWriter.
func NewGenericDatumWriter() *GenericDatumWriter {
return &GenericDatumWriter{}
}
// SetSchema sets the provided schema for this GenericDatumWriter to know the data structure.
// Note that it must be called before calling Write.
func (writer *GenericDatumWriter) SetSchema(schema Schema) {
writer.schema = schema
}
// Write writes a single entry using this GenericDatumWriter according to provided Schema.
// Accepts a value to write and Encoder to write to.
// May return an error indicating a write failure.
func (writer *GenericDatumWriter) Write(obj interface{}, enc Encoder) error {
return writer.write(obj, enc, writer.schema)
}
func (writer *GenericDatumWriter) write(v interface{}, enc Encoder, s Schema) error {
switch s.Type() {
case Null:
case Boolean:
return writer.writeBoolean(v, enc)
case Int:
return writer.writeInt(v, enc)
case Long:
return writer.writeLong(v, enc)
case Float:
return writer.writeFloat(v, enc)
case Double:
return writer.writeDouble(v, enc)
case Bytes:
return writer.writeBytes(v, enc)
case String:
return writer.writeString(v, enc)
case Array:
return writer.writeArray(v, enc, s)
case Map:
return writer.writeMap(v, enc, s)
case Enum:
return writer.writeEnum(v, enc, s)
case Union:
return writer.writeUnion(v, enc, s)
case Fixed:
return writer.writeFixed(v, enc, s)
case Record:
return writer.writeRecord(v, enc, s)
case Recursive:
return writer.writeRecord(v, enc, s.(*RecursiveSchema).Actual)
case Alias:
return writer.write(v, enc, s.(*AliasSchema).RefSchema)
}
return nil
}
func (writer *GenericDatumWriter) writeBoolean(v interface{}, enc Encoder) error {
switch value := v.(type) {
case bool:
enc.WriteBoolean(value)
default:
return fmt.Errorf("%v is not a boolean", v)
}
return nil
}
func (writer *GenericDatumWriter) writeInt(v interface{}, enc Encoder) error {
switch value := v.(type) {
case int32:
enc.WriteInt(value)
default:
return fmt.Errorf("%v is not an int32", v)
}
return nil
}
func (writer *GenericDatumWriter) writeLong(v interface{}, enc Encoder) error {
switch value := v.(type) {
case int64:
enc.WriteLong(value)
default:
return fmt.Errorf("%v is not an int64", v)
}
return nil
}
func (writer *GenericDatumWriter) writeFloat(v interface{}, enc Encoder) error {
switch value := v.(type) {
case float32:
enc.WriteFloat(value)
default:
return fmt.Errorf("%v is not a float32", v)
}
return nil
}
func (writer *GenericDatumWriter) writeDouble(v interface{}, enc Encoder) error {
switch value := v.(type) {
case float64:
enc.WriteDouble(value)
default:
return fmt.Errorf("%v is not a float64", v)
}
return nil
}
func (writer *GenericDatumWriter) writeBytes(v interface{}, enc Encoder) error {
switch value := v.(type) {
case []byte:
enc.WriteBytes(value)
default:
return fmt.Errorf("%v is not a []byte", v)
}
return nil
}
func (writer *GenericDatumWriter) writeString(v interface{}, enc Encoder) error {
switch value := v.(type) {
case string:
enc.WriteString(value)
default:
return fmt.Errorf("%v is not a string", v)
}
return nil
}
func (writer *GenericDatumWriter) writeArray(v interface{}, enc Encoder, s Schema) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
return errors.New("Not a slice or array type")
}
if rv.Len() == 0 {
enc.WriteArrayNext(0)
return nil
}
//TODO should probably write blocks of some length
enc.WriteArrayStart(int64(rv.Len()))
for i := 0; i < rv.Len(); i++ {
err := writer.write(rv.Index(i).Interface(), enc, s.(*ArraySchema).Items)
if err != nil {
return err
}
}
enc.WriteArrayNext(0)
return nil
}
func (writer *GenericDatumWriter) writeMap(v interface{}, enc Encoder, s Schema) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Map {
return errors.New("Not a map type")
}
if rv.Len() == 0 {
enc.WriteMapNext(0)
return nil
}
//TODO should probably write blocks of some length
enc.WriteMapStart(int64(rv.Len()))
for _, key := range rv.MapKeys() {
err := writer.writeString(key.Interface(), enc)
if err != nil {
return err
}
err = writer.write(rv.MapIndex(key).Interface(), enc, s.(*MapSchema).Values)
if err != nil {
return err
}
}
enc.WriteMapNext(0)
return nil
}
func (writer *GenericDatumWriter) writeEnum(v interface{}, enc Encoder, s Schema) error {
switch v.(type) {
case *GenericEnum:
{
rs := s.(*EnumSchema)
for i := range rs.Symbols {
if rs.Name == rs.Symbols[i] {
err := writer.writeInt(i, enc)
if err != nil {
return err
}
break
}
}
}
case string:
{
rs := s.(*EnumSchema)
for i := range rs.Symbols {
if v.(string) == rs.Symbols[i] {
enc.WriteInt(int32(i))
break
}
}
}
default:
return fmt.Errorf("%v is not a *GenericEnum", v)
}
return nil
}
func (writer *GenericDatumWriter) writeUnion(v interface{}, enc Encoder, s Schema) error {
unionSchema := s.(*UnionSchema)
index := unionSchema.GetType(reflect.ValueOf(v))
if index != -1 {
enc.WriteInt(int32(index))
return writer.write(v, enc, unionSchema.Types[index])
}
return fmt.Errorf("Could not write %v as %s", v, s)
}
func (writer *GenericDatumWriter) isWritableAs(v interface{}, s Schema) bool {
ok := false
switch s.(type) {
case *NullSchema:
return v == nil
case *BooleanSchema:
_, ok = v.(bool)
case *IntSchema:
_, ok = v.(int32)
case *LongSchema:
_, ok = v.(int64)
case *FloatSchema:
_, ok = v.(float32)
case *DoubleSchema:
_, ok = v.(float64)
case *StringSchema:
_, ok = v.(string)
case *BytesSchema:
_, ok = v.([]byte)
case *ArraySchema:
{
kind := reflect.ValueOf(v).Kind()
return kind == reflect.Array || kind == reflect.Slice
}
case *MapSchema:
return reflect.ValueOf(v).Kind() == reflect.Map
case *EnumSchema:
_, ok = v.(*GenericEnum)
case *UnionSchema:
panic("Nested unions not supported") //this is a part of spec: http://avro.apache.org/docs/current/spec.html#binary_encode_complex
case *RecordSchema:
_, ok = v.(*GenericRecord)
case *preparedRecordSchema:
_, ok = v.(*GenericRecord)
case *AliasSchema:
return writer.isWritableAs(v, s.(*AliasSchema).RefSchema)
}
return ok
}
func (writer *GenericDatumWriter) writeFixed(v interface{}, enc Encoder, s Schema) error {
return writer.writeBytes(v, enc)
}
func (writer *GenericDatumWriter) writeRecord(v interface{}, enc Encoder, s Schema) error {
switch value := v.(type) {
case *GenericRecord:
{
rs := assertRecordSchema(s)
for i := range rs.Fields {
schemaField := rs.Fields[i]
field := value.Get(schemaField.Name)
if field == nil {
field = schemaField.Default
}
err := writer.write(field, enc, schemaField.Type)
if err != nil {
return err
}
}
}
default:
return fmt.Errorf("%v is not a *GenericRecord", v)
}
return nil
}