-
Notifications
You must be signed in to change notification settings - Fork 11
/
avro.go
56 lines (45 loc) · 1.08 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
package coding
import (
"fmt"
"github.com/hamba/avro"
)
const (
EmptyAvroSchema = "EMPTY_AVRO_SCHEMA"
)
// NewAvro 使用avro模式创建一个新的coder
func NewAvro(rawSchema string) (c Coder, err error) {
if rawSchema == EmptyAvroSchema {
c = empty
return
}
schema, err := avro.Parse(rawSchema)
if err != nil {
return
}
c = &avroCoder{Schema: schema}
return
}
// avroCoder Avro模式的coder
type avroCoder struct {
Schema avro.Schema
}
func (c *avroCoder) Encode(data interface{}) ([]byte, error) {
return avro.Marshal(c.Schema, data)
}
func (c *avroCoder) Decode(bytes []byte) (data interface{}, err error) {
data = make(map[string]interface{})
err = avro.Unmarshal(c.Schema, bytes, &data)
return
}
func (c *avroCoder) DecodeIn(bytes []byte, i interface{}) (err error) {
err = avro.Unmarshal(c.Schema, bytes, &i)
return
}
// ToAvroObj 将参数转化为适用于avro的结构
func ToAvroObj(params []interface{}) interface{} {
out := make(map[string]interface{})
for i := 0; i < len(params); i++ {
out["F"+fmt.Sprintf("%d", i+1)] = params[i]
}
return out
}