-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathevent.go
91 lines (75 loc) · 1.83 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
// Tideland Go Cells - Event
//
// Copyright (C) 2010-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package cells
//--------------------
// IMPORTS
//--------------------
import (
"fmt"
"time"
"github.com/tideland/golib/errors"
)
//--------------------
// EVENT
//--------------------
// Event transports what to process.
type Event interface {
fmt.Stringer
// Timestamp returns the UTC time the event has been created.
Timestamp() time.Time
// Topic returns the topic of the event.
Topic() string
// Payload returns the payload of the event.
Payload() Payload
}
// event implements the Event interface.
type event struct {
timestamp time.Time
topic string
payload Payload
}
// NewEvent creates a new event with the given topic and payload.
func NewEvent(topic string, payload interface{}) (Event, error) {
if topic == "" {
return nil, errors.New(ErrNoTopic, errorMessages)
}
p, err := NewPayload(payload)
if err != nil {
return nil, err
}
return &event{
timestamp: time.Now().UTC(),
topic: topic,
payload: p,
}, nil
}
// Timestamp implements the Event interface.
func (e *event) Timestamp() time.Time {
return e.timestamp
}
// Topic implements the Event interface.
func (e *event) Topic() string {
return e.topic
}
// Payload implements the Event interface.
func (e *event) Payload() Payload {
if e.payload == nil {
// Fallback to empty one.
return newEmptyPayload()
}
return e.payload
}
// String implements the Stringer interface.
func (e *event) String() string {
timeStr := e.timestamp.Format(time.RFC3339Nano)
payloadStr := "none"
if e.payload != nil {
payloadStr = fmt.Sprintf("%v", e.payload)
}
return fmt.Sprintf("<timestamp: %s / topic: '%s' / payload: %s>", timeStr, e.topic, payloadStr)
}
// EOF