codec

package module
v0.0.0-...-8ba570d Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: MIT Imports: 6 Imported by: 0

README

codec-generic-go

Go Reference GoReportCard codecov

codec-generic-go provides the generic codec abstraction that can be used by concrete cmd-stream-go codecs.

It defines the common Codec structure independent of any specific serialization format.

Used By

Documentation

Overview

Package codec provides a generic, type-safe codec abstraction used for encoding and decoding values in the cmd-stream-go ecosystem.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFailedToMarshalByteSlice

func NewFailedToMarshalByteSlice(err error) error

NewFailedToMarshalByteSlice returns an error indicating that a byte slice could not be marshaled.

func NewFailedToMarshalDTM

func NewFailedToMarshalDTM(err error) error

NewFailedToMarshalDTM returns an error indicating that the data type marker (DTM) could not be marshaled.

func NewFailedToMarshalValue

func NewFailedToMarshalValue(value any, cause error) error

NewFailedToMarshalValue returns an error indicating that value marshaling failed.

func NewFailedToUnmarshalByteSlice

func NewFailedToUnmarshalByteSlice(err error) error

NewFailedToUnmarshalByteSlice returns an error indicating that a byte slice could not be unmarshaled.

func NewFailedToUnmarshalDTM

func NewFailedToUnmarshalDTM(err error) error

NewFailedToUnmarshalDTM returns an error indicating that the data type marker (DTM) could not be unmarshaled.

func NewFailedToUnmarshalValue

func NewFailedToUnmarshalValue(err error) error

NewFailedToUnmarshalValue returns an error indicating that value unmarshaling failed.

func NewUnrecognizedDTM

func NewUnrecognizedDTM(dtm com.DTM) error

NewUnrecognizedDTM returns an error indicating that an unknown data type marker (DTM) was received.

func NewUnrecognizedType

func NewUnrecognizedType(t reflect.Type) error

NewUnrecognizedType returns an error indicating that an unsupported type was encountered during encoding.

Types

type Codec

type Codec[T, V any] struct {
	// contains filtered or unexported fields
}

Codec represents a generic type-safe codec for encoding and decoding values. T is the type used for encoding, V is the type used for decoding.

func NewCodec

func NewCodec[T, V any](types1 []reflect.Type, types2 []reflect.Type,
	ser Serializer[T, V],
) (codec Codec[T, V])

NewCodec constructs a Codec with default value decoder logic.

Parameters:

  • types1 lists the Go types that can be encoded.
  • types2 lists the Go types that can be decoded.
  • ser is the serializer used for encoding/decoding values.

func NewCodecWithDecoder

func NewCodecWithDecoder[T, V any](types1 []reflect.Type, types2 []reflect.Type,
	ser Serializer[T, V],
	decodeValueFn DecodeValueFn[T, V],
) (codec Codec[T, V])

NewCodecWithDecoder constructs a Codec using a custom value decoder function.

decodeValueFn allows overriding the default decoding behavior.

func (Codec[T, V]) Decode

func (c Codec[T, V]) Decode(r transport.Reader) (v V, n int, err error)

Decode reads a value of type V from the given transport.Reader. Returns the decoded value, total bytes read, and any error.

func (Codec[T, V]) Encode

func (c Codec[T, V]) Encode(t T, w transport.Writer) (n int, err error)

Encode writes a value of type T to the given transport.Writer. Returns the total number of bytes written and any error.

type DecodeValueFn

type DecodeValueFn[T, V any] func(tp reflect.Type, ser Serializer[T, V],
	bs []byte) (v V, err error)

type Serializer

type Serializer[T, V any] interface {
	Marshal(t T) (bs []byte, err error)
	Unmarshal(bs []byte, v V) (err error)
}

Serializer defines the interface for encoding and decoding values.

T is the type used for marshaling (encoding), and V is the type used for unmarshaling (decoding).

Jump to

Keyboard shortcuts

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