codecs

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 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

View Source
const MimeJSON = "application/json"

MimeJSON is the mime type for JSON.

View Source
const MimeMsgpack = "application/msgpack"

MimeMsgpack is the mime type for MessagePack.

View Source
const MimeProto = "application/x-protobuf"

MimeProto is the mime type for protocol buffer.

View Source
const MimeTOML = "application/toml"

MimeTOML is the mime type for TOML.

View Source
const MimeXML = "application/xml"

MimeXML is the mime type for XML.

View Source
const MimeYAML = "application/yaml"

MimeYAML is the mime type for YAML.

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")

	// ErrUnknownExt happens when you request a codec for a unknown file extension.
	ErrUnknownExt = errors.New("unknown file extension 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 {
	// Marshal encodes "v" into byte sequence.
	// "v" must be a pointer value.
	Marshal(v any) ([]byte, error)

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

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

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

	// 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

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

	// Name returns the codec name.
	Name() 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 GetExt added in v0.3.0

func GetExt(ext string) (Marshaler, error)

GetExt returns a codec for a file extension.

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