codec

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2025 License: MIT Imports: 4 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidType indicates that the provided type is not supported by the codec operation.
	ErrInvalidType = errors.New("invalid type for codec operation")
	// ErrNilPointer indicates that a nil pointer was provided for marshaling, which is not allowed.
	ErrNilPointer = errors.New("nil pointer cannot be marshaled")
)

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Encoder
	Decoder
}

Codec combines both encoding and decoding capabilities in a single interface. It's useful for components that need bidirectional serialization support.

func JsonCodec

func JsonCodec() Codec

JsonCodec creates a codec for handling JSON serialization and deserialization. It uses the standard library's json.Marshal and json.Unmarshal functions. This codec can handle any type supported by the JSON package.

func NewCodec added in v0.0.3

func NewCodec(encoder EncoderFunc, decoder DecoderFunc) Codec

func StringCodec

func StringCodec() Codec

StringCodec creates a codec for handling string and *string types. It converts strings to bytes directly without any transformation. For decoding, the target must be a *string pointer.

type Decoder

type Decoder interface {
	// Unmarshal decodes the given byte slice into the provided value.
	// The val parameter must be a pointer to the target type.
	// Returns an error if the decoding fails.
	Unmarshal(data []byte, val any) error
}

Decoder defines the interface for decoding byte slices into values. It provides a generic way to deserialize bytes into any data type.

type DecoderFunc

type DecoderFunc func(data []byte, val any) error

DecoderFunc is a function adapter that implements the Decoder interface. It allows standalone functions to be used as Decoders.

func (DecoderFunc) Unmarshal

func (d DecoderFunc) Unmarshal(data []byte, val any) error

Unmarshal implements the Decoder interface by calling the function itself.

type Encoder

type Encoder interface {
	// Marshal encodes the given value into a byte slice.
	// Returns an error if the encoding fails.
	Marshal(val any) ([]byte, error)
}

Encoder defines the interface for encoding values into byte slices. It provides a generic way to serialize any data type to bytes.

type EncoderFunc

type EncoderFunc func(val any) ([]byte, error)

EncoderFunc is a function adapter that implements the Encoder interface. It allows standalone functions to be used as Encoders.

func (EncoderFunc) Marshal

func (e EncoderFunc) Marshal(val any) ([]byte, error)

Marshal implements the Encoder interface by calling the function itself.

type FallbackCodecGroup

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

FallbackCodecGroup implements a fallback mechanism for multiple codecs. It tries each codec in order until one succeeds for both marshal and unmarshal operations.

func NewCodecGroup

func NewCodecGroup(codecs ...Codec) *FallbackCodecGroup

NewCodecGroup creates a new FallbackCodecGroup with the provided codecs. The codecs will be tried in the order they are provided.

func (*FallbackCodecGroup) Marshal

func (m *FallbackCodecGroup) Marshal(value any) ([]byte, error)

Marshal attempts to marshal the value using each codec in order until one succeeds. Returns the marshaled data from the first successful codec, or an error if all codecs fail.

func (*FallbackCodecGroup) Unmarshal

func (m *FallbackCodecGroup) Unmarshal(data []byte, value any) error

Unmarshal attempts to unmarshal the data using each codec in order until one succeeds. Returns nil on the first successful unmarshal, or an error if all codecs fail.

Jump to

Keyboard shortcuts

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