-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.go
More file actions
58 lines (47 loc) · 1.03 KB
/
event.go
File metadata and controls
58 lines (47 loc) · 1.03 KB
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
package cone
import "context"
func NewEvent(subject string, body []byte) (*Event, error) {
return NewEventWithContext(context.Background(), subject, body)
}
func NewEventWithContext(ctx context.Context, subject string, body []byte) (*Event, error) {
return &Event{Subject: subject, Body: body, Header: make(Header), ctx: ctx}, nil
}
type Event struct {
Subject string
Body []byte
Header Header
ctx context.Context
}
func (e *Event) Context() context.Context {
return e.ctx
}
func (e *Event) WithContext(ctx context.Context) *Event {
if ctx == nil {
panic("nil context")
}
e2 := new(Event)
*e2 = *e
e2.ctx = ctx
return e2
}
type Header map[string][]string
func (h Header) Set(key, value string) {
h[key] = []string{value}
}
func (h Header) Add(key, value string) {
h[key] = append(h[key], value)
}
func (h Header) Get(key string) string {
values, ok := h[key]
if !ok {
return ""
}
return values[0]
}
func (h Header) Values(key string) []string {
values, ok := h[key]
if !ok {
return nil
}
return values
}