codec

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2024 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package codec provides an implementation of marshalkit.Marshaler that uses a set of priority-ordered codecs to marshal and unmarshal types and values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Capabilities

type Capabilities struct {
	// Types is a map of the supported types to their portable type name.
	Types map[reflect.Type]string
}

Capabilities describes the capabilities of a codec as it relates to specific Go types.

type Codec

type Codec interface {
	// Query returns the capabilities of the codec for the given types.
	Query(types []reflect.Type) Capabilities

	// BasicMediaType returns the type and subtype portion of the media-type
	// used to identify data encoded by this codec.
	BasicMediaType() string

	// Marshal returns the binary representation of v.
	Marshal(v any) ([]byte, error)

	// Unmarshal decodes a binary representation into v.
	Unmarshal(data []byte, v any) error
}

Codec is an interface for encoding and decoding values.

type Marshaler

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

Marshaler uses a set of priority-ordered codecs to marshal and unmarshal types and values.

func NewMarshaler

func NewMarshaler(
	types []reflect.Type,
	codecs []Codec,
) (*Marshaler, error)

NewMarshaler returns a new marshaler that uses the given set of codecs to marshal and unmarshal values.

The codecs are given in order of preference.

func (*Marshaler) Marshal

func (m *Marshaler) Marshal(v any) (marshalkit.Packet, error)

Marshal returns a binary representation of v.

func (*Marshaler) MarshalAs added in v0.5.0

func (m *Marshaler) MarshalAs(
	v any,
	mediaTypes []string,
) (p marshalkit.Packet, ok bool, err error)

MarshalAs returns a binary representation of v encoded using a format associated with one of the supplied media-types.

mediaTypes is a list of acceptible media-types, in order of preference. If none of the media-types are supported, ok is false.

func (*Marshaler) MarshalType

func (m *Marshaler) MarshalType(rt reflect.Type) (string, error)

MarshalType marshals a type to its portable representation.

func (*Marshaler) MediaTypesFor added in v0.5.0

func (m *Marshaler) MediaTypesFor(rt reflect.Type) []string

MediaTypesFor returns the media-types that the marshaler can use to represent the given type, in order of preference.

It returns an empty slice if the type is not supported.

func (*Marshaler) Unmarshal

func (m *Marshaler) Unmarshal(p marshalkit.Packet) (any, error)

Unmarshal produces a value from its binary representation.

func (*Marshaler) UnmarshalType

func (m *Marshaler) UnmarshalType(n string) (reflect.Type, error)

UnmarshalType unmarshals a type from its portable representation.

Directories

Path Synopsis
internal
portablename
Package portablename generates portable names for arbitrary types.
Package portablename generates portable names for arbitrary types.
Package json contains a codec that uses Go's standard JSON marshaling.
Package json contains a codec that uses Go's standard JSON marshaling.
Package protobuf contains codec implementations for protocol buffers types.
Package protobuf contains codec implementations for protocol buffers types.
Package stateless contains a codec implementation for dogma.StatelessProcessRoot values.
Package stateless contains a codec implementation for dogma.StatelessProcessRoot values.

Jump to

Keyboard shortcuts

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