forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec_enum.go
More file actions
129 lines (108 loc) · 3.09 KB
/
codec_enum.go
File metadata and controls
129 lines (108 loc) · 3.09 KB
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
package avro
import (
"encoding"
"errors"
"fmt"
"reflect"
"unsafe"
"github.com/modern-go/reflect2"
)
func createDecoderOfEnum(schema Schema, typ reflect2.Type) ValDecoder {
switch {
case typ.Kind() == reflect.String:
return &enumCodec{symbols: schema.(*EnumSchema).Symbols()}
case typ.Implements(textUnmarshalerType):
return &enumTextMarshalerCodec{typ: typ, symbols: schema.(*EnumSchema).Symbols()}
case reflect2.PtrTo(typ).Implements(textUnmarshalerType):
return &enumTextMarshalerCodec{typ: typ, symbols: schema.(*EnumSchema).Symbols(), ptr: true}
}
return &errorDecoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s", typ.String(), schema.Type())}
}
func createEncoderOfEnum(schema Schema, typ reflect2.Type) ValEncoder {
switch {
case typ.Kind() == reflect.String:
return &enumCodec{symbols: schema.(*EnumSchema).Symbols()}
case typ.Implements(textMarshalerType):
return &enumTextMarshalerCodec{typ: typ, symbols: schema.(*EnumSchema).Symbols()}
case reflect2.PtrTo(typ).Implements(textMarshalerType):
return &enumTextMarshalerCodec{typ: typ, symbols: schema.(*EnumSchema).Symbols(), ptr: true}
}
return &errorEncoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s", typ.String(), schema.Type())}
}
type enumCodec struct {
symbols []string
}
func (c *enumCodec) Decode(ptr unsafe.Pointer, r *Reader) {
i := int(r.ReadInt())
if i < 0 || i >= len(c.symbols) {
r.ReportError("decode enum symbol", "unknown enum symbol")
return
}
*((*string)(ptr)) = c.symbols[i]
}
func (c *enumCodec) Encode(ptr unsafe.Pointer, w *Writer) {
str := *((*string)(ptr))
for i, sym := range c.symbols {
if str != sym {
continue
}
w.WriteInt(int32(i))
return
}
w.Error = fmt.Errorf("avro: unknown enum symbol: %s", str)
}
type enumTextMarshalerCodec struct {
typ reflect2.Type
symbols []string
ptr bool
}
func (c *enumTextMarshalerCodec) Decode(ptr unsafe.Pointer, r *Reader) {
i := int(r.ReadInt())
if i < 0 || i >= len(c.symbols) {
r.ReportError("decode enum symbol", "unknown enum symbol")
return
}
var obj any
if c.ptr {
obj = c.typ.PackEFace(ptr)
} else {
obj = c.typ.UnsafeIndirect(ptr)
}
if reflect2.IsNil(obj) {
ptrType := c.typ.(*reflect2.UnsafePtrType)
newPtr := ptrType.Elem().UnsafeNew()
*((*unsafe.Pointer)(ptr)) = newPtr
obj = c.typ.UnsafeIndirect(ptr)
}
unmarshaler := (obj).(encoding.TextUnmarshaler)
if err := unmarshaler.UnmarshalText([]byte(c.symbols[i])); err != nil {
r.ReportError("decode enum text unmarshaler", err.Error())
}
}
func (c *enumTextMarshalerCodec) Encode(ptr unsafe.Pointer, w *Writer) {
var obj any
if c.ptr {
obj = c.typ.PackEFace(ptr)
} else {
obj = c.typ.UnsafeIndirect(ptr)
}
if c.typ.IsNullable() && reflect2.IsNil(obj) {
w.Error = errors.New("encoding nil enum text marshaler")
return
}
marshaler := (obj).(encoding.TextMarshaler)
b, err := marshaler.MarshalText()
if err != nil {
w.Error = err
return
}
str := string(b)
for i, sym := range c.symbols {
if str != sym {
continue
}
w.WriteInt(int32(i))
return
}
w.Error = fmt.Errorf("avro: unknown enum symbol: %s", str)
}