-
Notifications
You must be signed in to change notification settings - Fork 16
/
event.go
79 lines (67 loc) · 1.71 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
package kubemq
import (
"context"
"fmt"
)
type Event struct {
Id string
Channel string
Metadata string
Body []byte
ClientId string
Tags map[string]string
transport Transport
}
func NewEvent() *Event {
return &Event{}
}
// SetId - set event id otherwise new random uuid will be set
func (e *Event) SetId(id string) *Event {
e.Id = id
return e
}
// SetClientId - set event ClientId - mandatory if default client was not set
func (e *Event) SetClientId(clientId string) *Event {
e.ClientId = clientId
return e
}
// SetMetadata - set event metadata - mandatory if body field was not set
func (e *Event) SetMetadata(metadata string) *Event {
e.Metadata = metadata
return e
}
// SetChannel - set event channel - mandatory if default channel was not set
func (e *Event) SetChannel(channel string) *Event {
e.Channel = channel
return e
}
// SetBody - set event body - mandatory if metadata field was not set
func (e *Event) SetBody(body []byte) *Event {
e.Body = body
return e
}
// SetTags - set key value tags to event message
func (e *Event) SetTags(tags map[string]string) *Event {
e.Tags = map[string]string{}
for key, value := range tags {
e.Tags[key] = value
}
return e
}
// AddTag - add key value tags to event message
func (e *Event) AddTag(key, value string) *Event {
if e.Tags == nil {
e.Tags = map[string]string{}
}
e.Tags[key] = value
return e
}
func (e *Event) Send(ctx context.Context) error {
if e.transport == nil {
return ErrNoTransportDefined
}
return e.transport.SendEvent(ctx, e)
}
func (e *Event) String() string {
return fmt.Sprintf("Id: %s, Channel: %s, Metadata: %s, Body: %s, ClientId: %s, Tags: %s", e.Id, e.Channel, e.Metadata, e.Body, e.ClientId, e.Tags)
}