codec

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: Apache-2.0 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

func Decode added in v0.1.2

func Decode[D any](r *Registry, in io.Reader, name string) (D, 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 Encode added in v0.1.2

func Encode[D any](r *Registry, w io.Writer, name string, data D) 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 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)

func Make added in v0.1.2

func Make[D any](r *Registry, name string) (D, error)

Make 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. If the data cannot be casted to D, an error is returned.

func Register added in v0.1.2

func Register[D any, Enc Encoder[D], Dec Decoder[D]](r *Registry, name string, enc Enc, dec Dec)

Register registers the encoding for events with the given name.

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

type DecoderFunc[T any] func(r io.Reader) (T, error)

DecoderFunc allows a function to be used as a Decoder.

func (DecoderFunc[T]) Decode

func (decode DecoderFunc[T]) Decode(r io.Reader) (T, error)

Decode returns decode(r).

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

type EncoderFunc[T any] func(w io.Writer, data T) error

EncoderFunc allows a function to be used as an Encoder.

func (EncoderFunc[T]) Encode

func (encode EncoderFunc[T]) Encode(w io.Writer, data T) 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 any) error

	// Decode decodes the data in r using the configured decoder for the given name.
	Decode(r io.Reader, name string) (any, 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() 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

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() 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 New

func New() *Registry

New returns a new Registry.

func (*Registry) Decode

func (reg *Registry) Decode(r io.Reader, name string) (any, 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 any) 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) (any, 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.

Jump to

Keyboard shortcuts

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