-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.go
220 lines (182 loc) · 4.45 KB
/
serializer.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
package avro
import (
"context"
"errors"
"fmt"
"github.com/hamba/avro/v2"
"github.com/ln80/event-store/avro/registry"
"github.com/ln80/event-store/event"
"github.com/ln80/event-store/internal/logger"
)
var (
ErrReadOnlyModeEnabled = errors.New("read only mode enabled")
)
type EventSerializer struct {
registry *registry.Registry
cfg *EventSerializerConfig
}
type EventSerializerConfig struct {
ReadOnly bool
Namespace string
// SkipCurrentSchema disables the generation of the current schema from registered event.
SkipCurrentSchema bool
PersistCurrentSchema bool
}
func NewEventSerializer(ctx context.Context, r *registry.Registry, opts ...func(*EventSerializerConfig)) *EventSerializer {
cfg := &EventSerializerConfig{
ReadOnly: false,
}
for _, opt := range opts {
if opt == nil {
continue
}
opt(cfg)
}
var (
sch avro.Schema
err error
)
log := logger.FromContext(ctx).WithName("avro").WithValues("namespace", cfg.Namespace)
if !cfg.SkipCurrentSchema {
sch, err = eventSchema(r.API(), cfg.Namespace)
if err != nil {
log.Error(err, "Failed Avro schema generation")
panic(err)
}
log.V(1).Info("Generated Avro schema", "schema", sch.String())
}
if err := r.Setup(ctx, sch, func(rc *registry.RegistryConfig) {
rc.PersistCurrent = cfg.PersistCurrentSchema
}); err != nil {
log.Error(err, "Failed to setup Avro registry")
panic(err)
}
return &EventSerializer{
registry: r,
cfg: cfg,
}
}
var _ event.Serializer = &EventSerializer{}
// MarshalEvent implements event.Serializer.
func (s *EventSerializer) MarshalEvent(ctx context.Context, evt event.Envelope) (b []byte, err error) {
if s.cfg.ReadOnly {
err = ErrReadOnlyModeEnabled
return
}
if evt == nil {
err = event.ErrMarshalEmptyEvent
return
}
defer func() {
if err != nil {
err = fmt.Errorf("%w: %v", event.ErrMarshalEventFailed, err)
}
}()
var (
avroEvt *avroEvent
)
avroEvt, err = convertEvent(evt)
if err != nil {
return
}
id, schema, _, err := s.registry.GetCurrent(ctx, avroEvt)
if err != nil {
return
}
b, err = s.registry.API().Marshal(schema, avroEvt)
if err != nil {
return
}
b, err = s.registry.AppendSchemaID(b, id)
if err != nil {
return
}
return
}
// MarshalEventBatch implements event.Serializer.
// Note event size per item n []int is not supported at the moment
func (s *EventSerializer) MarshalEventBatch(ctx context.Context, events []event.Envelope) (b []byte, err error) {
if s.cfg.ReadOnly {
err = ErrReadOnlyModeEnabled
return
}
l := len(events)
if l == 0 {
err = event.ErrMarshalEmptyEvent
return
}
// normalize failure, and do not propagate infra error
defer func() {
if err != nil {
err = fmt.Errorf("%w: %v", event.ErrMarshalEventFailed, err)
}
}()
avroEvents := make([]avroEvent, l)
for i, evt := range events {
var (
avroEvt *avroEvent
ok bool
)
if avroEvt, ok = evt.(*avroEvent); !ok {
avroEvt, err = convertEvent(evt)
if err != nil {
return
}
}
avroEvents[i] = *avroEvt
}
id, _, batchSchema, err := s.registry.GetCurrent(ctx, &avroEvents[0])
if err != nil {
return
}
b, err = s.registry.API().Marshal(batchSchema, avroEvents)
if err != nil {
return
}
b, err = s.registry.AppendSchemaID(b, id)
if err != nil {
return
}
return
}
// UnmarshalEvent implements event.Serializer.
func (s *EventSerializer) UnmarshalEvent(ctx context.Context, b []byte) (event.Envelope, error) {
avroEvt := avroEvent{}
id, b, err := s.registry.ExtractSchemaID(b)
if err != nil {
return nil, err
}
_, schema, _, err := s.registry.GetSchema(ctx, id)
if err != nil {
return nil, err
}
if err := s.registry.API().Unmarshal(schema, b, &avroEvt); err != nil {
return nil, err
}
avroEvt.SetAVROSchemaID(id)
avroEvt.checkType(s.cfg.Namespace)
return &avroEvt, nil
}
// UnmarshalEventBatch implements event.Serializer.
func (s *EventSerializer) UnmarshalEventBatch(ctx context.Context, b []byte) ([]event.Envelope, error) {
avroEvents := []avroEvent{}
id, b, err := s.registry.ExtractSchemaID(b)
if err != nil {
return nil, err
}
_, _, batchSchema, err := s.registry.GetSchema(ctx, id)
if err != nil {
return nil, err
}
if err := s.registry.API().Unmarshal(batchSchema, b, &avroEvents); err != nil {
return nil, err
}
envs := make([]event.Envelope, len(avroEvents))
for i, avroEvt := range avroEvents {
avroEvt := avroEvt
avroEvt.SetAVROSchemaID(id)
avroEvt.checkType(s.cfg.Namespace)
envs[i] = &avroEvt
}
return envs, nil
}