This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.go
98 lines (80 loc) · 2.17 KB
/
basic.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
package lifecycle
import (
"fmt"
"time"
)
type basicEvent struct {
EventProtocol string `json:"protocol"`
EventID string `json:"id"`
Ident string `json:"identity"`
Comp string `json:"component"`
Timestamp int64 `json:"timestamp"`
EventFormat Format `json:"-"`
etype string
dtype Type
}
// Protocol retrieves the event protocol
func (e *basicEvent) Protocol() string {
return e.EventProtocol
}
// Format retrieves the encoding format for the event
func (e *basicEvent) Format() Format {
return e.EventFormat
}
// SetFormat sets the encoding format for the event
func (e *basicEvent) SetFormat(f Format) {
e.EventFormat = f
}
// ID is the v4 uuid of this message
func (e *basicEvent) ID() string {
return e.EventID
}
// Component is the component that produced the event
func (e *basicEvent) Component() string {
return e.Comp
}
// SetComponent sets the component for the event
func (e *basicEvent) SetComponent(c string) {
e.Comp = c
}
// SetIdentity sets the identity for the event
func (e *basicEvent) SetIdentity(i string) {
e.Ident = i
}
// Identity sets the identity for the event
func (e *basicEvent) Identity() string {
return e.Ident
}
// Target is where to publish the event to
func (e *basicEvent) Target() (string, error) {
if e.Comp == "" {
return "", fmt.Errorf("event is not complete, component has not been set")
}
return fmt.Sprintf("choria.lifecycle.event.%s.%s", e.etype, e.Comp), nil
}
// String is text suitable to display on the console etc
func (e *basicEvent) String() string {
return fmt.Sprintf("[%s] %s: %s", e.etype, e.Ident, e.Component())
}
// Type is the type of event
func (e *basicEvent) Type() Type {
return e.dtype
}
// TypeString the string representation of the event type
func (e *basicEvent) TypeString() string {
return e.etype
}
// Time retrieves the event time
func (e *basicEvent) TimeStamp() time.Time {
return time.Unix(e.Timestamp, 0)
}
func newBasicEvent(t string) basicEvent {
return basicEvent{
EventProtocol: fmt.Sprintf("io.choria.lifecycle.v1.%s", t),
EventID: eventID(),
Timestamp: timeStamp(),
EventFormat: ChoriaFormat,
etype: t,
dtype: eventTypes[t],
}
}