forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodec_enum.go
More file actions
173 lines (147 loc) · 4 KB
/
Copy pathcodec_enum.go
File metadata and controls
173 lines (147 loc) · 4 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
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
package avro
import (
"encoding"
"errors"
"fmt"
"reflect"
"unsafe"
"github.com/modern-go/reflect2"
)
func createDecoderOfEnum(schema *EnumSchema, typ reflect2.Type) ValDecoder {
switch {
case typ.Kind() == reflect.String:
return &enumCodec{enum: schema}
case typ.Implements(textUnmarshalerType):
return &enumTextMarshalerCodec{typ: typ, enum: schema}
case reflect2.PtrTo(typ).Implements(textUnmarshalerType):
return &enumTextMarshalerCodec{typ: typ, enum: schema, ptr: true}
}
return &errorDecoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s", typ.String(), schema.Type())}
}
func createEncoderOfEnum(schema *EnumSchema, typ reflect2.Type) ValEncoder {
switch {
case typ.Kind() == reflect.String:
return &enumCodec{enum: schema}
case typ.Implements(textAppenderType):
return &enumTextAppenderCodec{typ: typ, enum: schema}
case reflect2.PtrTo(typ).Implements(textAppenderType):
return &enumTextAppenderCodec{typ: typ, enum: schema, ptr: true}
case typ.Implements(textMarshalerType):
return &enumTextMarshalerCodec{typ: typ, enum: schema}
case reflect2.PtrTo(typ).Implements(textMarshalerType):
return &enumTextMarshalerCodec{typ: typ, enum: schema, ptr: true}
}
return &errorEncoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s", typ.String(), schema.Type())}
}
type enumCodec struct {
enum *EnumSchema
}
func (c *enumCodec) Decode(ptr unsafe.Pointer, r *Reader) {
i := int(r.ReadInt())
symbol, ok := c.enum.Symbol(i)
if !ok {
r.ReportError("decode enum symbol", "unknown enum symbol")
return
}
*((*string)(ptr)) = symbol
}
func (c *enumCodec) Encode(ptr unsafe.Pointer, w *Writer) {
str := *((*string)(ptr))
for i, sym := range c.enum.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
enum *EnumSchema
ptr bool
}
func (c *enumTextMarshalerCodec) Decode(ptr unsafe.Pointer, r *Reader) {
i := int(r.ReadInt())
symbol, ok := c.enum.Symbol(i)
if !ok {
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(symbol)); 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
}
for i, sym := range c.enum.symbols {
if string(b) != sym {
continue
}
w.WriteInt(int32(i))
return
}
w.Error = fmt.Errorf("avro: unknown enum symbol: %s", string(b))
}
type enumTextAppenderCodec struct {
typ reflect2.Type
enum *EnumSchema
ptr bool
}
func (c *enumTextAppenderCodec) 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 appender")
return
}
// AppendText must derive its result from the passed-in buffer, not alias internal state, or w.scratch reuse will corrupt later calls.
appender := (obj).(encoding.TextAppender)
b, err := appender.AppendText(w.scratch[:0])
if err != nil {
w.Error = err
return
}
for i, sym := range c.enum.symbols {
if string(b) != sym {
continue
}
w.scratch = b
w.WriteInt(int32(i))
return
}
w.scratch = b
w.Error = fmt.Errorf("avro: unknown enum symbol: %s", string(b))
}