Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
DecoderFunc is a function adapter that implements the Decoder interface. It allows standalone functions to be used as Decoders.
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 ¶
EncoderFunc is a function adapter that implements the Encoder interface. It allows standalone functions to be used as Encoders.
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.