-
Notifications
You must be signed in to change notification settings - Fork 7
/
event.go
188 lines (147 loc) · 4.28 KB
/
event.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
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package elemental
import (
"encoding/json"
"fmt"
"time"
)
// EventType is the type of an event.
type EventType string
const (
// EventCreate is the type of creation events.
EventCreate EventType = "create"
// EventUpdate is the type of update events.
EventUpdate EventType = "update"
// EventDelete is the type of delete events.
EventDelete EventType = "delete"
// EventError is the type of error events.
EventError EventType = "error"
)
// An Event represents a computational event.
type Event struct {
RawData []byte `msgpack:"entity" json:"-"`
JSONData json.RawMessage `msgpack:"-" json:"entity"`
Identity string `msgpack:"identity" json:"identity"`
Type EventType `msgpack:"type" json:"type"`
Timestamp time.Time `msgpack:"timestamp" json:"timestamp"`
Encoding EncodingType `msgpack:"encoding" json:"encoding"`
}
// NewEvent returns a new Event.
func NewEvent(t EventType, o Identifiable) *Event {
return NewEventWithEncoding(t, o, EncodingTypeMSGPACK)
}
// NewErrorEvent returns a new (error) Event embedded with the provided elemental.Error
func NewErrorEvent(ee Error, encoding EncodingType) *Event {
data, err := Encode(encoding, ee)
if err != nil {
panic(fmt.Sprintf("unable to create new error event: %s", err))
}
event := &Event{
Type: EventError,
Timestamp: time.Now(),
Encoding: encoding,
}
event.configureData(encoding, data)
return event
}
// NewEventWithEncoding returns a new Event using the given encoding
func NewEventWithEncoding(t EventType, o Identifiable, encoding EncodingType) *Event {
data, err := Encode(encoding, o)
if err != nil {
panic(fmt.Sprintf("unable to create new event: %s", err))
}
event := &Event{
Type: t,
Identity: o.Identity().Name,
Timestamp: time.Now(),
Encoding: encoding,
}
event.configureData(encoding, data)
return event
}
func (e *Event) configureData(encoding EncodingType, data []byte) {
switch encoding {
case EncodingTypeJSON:
e.JSONData = json.RawMessage(data)
case EncodingTypeMSGPACK:
e.RawData = data
}
}
// GetEncoding returns the encoding used to encode the entity.
func (e *Event) GetEncoding() EncodingType {
return e.Encoding
}
// Decode decodes the data into the given destination.
func (e *Event) Decode(dst any) error {
return Decode(e.GetEncoding(), e.Entity(), dst)
}
// Convert converts the internal encoded data to the given
// encoding.
func (e *Event) Convert(encoding EncodingType) error {
switch e.Encoding {
case encoding:
return nil
case EncodingTypeMSGPACK:
d, err := Convert(e.Encoding, encoding, e.RawData)
if err != nil {
return err
}
e.JSONData = json.RawMessage(d)
e.RawData = nil
default:
d, err := Convert(e.Encoding, encoding, []byte(e.JSONData))
if err != nil {
return err
}
e.JSONData = nil
e.RawData = d
}
e.Encoding = encoding
return nil
}
// Entity returns the byte encoded entity.
func (e *Event) Entity() []byte {
switch e.Encoding {
case EncodingTypeMSGPACK:
return e.RawData
default:
return []byte(e.JSONData)
}
}
func (e *Event) String() string {
return fmt.Sprintf("<event type: %s identity: %s>", e.Type, e.Identity)
}
// Duplicate creates a copy of the event.
func (e *Event) Duplicate() *Event {
var jd json.RawMessage
var rd []byte
if e.JSONData != nil {
jd = append(json.RawMessage{}, e.JSONData...)
}
if e.RawData != nil {
rd = append([]byte{}, e.RawData...)
}
return &Event{
Type: e.Type,
JSONData: jd,
RawData: rd,
Identity: e.Identity,
Timestamp: e.Timestamp,
Encoding: e.Encoding,
}
}
// An Events represents a list of Event.
type Events []*Event
// NewEvents retutns a new Events.
func NewEvents(events ...*Event) Events {
return append(Events{}, events...)
}