codecs

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2025 License: Apache-2.0 Imports: 4 Imported by: 35

Documentation

Overview

Package codecs is provides an interface to encode and decode content types to and from byte sequences.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownMimeType happens when you request a codec for a unknown mime type.
	ErrUnknownMimeType = errors.New("unknown mime type given")

	// ErrUnknownValueType happens when you give a golang type to a codec that doesn't understand it.
	ErrUnknownValueType = errors.New("unknown golang type given")
)
View Source
var (
	// ErrNoFileMarshaler happens when we haven't found a marshaler for the given file.
	ErrNoFileMarshaler = errors.New("no marshaler for the given file found")
)
View Source
var Plugins = container.NewMap[string, Marshaler]() //nolint:gochecknoglobals

Plugins is the registry for codec plugins.

Functions

func Register added in v0.1.0

func Register(name string, codec Marshaler) bool

Register makes a plugin available by the provided name.

Types

type Decoder

type Decoder interface {
	Decode(v any) error
}

Decoder decodes a byte sequence.

type DecoderFunc

type DecoderFunc func(v any) error

DecoderFunc adapts an decoder function into Decoder.

func (DecoderFunc) Decode

func (f DecoderFunc) Decode(v any) error

Decode delegates invocations to the underlying function itself.

type Encoder

type Encoder interface {
	Encode(v any) error
}

Encoder encodes payloads / fields into byte sequence.

type EncoderFunc

type EncoderFunc func(v any) error

EncoderFunc adapts an encoder function into Encoder.

func (EncoderFunc) Encode

func (f EncoderFunc) Encode(v any) error

Encode delegates invocations to the underlying function itself.

type Map

type Map map[string]Marshaler

Map is an alias for an codec map. Common keys to use here are either the plugin name or mime types.

type Marshaler

type Marshaler interface {
	// Encode encodes "v" into byte sequence.
	Encode(v any) ([]byte, error)

	// Decode decodes "data" into "v".
	// "v" must be a pointer value.
	Decode(data []byte, v any) error

	// NewDecoder returns a Decoder which reads byte sequence from "r".
	NewDecoder(r io.Reader) Decoder

	// NewEncoder returns an Encoder which writes bytes sequence into "w".
	NewEncoder(w io.Writer) Encoder

	// Encodes returns if this codec is able to encode the given type.
	Encodes(v any) bool

	// Decodes returns if this codec is able to decode the given type.
	Decodes(v any) bool

	// ContentTypes returns the list of content types this codec is able to
	// output.
	ContentTypes() []string

	// String returns the codec name.
	String() string

	// Exts returns the common file extensions for this encoder.
	Exts() []string
}

Marshaler is able to encode/decode a content type to/from a byte sequence.

func GetDecoder added in v0.1.0

func GetDecoder(mime string, v any) (Marshaler, error)

GetDecoder returns a decoder codec for a mime and golang type.

func GetEncoder added in v0.1.0

func GetEncoder(mime string, v any) (Marshaler, error)

GetEncoder returns a encoder codec for a mime and golang type.

func GetMime added in v0.1.0

func GetMime(mime string) (Marshaler, error)

GetMime returns a codec for a mime type.

Jump to

Keyboard shortcuts

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