Documentation
¶
Overview ¶
Package codecs is provides an interface to encode and decode content types to and from byte sequences.
Index ¶
Constants ¶
const MimeJSON = "application/json"
MimeJSON is the mime type for JSON.
const MimeProto = "application/x-protobuf"
MimeProto is the mime type for protocol buffer.
const MimeTOML = "application/toml"
MimeTOML is the mime type for TOML.
const MimeXML = "application/xml"
MimeXML is the mime type for XML.
const MimeYAML = "application/yaml"
MimeYAML is the mime type for YAML.
Variables ¶
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") )
var ( // ErrNoFileMarshaler happens when we haven't found a marshaler for the given file. ErrNoFileMarshaler = errors.New("no marshaler for the given file found") )
var Plugins = container.NewMap[string, Marshaler]() //nolint:gochecknoglobals
Plugins is the registry for codec plugins.
Functions ¶
Types ¶
type DecoderFunc ¶
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 EncoderFunc ¶
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 ¶
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
GetDecoder returns a decoder codec for a mime and golang type.
func GetEncoder ¶ added in v0.1.0
GetEncoder returns a encoder codec for a mime and golang type.