Documentation ¶
Index ¶
- Variables
- func Decode[D any](r *Registry, in io.Reader, name string) (D, error)
- func Encode[D any](r *Registry, w io.Writer, name string, data D) error
- func GobRegister[T any](r *GobRegistry, name string)
- func JSONRegister[T any](r *JSONRegistry, name string)
- func Make[D any](r *Registry, name string) (D, error)
- func Register[D any, Enc Encoder[D], Dec Decoder[D]](r *Registry, name string, enc Enc, dec Dec)
- type Decoder
- type DecoderFunc
- type Encoder
- type EncoderFunc
- type Encoding
- type GobOption
- type GobRegistry
- type JSONRegistry
- type Registry
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is returned when trying to encode/decode data which hasn't // been registered into a registry. ErrNotFound = errors.New("encoding not found. forgot to register?") // ErrMissingFactory is returned when trying to instantiate data for which // no factory function was provided. ErrMissingFactory = errors.New("missing factory for data. forgot to register?") )
Functions ¶
func Decode ¶ added in v0.1.2
Decode decodes the data that is registered under the given name using the registered Decoder. If no Decoder is registered for the give name, an error that unwraps to ErrNotFound is returned.
func Encode ¶ added in v0.1.2
Encode encodes the data that is registered under the given name using the registered Encoder. If no Encoder is registered for the given name, an error that unwraps to ErrNotFound is returned.
func GobRegister ¶ added in v0.1.2
func GobRegister[T any](r *GobRegistry, name string)
func JSONRegister ¶ added in v0.1.2
func JSONRegister[T any](r *JSONRegistry, name string)
Types ¶
type Decoder ¶
type Decoder[T any] interface { // Decode decodes the data in r and returns the decoded data. Decode(r io.Reader) (T, error) }
Decoder is a decoder for a specific event data or command payload.
type DecoderFunc ¶
DecoderFunc allows a function to be used as a Decoder.
type Encoder ¶
type Encoder[T any] interface { // Encode encodes the given data and writes the result into w. Encode(w io.Writer, data T) error }
Encoder is an encoder for a specific event data or command payload.
type EncoderFunc ¶
EncoderFunc allows a function to be used as an Encoder.
type GobOption ¶
type GobOption func(*GobRegistry)
GobOption is an option for a GobRegistry.
func GobNameFunc ¶
GobNameFunc returns a GobOption that specifies the name under which types are registered in the encoding/gob package. If no custom GobNameFunc is provided, the format for gob names is
fmt.Sprintf("goes(%s)", name)
type GobRegistry ¶
type GobRegistry struct { *Registry // contains filtered or unexported fields }
A GobRegistry allows registering data into a Registry using factory functions. Data that is registered via a GobRegistry will be encoded and decoded using the encoding/gob package.
func Gob ¶
func Gob(reg *Registry, opts ...GobOption) *GobRegistry
Gob wraps the given Registry in a GobRegistry. The GobRegistry provides a GobRegister function to register data using a factory function.
If reg is nil, a new underlying Registry is created with New().
func (*GobRegistry) GobRegister ¶
func (reg *GobRegistry) GobRegister(name string, makeFunc func() any)
GobRegister registers data with the given name into the underlying registry. makeFunc is used create instances of the data and encoding/gob will be used to encode and decode the data returned by makeFunc.
type JSONRegistry ¶
type JSONRegistry struct { *Registry // contains filtered or unexported fields }
A JSONRegistry allows registering data into a Registry using factory functions. Data that is registered via a JSONRegistry will be encoded and decoded using the encoding/json package.
func JSON ¶
func JSON(reg *Registry) *JSONRegistry
JSON wraps the given Registry in a JSONRegistry. The JSONRegistry provides a JSONRegister function to register data using a factory function.
If reg is nil, a new underlying Registry is created with New().
func (*JSONRegistry) IgnoreDecodeErrors ¶ added in v0.1.3
func (reg *JSONRegistry) IgnoreDecodeErrors(ignore bool)
func (*JSONRegistry) JSONRegister ¶
func (r *JSONRegistry) JSONRegister(name string, makeFunc func() any)
JSONRegister registers data with the given name into the underlying registry. makeFunc is used create instances of the data and encoding/json will be used to encode and decode the data returned by makeFunc.
func (*JSONRegistry) UseMapstructure ¶ added in v0.1.1
func (reg *JSONRegistry) UseMapstructure(use bool)
type Registry ¶
A Registry provides the Encoders and Decoders for event data or command payloads. Use the Register method to register the Encoder and Decoder for a specific type.
You likely don't want to use this registry directly, as it requires you to define an Encoder and Decoder for every registered type/name. You can for example wrap this *Registry in a *GobRegistry to use encoding/gob for encoding and decoding data:
Register
type fooData struct { ... } reg := Gob(New()) reg.GobRegister("foo", func() any { return fooData{}})
Encode
var w io.Writer err := reg.Encode(w, "foo", someData{...})
Decode
var r io.Reader err := reg.Decode(r, "foo")
func (*Registry) Decode ¶
Decode decodes the data that is registered under the given name using the registered Decoder. If no Decoder is registered for the give name, an error that unwraps to ErrNotFound is returned.