-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathavro.go
357 lines (311 loc) · 9.1 KB
/
avro.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
// Package avro wraps linkedin/goavro to provide serialization and
// deserialization of avro data to go struct with tags.
package avro
import (
"encoding/json"
"reflect"
"strings"
"github.com/fatih/camelcase"
"github.com/leboncoin/structs"
"github.com/linkedin/goavro/v2"
"github.com/mitchellh/mapstructure"
)
// TypeNameEncoder will transform a Go type name (ex: int, string, ..) into a avro type name
type TypeNameEncoder func(name string) string
// CamelCaseToSnakeCase will transform an input string written in CamelCase into snake_case
func CamelCaseToSnakeCase(name string) string {
result := camelcase.Split(name)
for i, s := range result {
result[i] = strings.ToLower(s)
}
return strings.Join(result, "_")
}
var typeConversion = map[string]string{
"uint8": "int",
"uint16": "int",
"uint32": "int",
"int8": "int",
"int16": "int",
"int32": "int",
"rune": "int",
"uint": "long",
"int": "long",
"uint64": "long",
"int64": "long",
"float32": "float",
"float64": "double",
"bool": "boolean",
}
var avroBaseTypes = []string{
"null",
"boolean",
"int",
"long",
"float",
"double",
"bytes",
"string",
}
// GoToAvroType transforms a Go type name to an Avro type name
// Note that complex types are not handled by this function as there is no
// counterpart
func GoToAvroType(name string) string {
out, ok := typeConversion[name]
if !ok {
return name
}
return out
}
// DefaultTypeNameEncoder combines avro type conversion and camel case to snake case
func DefaultTypeNameEncoder(name string) string {
return CamelCaseToSnakeCase(GoToAvroType(name))
}
// AddNamespace will contact a namespace with the given type name in avro standard way.
func AddNamespace(namespace, typeName string) string {
if len(namespace) == 0 {
return typeName
}
return namespace + "." + typeName
}
type avroSchemaNamespace struct {
Namespace string `json:"namespace,omitempty"`
}
// TypeNamer is an interface which will be used during marshalling
// to rename a type into its avro name
type TypeNamer interface {
AvroName() string
}
// CustomUnmarshaler will allow to define custom unmarshaller.
type CustomUnmarshaler interface {
UnmarshalAvro(b []byte) error
}
// Marshaler is for types marshaling go types to avro
//
// The Marshaler will understand structure field annotations like
// * "-" to completely omit the field
// * "omitempty" which will not use the field value if it is a zero value and replace it by its default value
// if there is one. WARNING: if no default value are provided, the Marshaler will return an error
//
// Marshaler will also handle pointer on values as the default optional value pattern in avro
// (union with null type and null as default value).
type Marshaler interface {
Marshal(interface{}) ([]byte, error)
}
// Unmarshaler is for types unmarshaling go types to avro
type Unmarshaler interface {
Unmarshal([]byte, interface{}) error
}
// Codec wraps the goavro.Codec type
type Codec struct {
goavro.Codec
// Namespace is the namespace which will be used to encode nested type
Namespace string
// TypeNameEncoder will be applied on all type during encoding to transform them from Go name to avro naming convention
TypeNameEncoder TypeNameEncoder
}
// NewCodec creates a codec from a schema
func NewCodec(schemaSpecification string) (*Codec, error) {
o, err := goavro.NewCodec(schemaSpecification)
if err != nil {
return nil, err
}
namespaceStruct := avroSchemaNamespace{}
err = json.Unmarshal([]byte(schemaSpecification), &namespaceStruct)
var namespace string
if err != nil {
namespace = ""
} else {
namespace = namespaceStruct.Namespace
}
return &Codec{*o, namespace, DefaultTypeNameEncoder}, nil
}
// Marshal marshals any go type to avro
func (c *Codec) Marshal(st interface{}) ([]byte, error) {
return c.marshal(&c.Codec, st)
}
// Unmarshal unmarshals any go type from avro
func (c *Codec) Unmarshal(avro []byte, output interface{}) error {
return c.unmarshal(&c.Codec, avro, output)
}
func (c *Codec) addNamespace(typeName string) string {
return AddNamespace(c.Namespace, typeName)
}
func (c *Codec) encodeTypeName(name string) string {
return c.TypeNameEncoder(name)
}
func (c *Codec) encodeUnionHook(kind reflect.Kind, data interface{}) (interface{}, error) {
value := reflect.ValueOf(data)
switch kind {
case reflect.Struct:
s := structs.New(data)
s.TagName = "avro"
s.EncodeHook = c.encodeUnionHook
data = s.Map()
case reflect.Slice:
elemType := getBaseType(reflect.TypeOf(data).Elem())
newData := reflect.MakeSlice(reflect.SliceOf(elemType), value.Len(), value.Cap())
for idx := 0; idx < value.Len(); idx++ {
elem := value.Index(idx)
val, err := c.encodeUnionHook(elem.Kind(), elem.Interface())
if err != nil {
return nil, err
}
newData.Index(idx).Set(reflect.ValueOf(val))
}
data = newData.Interface()
case reflect.Ptr:
if data != nil {
pointed := value.Elem()
if pointed.IsValid() {
val, err := c.encodeUnionHook(pointed.Kind(), pointed.Interface())
if err != nil {
return nil, err
}
typeName := c.getTypeName(pointed)
if !isAvroBaseType(typeName) {
typeName = c.addNamespace(typeName)
}
data = map[string]interface{}{
typeName: val,
}
} else {
data = map[string]interface{}{
"null": nil,
}
}
}
default:
data = convertToBaseType(value).Interface()
}
return data, nil
}
func isAvroBaseType(avroType string) bool {
for _, t := range avroBaseTypes {
if t == avroType {
return true
}
}
return false
}
func getBaseType(t reflect.Type) reflect.Type {
switch t.Kind() {
// Simple types
case reflect.String:
return reflect.TypeOf("")
case reflect.Uint:
return reflect.TypeOf(uint(0))
case reflect.Uint8:
return reflect.TypeOf(uint8(0))
case reflect.Uint16:
return reflect.TypeOf(uint16(0))
case reflect.Uint32:
return reflect.TypeOf(uint32(0))
case reflect.Uint64:
return reflect.TypeOf(uint64(0))
case reflect.Int:
return reflect.TypeOf(int(0))
case reflect.Int8:
return reflect.TypeOf(int8(0))
case reflect.Int16:
return reflect.TypeOf(int16(0))
case reflect.Int32:
return reflect.TypeOf(int32(0))
case reflect.Int64:
return reflect.TypeOf(int64(0))
case reflect.Float32:
return reflect.TypeOf(float32(0))
case reflect.Float64:
return reflect.TypeOf(float64(0))
// Composed types
case reflect.Struct, reflect.Ptr:
return reflect.TypeOf(map[string]interface{}{})
case reflect.Slice:
elemType := getBaseType(t.Elem())
return reflect.SliceOf(elemType)
default:
return t
}
}
// convertToBaseType takes a value which have a custom type (type MyType string) and returns the translation in basic type
func convertToBaseType(value reflect.Value) reflect.Value {
return value.Convert(getBaseType(value.Type()))
}
// toStructPtr will return a empty interface which is a pointer on the given interface{}
func toStructPtr(obj interface{}) interface{} {
val := reflect.ValueOf(obj)
vp := reflect.New(val.Type())
vp.Elem().Set(val)
return vp.Interface()
}
func (c *Codec) getTypeName(val reflect.Value) string {
data := val.Interface()
// Check if the value implement the interface, with a value as receiver
if avroNamer, ok := data.(TypeNamer); ok {
return avroNamer.AvroName()
}
ptrData := toStructPtr(data)
// Check if the pointer on value implement the interface, with a pointer as receiver
if avroNamer, ok := ptrData.(TypeNamer); ok {
return avroNamer.AvroName()
}
// otherwise return the type name
return c.encodeTypeName(val.Type().Name())
}
func (c Codec) decodeUnionHook(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
if to.Kind() == reflect.Ptr && data != nil {
// Handle optional value as union
unionData, ok := data.(map[string]interface{})
// In goavro union types are represented as map[string]interface{} of
// ONLY ONE VALUE. Hence larger maps do not correspond to union types.
// We thus keep them unaltered.
if ok && len(unionData) == 1 {
for _, val := range unionData {
return val, nil
}
}
}
// Lookup for a specific unmarshal method which implements 'CustomUnmarshaler'
ptrTo := reflect.New(to).Interface()
if unmarshaler, ok := ptrTo.(CustomUnmarshaler); ok {
paramVal := []byte(data.(string))
if err := unmarshaler.UnmarshalAvro(paramVal); err != nil {
return nil, err
}
return unmarshaler, nil
}
// Not union or unexpected type, return data unaltered.
return data, nil
}
func (c *Codec) marshal(codec *goavro.Codec, data interface{}) ([]byte, error) {
var (
value = reflect.ValueOf(data)
kind = value.Kind()
)
switch kind {
case reflect.Ptr:
value = value.Elem()
kind = value.Kind()
data = value.Interface()
}
nativeData, err := c.encodeUnionHook(kind, data)
if err != nil {
return nil, err
}
return codec.BinaryFromNative(nil, nativeData)
}
func (c *Codec) unmarshal(codec *goavro.Codec, avro []byte, output interface{}) (err error) {
m, _, err := codec.NativeFromBinary(avro)
if err != nil {
return err
}
config := mapstructure.DecoderConfig{
TagName: "avro",
DecodeHook: c.decodeUnionHook,
Result: output,
}
decoder, err := mapstructure.NewDecoder(&config)
if err != nil {
return err
}
return decoder.Decode(m)
}