codec

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2022 License: MIT Imports: 10 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

type DecoderFunc func(r io.Reader) (interface{}, error)

DecoderFunc allows a function to be used as a Decoder.

func (DecoderFunc) Decode

func (decode DecoderFunc) Decode(r io.Reader) (interface{}, error)

Decode returns decode(r).

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

type EncoderFunc func(w io.Writer, data interface{}) error

EncoderFunc allows a function to be used as an Encoder.

func (EncoderFunc) Encode

func (encode EncoderFunc) Encode(w io.Writer, data interface{}) error

Encode returns encode(w, data).

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

func GobNameFunc(fn func(string) string) GobOption

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 }

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.

type Registry

type Registry struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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 New

func New() *Registry

New returns a new Registry for event data or command payloads.

func (*Registry) Decode

func (reg *Registry) Decode(r io.Reader, name string) (interface{}, error)

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

func (reg *Registry) Encode(w io.Writer, name string, data interface{}) error

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

func (reg *Registry) New(name string) (interface{}, error)

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

func (reg *Registry) Register(name string, enc Encoder, dec Decoder, makeFunc func() interface{})

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL