Skip to content

Commit

Permalink
Event ReaderWriter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBurian committed Aug 24, 2017
0 parents commit d73832f
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package eventsource

import (
"bytes"
"strconv"
)

// Holds the data for an event
type Event struct {
id []byte
data [][]byte
event []byte
retry uint64
buf bytes.Buffer
bufSet bool
}

// Read the event in wire format
func (e *Event) Read(p []byte) (int, error) {
if e.bufSet {
return e.buf.Read(p)
}

// Wipe out any existing data
e.buf.Reset()

// event:
if len(e.event) > 0 {
e.buf.WriteString("event: ")
e.buf.Write(e.event)
e.buf.WriteByte('\n')
}

// id:
if len(e.id) > 0 {
e.buf.WriteString("id: ")
e.buf.Write(e.id)
e.buf.WriteByte('\n')
}

// data:
if len(e.data) > 0 {
for _, entry := range e.data {
e.buf.WriteString("data: ")
e.buf.Write(entry)
e.buf.WriteByte('\n')
}
}

// retry:
if e.retry > 0 {
e.buf.WriteString("retry: ")
e.buf.WriteString(strconv.FormatUint(e.retry, 10))
e.buf.WriteByte('\n')
}

e.buf.WriteByte('\n')
e.bufSet = true

return e.buf.Read(p)
}

// Write to the event. Buffer will be converted to one or more
// `data` sections in wire format
//
// Successive calls to write will each create data entry lines
//
// Newlines will be split into multiple data entry lines, successive
// newlines are discarded
func (e *Event) Write(p []byte) (int, error) {
// split event on newlines
split := bytes.Split(p, []byte{'\n'})
for _, entry := range split {
// don't write empty entries
if len(entry) == 0 {
continue
}
e.data = append(e.data, entry)
}
e.bufSet = false
return len(p), nil
}

0 comments on commit d73832f

Please sign in to comment.