-
Notifications
You must be signed in to change notification settings - Fork 15
/
singleencoder.go
83 lines (75 loc) · 2.39 KB
/
singleencoder.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
package avro
import (
"context"
"reflect"
"sync"
)
// EncodingRegistry is used by SingleEncoder to find
// ids for schemas encoded in messages.
type EncodingRegistry interface {
// AppendSchemaID appends the given schema ID header to buf
// and returns the resulting slice.
AppendSchemaID(buf []byte, id int64) []byte
// IDForSchema returns an ID for the given schema.
IDForSchema(ctx context.Context, schema *Type) (int64, error)
}
// SingleEncoder encodes messages in Avro binary format.
// Each message includes a header or wrapper that indicates the schema.
type SingleEncoder struct {
registry EncodingRegistry
names *Names
// ids holds a map from Go type (reflect.Type) to schema ID (int64)
ids sync.Map
}
// NewSingleEncoder returns a SingleEncoder instance that encodes single
// messages along with their schema identifier.
//
// Go values unmarshaled through Marshal will have their Avro schemas
// translated with the given Names instance. If names is nil, the global
// namespace will be used.
func NewSingleEncoder(r EncodingRegistry, names *Names) *SingleEncoder {
if names == nil {
names = globalNames
}
return &SingleEncoder{
registry: r,
names: names,
}
}
// CheckMarshalType checks that the given type can be marshaled with the encoder.
// It also caches any type information obtained from the EncodingRegistry from the
// type, so future calls to Marshal with that type won't call it.
func (enc *SingleEncoder) CheckMarshalType(ctx context.Context, x interface{}) error {
_, err := enc.idForType(ctx, reflect.TypeOf(x))
return err
}
// Marshal returns x marshaled as using the Avro binary encoding,
// along with an identifier that records the type that it was encoded
// with.
func (enc *SingleEncoder) Marshal(ctx context.Context, x interface{}) ([]byte, error) {
xv := reflect.ValueOf(x)
id, err := enc.idForType(ctx, xv.Type())
if err != nil {
return nil, err
}
buf := make([]byte, 0, 100)
buf = enc.registry.AppendSchemaID(buf, id)
data, _, err := marshalAppend(enc.names, buf, xv)
return data, err
}
func (enc *SingleEncoder) idForType(ctx context.Context, t reflect.Type) (int64, error) {
id, ok := enc.ids.Load(t)
if ok {
return id.(int64), nil
}
avroType, err := avroTypeOf(enc.names, t)
if err != nil {
return 0, err
}
id1, err := enc.registry.IDForSchema(ctx, avroType)
if err != nil {
return 0, err
}
enc.ids.LoadOrStore(t, id1)
return id1, nil
}