-
Notifications
You must be signed in to change notification settings - Fork 39
/
serializer.go
104 lines (85 loc) · 2.63 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
package eventsource
import (
"encoding/json"
"reflect"
)
// Serializer converts between Events and Records
type Serializer interface {
// MarshalEvent converts an Event to a Record
MarshalEvent(event Event) (Record, error)
// UnmarshalEvent converts an Event backed into a Record
UnmarshalEvent(record Record) (Event, error)
}
type jsonEvent struct {
Type string `json:"t"`
Data json.RawMessage `json:"d"`
}
// JSONSerializer provides a simple serializer implementation
type JSONSerializer struct {
eventTypes map[string]reflect.Type
}
// Bind registers the specified events with the serializer; may be called more than once
func (j *JSONSerializer) Bind(events ...Event) {
for _, event := range events {
eventType, t := EventType(event)
j.eventTypes[eventType] = t
}
}
// MarshalEvent converts an event into its persistent type, Record
func (j *JSONSerializer) MarshalEvent(v Event) (Record, error) {
eventType, _ := EventType(v)
data, err := json.Marshal(v)
if err != nil {
return Record{}, err
}
data, err = json.Marshal(jsonEvent{
Type: eventType,
Data: json.RawMessage(data),
})
if err != nil {
return Record{}, NewError(err, ErrInvalidEncoding, "unable to encode event")
}
return Record{
Version: v.EventVersion(),
Data: data,
}, nil
}
// UnmarshalEvent converts the persistent type, Record, into an Event instance
func (j *JSONSerializer) UnmarshalEvent(record Record) (Event, error) {
wrapper := jsonEvent{}
err := json.Unmarshal(record.Data, &wrapper)
if err != nil {
return nil, NewError(err, ErrInvalidEncoding, "unable to unmarshal event")
}
t, ok := j.eventTypes[wrapper.Type]
if !ok {
return nil, NewError(err, ErrUnboundEventType, "unbound event type, %v", wrapper.Type)
}
v := reflect.New(t).Interface()
err = json.Unmarshal(wrapper.Data, v)
if err != nil {
return nil, NewError(err, ErrInvalidEncoding, "unable to unmarshal event data into %#v", v)
}
return v.(Event), nil
}
// MarshalAll is a utility that marshals all the events provided into a History object
func (j *JSONSerializer) MarshalAll(events ...Event) (History, error) {
history := make(History, 0, len(events))
for _, event := range events {
record, err := j.MarshalEvent(event)
if err != nil {
return nil, err
}
history = append(history, record)
}
return history, nil
}
// NewJSONSerializer constructs a new JSONSerializer and populates it with the specified events.
// Bind may be subsequently called to add more events.
func NewJSONSerializer(events ...Event) *JSONSerializer {
serializer := &JSONSerializer{
eventTypes: map[string]reflect.Type{},
}
serializer.Bind(events...)
return serializer
}