-
Notifications
You must be signed in to change notification settings - Fork 2
/
topic.go
219 lines (174 loc) · 4.81 KB
/
topic.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
package eventually
import (
"encoding/json"
"fmt"
"github.com/ahmedkamals/eventually/internal/errors"
"github.com/google/uuid"
"sync"
"time"
)
type (
// UUID of the topic.
UUID string
// Category of the topic.
Category string
// Version of the topic.
Version int
// Metadata about the topic.
Metadata map[string]interface{}
// Payload of the topic.
Payload interface{}
identifiable interface {
// AggregateID returns the id of topic's related Aggregate.
AggregateID() UUID
}
versional interface {
// Version returns the version of the topic.
Version() Version
}
timeAware interface {
// CreatedAt returns the time when the topic was created.
CreatedAt() time.Time
}
decorative interface {
// GetHeaders returns the MetaData for the topic.
GetHeaders() Metadata
// SetHeader sets the value of the metadata specified by the key.
SetHeader(string, interface{})
// GetHeader returns the value of the metadata specified by the key.
GetHeader(string) (interface{}, bool)
}
carrier interface {
// Payload return the actual payload of the topic message.
Payload() Payload
}
marshaller interface {
// Marshal returns the JSON encoding of a topic.
Marshal() (string, error)
}
unmarshaller interface {
// Unmarshal parses the JSON-encoded topic.
Unmarshal(data string) error
}
serializable interface {
// Serialize converts the current topic to a serialized format.
Serialize() (string, error)
}
unserializable interface {
// Deserialize converts serialized format to a topic.
Deserialize(data string) error
}
// Descriptor is the interface that a topic must implement.
Descriptor interface {
identifiable
versional
timeAware
decorative
carrier
marshaller
unmarshaller
serializable
unserializable
fmt.GoStringer
fmt.Stringer
// Name returns the topic category.
Name() Category
}
// descriptor is an implementation of the topic Descriptor interface.
descriptor struct {
mtx sync.RWMutex
ID UUID `json:"id,omitempty"`
TopicName Category `json:"name,omitempty"`
EventVersion Version `json:"version,omitempty"`
EventCreatedAt time.Time `json:"created_at,omitempty"`
MetaData Metadata `json:"metadata"`
EventPayload Payload `json:"payload,omitempty"`
serializer Serializer
}
)
const (
// universalTopicUUID used for subscribing to all the topicsMap.
universalTopicUUID UUID = "*"
)
// NewUUID creates new UUID.
func NewUUID() UUID {
return UUID(uuid.New().String())
}
func newDescriptor(aggregateID UUID, category Category, payload Payload, version Version) Descriptor {
return &descriptor{
ID: aggregateID,
TopicName: category,
EventPayload: payload,
EventVersion: version,
EventCreatedAt: time.Now(),
MetaData: make(Metadata),
serializer: NewSerializer(),
}
}
// NewDescriptor returns a new topic descriptor.
func NewDescriptor(category Category, payload Payload, version Version) Descriptor {
return newDescriptor(NewUUID(), category, payload, version)
}
// newUniversalDescriptor returns a new universal topic descriptor.
func newUniversalDescriptor(category Category, payload Payload, version Version) Descriptor {
return newDescriptor(universalTopicUUID, category, payload, version)
}
func (d *descriptor) AggregateID() UUID {
return d.ID
}
func (d *descriptor) Name() Category {
return d.TopicName
}
func (d *descriptor) Version() Version {
return d.EventVersion
}
func (d *descriptor) GoString() string {
return fmt.Sprintf("%s[%s]", d.String(), d.ID)
}
func (d *descriptor) String() string {
return fmt.Sprintf("%s@v%d", d.Name(), d.Version())
}
func (d *descriptor) CreatedAt() time.Time {
return d.EventCreatedAt
}
func (d *descriptor) GetHeaders() Metadata {
d.mtx.Lock()
defer d.mtx.Unlock()
return d.MetaData
}
func (d *descriptor) SetHeader(key string, value interface{}) {
d.mtx.Lock()
defer d.mtx.Unlock()
d.MetaData[key] = value
}
func (d *descriptor) GetHeader(key string) (interface{}, bool) {
d.mtx.Lock()
defer d.mtx.Unlock()
item, ok := d.MetaData[key]
return item, ok
}
func (d *descriptor) Payload() Payload {
return d.EventPayload
}
func (d *descriptor) Marshal() (string, error) {
const op errors.Operation = "Topic.Marshal"
encodedData, err := json.Marshal(d)
if err != nil {
return "", errors.E(op, errors.Failure, err)
}
return string(encodedData), nil
}
func (d *descriptor) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), d)
}
func (d *descriptor) Serialize() (string, error) {
const op errors.Operation = "Topic.Serialize"
serializedData, err := d.serializer.serialize(d)
if err != nil {
return "", errors.E(op, errors.Failure, err)
}
return string(serializedData), nil
}
func (d *descriptor) Deserialize(data string) error {
return d.serializer.deserialize([]byte(data), d)
}