codecs

package
v0.0.1 Latest Latest
Warning

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

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

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 (
	// 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[Marshaler]() //nolint:gochecknoglobals

Plugins is the registry for codec plugins.

Functions

This section is empty.

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 marshals "v" into byte sequence.
	Marshal(v any) ([]byte, error)

	// Unmarshal unmarshals "data" into "v".
	// "v" must be a pointer value.
	Unmarshal(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

	// ContentTypes returns the list of content types this marshaller is able to
	// handle.
	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 GetCodec

func GetCodec(preference []string) (Marshaler, error)

GetCodec returns the first codec by preference list.

Jump to

Keyboard shortcuts

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