Last active
December 18, 2015 01:29
-
-
Save bernerdschaefer/5704500 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package eventsource | |
type Metadata map[string]string | |
func (Metadata) Set(key, value string) | |
func (Metadata) Get(key string) (string, bool) | |
func (Metadata) Del(key string) | |
type Writer struct { | |
} | |
func NewWriter(w io.Writer) *Writer | |
// Creates a new event with the provided metadata. The data of the message | |
// should be written to the returned WriteCloser. After writing, it should be | |
// closed to signal the end of the event's data. | |
func (*Writer) CreateEvent(meta Metadata) (io.WriteCloser, error) | |
type Reader struct { | |
} | |
func NewReader(r io.Reader) *Reader | |
func (*Reader) NextEvent() (*Event, error) | |
type Event struct { | |
Metadata Metadata | |
} | |
func (*Event) Read(buf []byte) (n int, err error) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Example() { | |
r, w := io.Pipe() | |
er, ew := eventsource.NewReader(r), eventsource.NewWriter(w) | |
go func() { | |
event, _ := ew.CreateEvent(Metadata{"id": "10"}) | |
json.NewEncoder(event).Encode(map[string]string{ | |
"value": "foo", | |
}) | |
event.Close() | |
}() | |
event, _ := er.NextEvent() | |
if id, ok := event.Metadata.Get("id"); ok { | |
// writer send an id | |
} | |
var data map[string]string | |
json.NewDecoder(event).Decode(&data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment