/
encoder_decoder_avro.go
439 lines (377 loc) · 9.21 KB
/
encoder_decoder_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
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
package kafkaclient
import (
"context"
"reflect"
"time"
"github.com/linkedin/goavro/v2"
cache "github.com/patrickmn/go-cache"
logger "github.com/san-services/apilogger"
)
var (
// TypeMap maps go types to avro types
// k: go type, v: avro type
typeMap = map[string]string{
"string": "string",
"int": "int",
"int32": "int",
"int64": "long",
"float32": "float",
"float64": "double",
"[]uint8": "bytes"}
)
// AvroEncoderDecoder enhances the capabilities of the linkedin/goavro
// package - it helps transform avro-formatted kafka messages between
// struct and binary form in relation to an avro schema
type avroEncoderDecoder struct {
schemaReg schemaRegistry
cache *cache.Cache
cacheTime time.Duration
}
// NewAvroEncDec constructs and returns a new avro message EncoderDecoder
func newAvroEncDec(s schemaRegistry,
c *cache.Cache, cacheTime time.Duration) EncoderDecoder {
return &avroEncoderDecoder{
schemaReg: s,
cache: c,
cacheTime: cacheTime}
}
// Encode converts a native go struct to binary avro data
//
// Fields of the struct to be encoded should have avro
// tags matching schema field names, e.g.:
//
// type Thing struct {
// ID int64 `avro:"ID"`
// Name string `avro:"NAME"`
// }
//
// matches
//
// schema := `
// {
// "type": "record",
// "fields": [
// {
// "name": "ID",
// "type": [
// "null",
// "long"
// ],
// "default": null
// },
// {
// "name": "NAME",
// "type": [
// "null",
// "string"
// ],
// "default": null
// },
// ]
// }
// `
//
// Also note the typemap above when creating structs to
// either encode as avro or unmarshall avro message data into.
//
func (ed avroEncoderDecoder) Encode(
ctx context.Context, topic string, s interface{}) (b []byte, e error) {
lg := logger.New(ctx, "")
rv := reflect.ValueOf(s)
if rv.Kind() != reflect.Struct {
e = errStructRequired
lg.Error(logger.LogCatInputValidation, e)
return
}
fields := getFieldMap(ctx, rv)
if fields == nil || len(fields) == 0 {
e = errNoFields
lg.Error(logger.LogCatInputValidation, e)
return
}
dataMap := make(map[string]interface{})
for k, v := range fields {
if v.GoType == "[]uint8" {
v.Value = string(v.Value.([]byte))
}
dataMap[k] = map[string]interface{}{v.AvroType: v.Value}
}
codec, e := ed.getTopicCodec(ctx, topic)
if e != nil {
lg.Error(logger.LogCatKafkaEncode, e)
return
}
b, e = codec.BinaryFromNative(nil, dataMap)
if e != nil {
lg.Error(logger.LogCatKafkaEncode, e)
return
}
return
}
// Decode converts a binary message to a native go struct
//
// Target struct fields should have avro tags matching schema field names.
// Complex target fields, which are structs relating to another topic schema,
// should include a "topic" tag to help determine the schema required to decode them.
//
// e.g.:
//
// type Thing struct {
// ID int64 `avro:"ID"`
// Name string `avro:"NAME"`
// }
//
// matches the following schema:
//
// {
// "type": "record",
// "fields": [
// {
// "name": "ID",
// "type": [
// "null",
// "long"
// ],
// "default": null
// },
// {
// "name": "NAME",
// "type": [
// "null",
// "string"
// ],
// "default": null
// },
// ]
// }
//
// and
//
// type ThingRetry struct {
// ErrorMessage string `avro:"ERROR_MESSAGE"`
// OriginalMessage []byte `avro:"ORIGINAL_MESSAGE"`
// }
//
// and
//
// type ThingRetry struct {
// ErrorMessage string `avro:"ERROR_MESSAGE"`
// OriginalMessage TopicMessage `avro:"ORIGINAL_MESSAGE" topic:"new_things"`
// }
//
// match the following schema:
//
// {
// "type": "record",
// "fields": [
// {
// "name": "ERROR_MESSAGE",
// "type": [
// "null",
// "string"
// ],
// "default": null
// },
// {
// "name": "ORIGINAL_MESSAGE",
// "type": [
// "null",
// "bytes"
// ],
// "default": null
// },
// ]
// }
//
func (ed avroEncoderDecoder) Decode(
topic string, b []byte, target interface{}) (e error) {
lg := logger.New(nil, "")
var rv reflect.Value
if _, ok := target.(reflect.Value); ok {
rv = target.(reflect.Value)
} else {
rv = reflect.ValueOf(target)
}
if rv.Kind() != reflect.Ptr || rv.IsNil() {
e = errPtrRequired
lg.Error(logger.LogCatInputValidation, e)
return
}
rv = rv.Elem()
// convert binary message into a map of data
data, e := ed.binaryToMap(topic, b)
if e != nil {
lg.Error(logger.LogCatKafkaDecode, e)
return
}
// get metadata for all fields in target struct
fields := getFieldMap(nil, rv)
// range through data map and insert data into
// target struct, field-by-field
for k, v := range data {
fieldInfo := fields[k]
if (fieldInfo == field{}) {
e = errFieldInfo(k)
lg.Error(logger.LogCatInputValidation, e)
return
}
// get appropriate target field
f := rv.FieldByName(fieldInfo.Name)
// get field type
tarFieldType := f.Type()
e = ed.applyDataToField(fieldInfo, f, tarFieldType, v)
if e != nil {
lg.Error(logger.LogCatKafkaDecode, e)
return
}
}
return
}
func (ed avroEncoderDecoder) applyDataToField(fldInfo field,
fld reflect.Value, fldType reflect.Type, dataVal interface{}) (e error) {
lg := logger.New(nil, "")
if dataVal == nil || fldType == nil {
return
}
// find val to set
valToSet := dataVal
if v, ok := dataVal.(map[string]interface{}); ok {
// handle primitive type/value
// get key for inner map
mapKey := fldInfo.AvroType
// get field value using inner map key
valToSet = v[mapKey]
}
if valToSet == nil {
return
}
// handle complex type/value (recursive decode)
if fldInfo.TopicTag != "" {
b, ok := valToSet.([]byte)
if ok {
nestedMsg := reflect.New(fldType)
e = ed.Decode(fldInfo.TopicTag, b, nestedMsg)
if e != nil {
lg.Error(logger.LogCatKafkaDecode, e)
return
}
if fld.IsValid() && fld.CanSet() {
fld.Set(nestedMsg.Elem())
}
return
}
}
// get intended value type
valType := reflect.TypeOf(valToSet)
// check field type and intended value type match
if fldType != valType {
e = errUnmarshallFieldType(fldInfo.Name, fldType, valType)
lg.Error(logger.LogCatKafkaDecode, e)
return
}
// set field value
if fld.IsValid() && fld.CanSet() {
fld.Set(reflect.ValueOf(valToSet))
}
return
}
func (ed avroEncoderDecoder) GetSchemaID(
ctx context.Context, topic string) (id int, e error) {
lg := logger.New(ctx, "")
_, id, e = ed.schemaReg.GetSchemaByTopic(ctx, topic)
if e != nil {
lg.Error(logger.LogCatKafkaSchemaReg, e)
}
return
}
func (ed avroEncoderDecoder) binaryToMap(
topic string, b []byte) (data map[string]interface{}, e error) {
lg := logger.New(nil, "")
codec, e := ed.getTopicCodec(nil, topic)
if e != nil {
lg.Error(logger.LogCatKafkaDecode, e)
return
}
// the first 5 bytes contain encoded metadata, so
// we skip them in order for the decoding to work
i, _, e := codec.NativeFromBinary(b[5:])
if e != nil {
lg.Error(logger.LogCatKafkaDecode, e)
// if the schema was first registered by the service
// messages might not contain encoded metadata
i, _, e = codec.NativeFromBinary(b)
if e != nil {
lg.Error(logger.LogCatKafkaDecode, e)
return
}
}
data, ok := i.(map[string]interface{})
if !ok {
e = errMessageFmt
lg.Error(logger.LogCatKafkaDecode, e)
return
}
return
}
func (ed avroEncoderDecoder) getTopicCodec(
ctx context.Context, topic string) (c *goavro.Codec, e error) {
lg := logger.New(ctx, "")
if ed.cache != nil {
if codec, found := ed.cache.Get(topic); found {
var ok bool
c, ok = codec.(*goavro.Codec)
if ok {
return
}
// if there was an issue above, log error,
// go on ahead and fetch schema from schema reg, not cache
lg.Error(logger.LogCatCacheRead, errCacheItemType)
}
}
schema, _, e := ed.schemaReg.GetSchemaByTopic(ctx, topic)
if e != nil {
lg.Error(logger.LogCatKafkaSchemaReg, e)
return
}
c, e = goavro.NewCodec(schema)
if e != nil {
lg.Error(logger.LogCatKafkaDecode, e)
return
}
if ed.cache != nil {
ed.cache.Set(topic, c, ed.cacheTime)
}
return
}
type field struct {
Name string
Value interface{}
GoType string
AvroType string
AvroTag string
TopicTag string
}
func newField(name string, val interface{},
goType string, avroType string, avroTag string,
topicTag string) field {
return field{
Name: name, Value: val,
GoType: goType, AvroType: avroType,
AvroTag: avroTag, TopicTag: topicTag}
}
func getFieldMap(ctx context.Context,
rv reflect.Value) (m map[string]field) {
m = make(map[string]field)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
for i := 0; i < rv.NumField(); i++ {
fName := rv.Type().Field(i).Name
fType := rv.Field(i).Type().String()
aTag := rv.Type().Field(i).Tag.Get("avro")
tTag := rv.Type().Field(i).Tag.Get("topic")
fVal := rv.Field(i).Interface()
m[aTag] = newField(fName, fVal, fType, typeMap[fType], aTag, tTag)
}
return
}