-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.go
More file actions
218 lines (190 loc) · 5.5 KB
/
Copy pathcodec.go
File metadata and controls
218 lines (190 loc) · 5.5 KB
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
package timebox
import (
"encoding/json"
"time"
bin "github.com/kode4food/timebox/internal/binary"
)
type (
// Codec encodes and decodes values
Codec[Value, Data any] interface {
// Encode encodes a value to the codec data format
Encode(Value) (Data, error)
// Decode decodes codec data to a value
Decode(Data) (Value, error)
// Append appends an encoded value to a buffer
Append(Data, Value) (Data, error)
// Read reads a value from a buffer and returns the remainder
Read(Data) (Value, Data, error)
}
// EventCodec encodes and decodes complete events
EventCodec[Data any] interface {
Codec[*Event, Data]
}
jsonEventCodec struct{}
binEventCodec struct{}
)
var (
// JSONEvent encodes complete events as JSON
JSONEvent EventCodec[[]byte] = jsonEventCodec{}
// BinEvent encodes complete events using the internal binary format
BinEvent interface {
EventCodec[[]byte]
AppendAll(buf []byte, evs []*Event) ([]byte, error)
ReadAll(data []byte) ([]*Event, []byte, error)
} = binEventCodec{}
// EncodeJSONEvents encodes complete events as JSON bytes
EncodeJSONEvents = MakeEncodeAll(JSONEvent)
// DecodeJSONEvents decodes complete events from JSON bytes
DecodeJSONEvents = MakeDecodeAll(JSONEvent)
// EncodeBinEvents encodes complete events to the internal binary format
EncodeBinEvents = MakeEncodeAll(BinEvent)
// DecodeBinEvents decodes complete events from the internal binary format
DecodeBinEvents = MakeDecodeAll(BinEvent)
)
// MakeEncodeAll returns a batch encoder for the provided codec
func MakeEncodeAll[Value, Data any](
codec Codec[Value, Data],
) func([]Value) ([]Data, error) {
return func(vals []Value) ([]Data, error) {
res := make([]Data, 0, len(vals))
for _, val := range vals {
b, err := codec.Encode(val)
if err != nil {
return nil, err
}
res = append(res, b)
}
return res, nil
}
}
// MakeDecodeAll returns a batch decoder for the provided codec
func MakeDecodeAll[Value, Data any](
codec Codec[Value, Data],
) func([]Data) ([]Value, error) {
return func(vals []Data) ([]Value, error) {
res := make([]Value, 0, len(vals))
for _, val := range vals {
decoded, err := codec.Decode(val)
if err != nil {
return nil, err
}
res = append(res, decoded)
}
return res, nil
}
}
// Encode converts an Event to JSON using its struct tags
func (c jsonEventCodec) Encode(ev *Event) ([]byte, error) {
return c.Append(nil, ev)
}
// Decode converts JSON into an Event using its struct tags
func (c jsonEventCodec) Decode(data []byte) (*Event, error) {
res, _, err := c.Read(data)
return res, err
}
// Append appends a JSON-encoded Event to a buffer
func (jsonEventCodec) Append(buf []byte, ev *Event) ([]byte, error) {
data, err := json.Marshal(ev)
if err != nil {
return nil, err
}
return append(buf, data...), nil
}
// Read reads and decodes a JSON Event from a buffer
func (jsonEventCodec) Read(data []byte) (*Event, []byte, error) {
var ev Event
if err := json.Unmarshal(data, &ev); err != nil {
return nil, nil, err
}
return &ev, nil, nil
}
// Encode converts an Event to the internal binary format
func (c binEventCodec) Encode(ev *Event) ([]byte, error) {
return c.Append(nil, ev)
}
// Decode converts the internal binary format into an Event
func (c binEventCodec) Decode(data []byte) (*Event, error) {
res, _, err := c.Read(data)
return res, err
}
// Append appends a binary-encoded Event to a buffer
func (binEventCodec) Append(buf []byte, ev *Event) ([]byte, error) {
buf = bin.AppendInt64(buf, ev.Timestamp.UnixNano())
buf = bin.AppendInt64(buf, ev.Sequence)
buf = appendAggregateID(buf, ev.AggregateID)
buf = bin.AppendString(buf, string(ev.Type))
buf = bin.AppendBytes(buf, ev.Data)
return buf, nil
}
// AppendAll appends a length-prefixed batch of binary-encoded Events
func (c binEventCodec) AppendAll(buf []byte, evs []*Event) ([]byte, error) {
buf = bin.AppendUint32(buf, uint32(len(evs)))
for _, ev := range evs {
var err error
buf, err = c.Append(buf, ev)
if err != nil {
return buf, err
}
}
return buf, nil
}
// Read reads and decodes a binary Event from a buffer
func (binEventCodec) Read(data []byte) (*Event, []byte, error) {
ts, data, err := bin.ReadInt64(data)
if err != nil {
return nil, nil, err
}
seq, data, err := bin.ReadInt64(data)
if err != nil {
return nil, nil, err
}
id, data, err := readAggregateID(data)
if err != nil {
return nil, nil, err
}
typ, data, err := bin.ReadString(data)
if err != nil {
return nil, nil, err
}
payload, data, err := bin.ReadBytes(data)
if err != nil {
return nil, nil, err
}
return &Event{
Timestamp: time.Unix(0, ts).UTC(),
Sequence: seq,
Type: EventType(typ),
AggregateID: id,
Data: payload,
}, data, nil
}
// ReadAll reads a length-prefixed batch of binary-encoded Events
func (c binEventCodec) ReadAll(data []byte) ([]*Event, []byte, error) {
n, data, err := bin.ReadUint32(data)
if err != nil {
return nil, nil, err
}
res := make([]*Event, n)
for i := range res {
res[i], data, err = c.Read(data)
if err != nil {
return nil, data, err
}
}
return res, data, nil
}
func appendAggregateID(buf []byte, id AggregateID) []byte {
buf = bin.AppendString(buf, string(id.Type))
return bin.AppendString(buf, string(id.Key))
}
func readAggregateID(data []byte) (AggregateID, []byte, error) {
typ, data, err := bin.ReadString(data)
if err != nil {
return AggregateID{}, nil, err
}
key, data, err := bin.ReadString(data)
if err != nil {
return AggregateID{}, nil, err
}
return NewAggregateID(ID(typ), ID(key)), data, nil
}