Documentation
¶
Index ¶
- Variables
- type Decoder
- type DecoderFunc
- type Encoder
- type EncoderFunc
- type Encoding
- type GobOption
- type GobRegistry
- type JSONRegistry
- type Registry
- func (reg *Registry) Decode(r io.Reader, name string) (interface{}, error)
- func (reg *Registry) Encode(w io.Writer, name string, data interface{}) error
- func (reg *Registry) New(name string) (interface{}, error)
- func (reg *Registry) Register(name string, enc Encoder, dec Decoder, makeFunc func() interface{})
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 ¶
This section is empty.
Types ¶
type Decoder ¶
type Decoder interface { // Decode decodes the data in r and returns the decoded data. Decode(r io.Reader) (interface{}, 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 interface { // Encode encodes the given data and writes the result into w. Encode(w io.Writer, data interface{}) 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 Encoding ¶
type Encoding interface { // Encode encodes the given data using the configured encoder for the given name. Encode(w io.Writer, name string, data interface{}) error // Decode decodes the data in r using the configured decoder for the given name. Decode(r io.Reader, name string) (interface{}, error) }
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() interface{})
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) JSONRegister ¶
func (reg *JSONRegistry) JSONRegister(name string, makeFunc func() interface{})
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() interface{} { 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.
func (*Registry) Encode ¶
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 (*Registry) New ¶
New creates and returns a new instance of the data that is registered under the given name. If no factory function was provided for this data, ErrMissingFactory is returned.
func (*Registry) Register ¶
Register registers the given Encoder and Decoder under the given name. When reg.Encode is called, the provided Encoder is be used to encode the given data. When reg.Decode is called, the provided Decoder is used. The makeFunc is required for custom data unmarshalers to work.