codec

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 5 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make added in v0.1.2

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

Make initializes the data that is registered under the given name. If the data type is not the provided generic type, an error is returned.

func Register added in v0.1.2

func Register[D any](r Registerer, name string)

Register registers the generic data type under the given name.

Types

type Encoding

type Encoding interface {
	Marshal(any) ([]byte, error)
	Unmarshal([]byte, string) (any, error)
}

Encoding can be used to encode registered data types to and from bytes.

type Marshaler added in v0.2.0

type Marshaler interface {
	Marshal() ([]byte, error)
}

Marshaler can be implemented by data types to override the default marshaler.

type Option added in v0.2.0

type Option func(*Registry)

Option is an option for the Registry.

func Debug added in v0.2.0

func Debug(debug bool) Option

Debug returns an Option that configures the Registry to log debug information.

func Default added in v0.2.0

func Default(marshal func(any) ([]byte, error), unmarshal func([]byte, any) error) Option

Default returns an Option that configures the default marshaler and unmarshaler functions to be used when data types do not override the default marshaler and unmarshaler.

type Registerer added in v0.2.0

type Registerer interface {
	Register(string, func() any)
}

Registerer is implemented by Registry to allow for registering of data types.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry is a registry of data types. A Registry marshals and unmarshals event data and command payloads.

func New

func New(opts ...Option) *Registry

New returns a new Registry for encoding and decoding of event data or command payloads.

func (*Registry) Map added in v0.2.5

func (r *Registry) Map() map[string]func() any

Map returns all registered factory functions, mapped to the registered name.

func (*Registry) Marshal added in v0.2.0

func (r *Registry) Marshal(data any) ([]byte, error)

Marshal marshals the provided data to a byte slice.

func (*Registry) New

func (r *Registry) New(name string) (any, error)

New initializes the data type that is registered under the given name and returns a pointer to the data.

func (*Registry) Register

func (r *Registry) Register(name string, factory func() any)

Register registers the data type with the given name. The provided factory function is used to initialize the data type when needed. Call the package-level Register function instead to register using a generic type:

var r *codec.Registry
codec.Register[FooData](r, "foo")

func (*Registry) Unmarshal added in v0.2.0

func (r *Registry) Unmarshal(b []byte, name string) (any, error)

Unmarshal unmarshals the provided bytes to the data type that is registered under the given name.

type Unmarshaler added in v0.2.0

type Unmarshaler interface {
	Unmarshal([]byte) error
}

Unmarshaler can be implemented by data types to override the default unmarshaler.

Jump to

Keyboard shortcuts

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