modernice / goes

goes is an event-sourcing framework for Go.
https://goes.modernice.dev
Apache License 2.0
134 stars 12 forks source link

Change how registries work #15

Closed bounoable closed 2 years ago

bounoable commented 3 years ago

Currently, events & commands must be registered using a factory function:

package example

type fooData struct{}

func registerEvents(r event.Registry) {
    r.Register("foo", func() event.Data { return fooData{} })
}

type fooPayload struct{}

func registerCommands(r command.Registry) {
    r.Register("foo", func() command.Payload { return fooPayload{} })
}

event.Data & command.Payload are both empty interfaces. Both registries can be combined into a single implementation that uses interface{}es instead (single implementation can also make use of generics when they land).

New design:

package codec

type Encoding interface {
    Encode(w io.Writer, name string, val interface{}) error
    Decode(name string, r io.Reader) (interface{}, error)
}

// Encoder is the encoder for a specific event data or command payload.
type Encoder interface {
    Encode(w io.Writer, interface{}) error
}

// Decoder is the decoder for a specific event data or command payload.
type Decoder interface {
    Decode(io.Reader) (interface{}, error)
}

type Registry struct { ... } // implements Encoding

// Register the encoder & decoder for specific event data or command payload.
func (r *Registry) Register(name string, enc Encoder, dec Decoder)

// Gob registers the encoding for the event data or command payload with the given name.
// It uses the provided factory function to make the event data and encodes & decodes it using encoding/gob.
//
// This is basically the previous implementation.
func (r *Registry) Gob(name string, make func() interface{})