/
event.go
152 lines (119 loc) · 3.51 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
// 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 event.
EventCreate EventType = "create"
// EventUpdate is the type of update event.
EventUpdate EventType = "update"
// EventDelete is the type of delete event.
EventDelete EventType = "delete"
)
// 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)
}
// 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))
}
evt := &Event{
Type: t,
Identity: o.Identity().Name,
Timestamp: time.Now(),
Encoding: encoding,
}
if encoding == EncodingTypeJSON {
evt.JSONData = json.RawMessage(data)
} else {
evt.RawData = data
}
return evt
}
// 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 interface{}) 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 {
return &Event{
Type: e.Type,
JSONData: e.JSONData[:],
RawData: e.RawData[:],
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...)
}