-
Notifications
You must be signed in to change notification settings - Fork 3
/
publishableEvent.go
61 lines (52 loc) · 1.49 KB
/
publishableEvent.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
package bunnify
import (
"time"
"github.com/google/uuid"
)
// PublishableEvent represents an event that can be published.
// The Payload field holds the event's payload data, which can be of
// any type that can be marshal to json.
type PublishableEvent struct {
Metadata
Payload any `json:"payload"`
}
type eventOptions struct {
eventID string
correlationID string
}
// WithEventID specifies the eventID to be published
// if it is not used a random uuid will be generated.
func WithEventID(eventID string) func(*eventOptions) {
return func(opt *eventOptions) {
opt.eventID = eventID
}
}
// WithCorrelationID specifies the correlationID to be published
// if it is not used a random uuid will be generated.
func WithCorrelationID(correlationID string) func(*eventOptions) {
return func(opt *eventOptions) {
opt.correlationID = correlationID
}
}
// NewPublishableEvent creates an instance of a PublishableEvent.
// In case the ID and correlation ID are not supplied via options random uuid will be generated.
func NewPublishableEvent(payload any, opts ...func(*eventOptions)) PublishableEvent {
evtOpts := eventOptions{}
for _, opt := range opts {
opt(&evtOpts)
}
if evtOpts.correlationID == "" {
evtOpts.correlationID = uuid.NewString()
}
if evtOpts.eventID == "" {
evtOpts.eventID = uuid.NewString()
}
return PublishableEvent{
Metadata: Metadata{
ID: evtOpts.eventID,
CorrelationID: evtOpts.correlationID,
Timestamp: time.Now(),
},
Payload: payload,
}
}