Last active
April 30, 2020 14:28
-
-
Save asim/f0326dffef2f9be34aacf8db5ac0a9e8 to your computer and use it in GitHub Desktop.
The events interface
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 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 | |
} | |
} |
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.
👍 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
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 theEvents
interfaceStream
func receives a variadic ofStreamOption
, 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