Skip to content

Commit d8f958f

Browse files
committed
Event helpers
1 parent 0be37ff commit d8f958f

File tree

2 files changed

+109
-16
lines changed

2 files changed

+109
-16
lines changed

event.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,51 @@ package eventsource
22

33
import (
44
"bytes"
5-
"io"
65
"io/ioutil"
76
"strconv"
7+
"strings"
88
)
99

1010
// Holds the data for an event
1111
type Event struct {
12-
id []byte
13-
data [][]byte
14-
event []byte
12+
id string
13+
data []string
14+
event string
1515
retry uint64
1616
buf bytes.Buffer
1717
bufSet bool
1818
}
1919

20-
// DataEvent creates a new Event with the data field set
21-
func DataEvent(data string) *Event {
22-
e := &Event{}
23-
io.WriteString(e, data)
20+
// ID sets the event ID
21+
func (e *Event) ID(id string) *Event {
22+
e.id = id
2423
return e
2524
}
2625

27-
// ID sets the event ID
28-
func (e *Event) ID(id string) {
29-
e.id = []byte(id)
26+
// Type sets the event's event: field
27+
func (e *Event) Type(t string) *Event {
28+
e.event = t
29+
return e
30+
}
31+
32+
// Type sets the event's retry: field
33+
func (e *Event) Retry(t uint64) *Event {
34+
e.retry = t
35+
return e
36+
}
37+
38+
// Data replaces the data with the given string
39+
func (e *Event) Data(dat string) *Event {
40+
// truncate
41+
e.data = e.data[:0]
42+
e.WriteString(dat)
43+
return e
44+
}
45+
46+
// Adds data to the event without overwriting
47+
func (e *Event) AppendData(dat string) *Event {
48+
e.WriteString(dat)
49+
return e
3050
}
3151

3252
// Read the event in wire format
@@ -41,22 +61,22 @@ func (e *Event) Read(p []byte) (int, error) {
4161
// event:
4262
if len(e.event) > 0 {
4363
e.buf.WriteString("event: ")
44-
e.buf.Write(e.event)
64+
e.buf.WriteString(e.event)
4565
e.buf.WriteByte('\n')
4666
}
4767

4868
// id:
4969
if len(e.id) > 0 {
5070
e.buf.WriteString("id: ")
51-
e.buf.Write(e.id)
71+
e.buf.WriteString(e.id)
5272
e.buf.WriteByte('\n')
5373
}
5474

5575
// data:
5676
if len(e.data) > 0 {
5777
for _, entry := range e.data {
5878
e.buf.WriteString("data: ")
59-
e.buf.Write(entry)
79+
e.buf.WriteString(entry)
6080
e.buf.WriteByte('\n')
6181
}
6282
}
@@ -82,8 +102,13 @@ func (e *Event) Read(p []byte) (int, error) {
82102
// Newlines will be split into multiple data entry lines, successive
83103
// newlines are discarded
84104
func (e *Event) Write(p []byte) (int, error) {
105+
e.WriteString(string(p))
106+
return len(p), nil
107+
}
108+
109+
func (e *Event) WriteString(p string) {
85110
// split event on newlines
86-
split := bytes.Split(p, []byte{'\n'})
111+
split := strings.Split(p, "\n")
87112
for _, entry := range split {
88113
// don't write empty entries
89114
if len(entry) == 0 {
@@ -92,7 +117,6 @@ func (e *Event) Write(p []byte) (int, error) {
92117
e.data = append(e.data, entry)
93118
}
94119
e.bufSet = false
95-
return len(p), nil
96120
}
97121

98122
// WriteRaw sets an event directly in wire format

event_factory.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package eventsource
2+
3+
import (
4+
"io"
5+
"strconv"
6+
)
7+
8+
// EventFactory is a type of object that can create new events
9+
type EventFactory interface {
10+
New() *Event
11+
}
12+
13+
// EventIdIncrementer is an event factory that creates events with
14+
// sequential ID fields.
15+
// If NewFunc is set, the factory uses it to create events before setting
16+
// their IDs
17+
type EventIdFactory struct {
18+
NewFunc func() *Event
19+
Next uint64
20+
}
21+
22+
// New creates an event with the Next id in the sequence
23+
func (f *EventIdFactory) New() *Event {
24+
var e *Event
25+
if f.NewFunc != nil {
26+
e = f.NewFunc()
27+
} else {
28+
e = &Event{}
29+
}
30+
31+
e.id = strconv.FormatUint(f.Next, 10)
32+
f.Next++
33+
return e
34+
}
35+
36+
// EventTypeFactory creates events of a specific type
37+
type EventTypeFactory struct {
38+
NewFunc func() *Event
39+
Type string
40+
}
41+
42+
// New creates an event with the event type set
43+
// If NewFunc is set, the factory uses it to create events before setting
44+
// their event types
45+
func (f *EventTypeFactory) New() *Event {
46+
var e *Event
47+
if f.NewFunc != nil {
48+
e = f.NewFunc()
49+
} else {
50+
e = &Event{}
51+
}
52+
53+
e.event = f.Type
54+
return e
55+
}
56+
57+
// DataEvent creates a new Event with the data field set
58+
func DataEvent(data string) *Event {
59+
e := &Event{}
60+
io.WriteString(e, data)
61+
return e
62+
}
63+
64+
// TypeEvent creates a new Event with the event field set
65+
func TypeEvent(t string) *Event {
66+
return &Event{
67+
event: t,
68+
}
69+
}

0 commit comments

Comments
 (0)