Skip to content

Instantly share code, notes, and snippets.

@asim
Last active April 30, 2020 14:28
Show Gist options
  • Save asim/f0326dffef2f9be34aacf8db5ac0a9e8 to your computer and use it in GitHub Desktop.
Save asim/f0326dffef2f9be34aacf8db5ac0a9e8 to your computer and use it in GitHub Desktop.
The events interface
// Package events is for event streaming
package events
// Events is an event streaming interface
type Events interface {
// Stream returns a event stream by ID
Stream(id string, ...StreamOption) Stream
}
// Stream represents an event stream
type Stream interface {
// Read from the stream
Read() (*Event, error)
// Write to the stream
Write(*Event) error
}
// Event is a single event sent or received on a stream
type Event struct {
// Unique ID
Id string
// Timestamp of event
Timestamp time.Time
// Associated metadata
Header map[string]string
// Payload of the event
Body []byte
}
type StreamOptions struct {
// The offset at which to start reading from
Offset time.Time
}
type StreamOption func(*StreamOptions) error
// WithOffset sets the offset from which to read
type WithOffset(t time.Time) StreamOption {
return func(o *StreamOptions) error {
o.Offset = t
}
}
@derekcollison
Copy link

NATS is about to get header support.. ;)

@derekcollison
Copy link

We already have the notion of response types of Singelton, Chunked, Streamed.

@asim
Copy link
Author

asim commented Apr 30, 2020

Thats useful. If you have header support then we don't need to json encode our types, we can map 1:1 in our current broker.

@kylehqcom
Copy link

Can I ask a Qs re: the visibility of the StreamOptions struct?

When I personally use the "WithFunc" pattern, I generally keep the ***Options struct and its members private because only the package uses these values internally. The above looks the same in that the Events interface Stream func receives a variadic of StreamOption, so the OptionS is not used from outside callers.

Being public may be a better fit for calling code you have I'm not aware of, I'm just trying to better understand my own preference here to private vs public members and why public was chosen here. Thanks K

@asim
Copy link
Author

asim commented Apr 30, 2020

The use case here is that you'll then provide Options as a method on Stream so that you can actually access these and see whats set. Often you might be passing the interfaces around and you need to know what the setup is. If its private, its opaque. You basically lose the config. And if you want to create a new instance of something again, you don't have those values.

@kylehqcom
Copy link

👍 Ok thanks makes sense - thanks for the time & response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment