Documentation
¶
Overview ¶
Package codec provides a flexible framework for encoding and decoding data structures. It supports multiple encoding formats (GOB, MessagePack) and uses generics for type safety. The primary use case is to serialize complex types into a string format for transport or storage, for example, in session data.
Package codec provides a flexible framework for encoding and decoding data structures. It supports multiple encoding formats (GOB, MessagePack) and uses generics for type safety.
Package codec provides a flexible framework for encoding and decoding data structures. It supports multiple encoding formats (GOB, MessagePack) and uses generics for type safety.
Package codec provides a flexible framework for encoding and decoding data structures. It supports multiple encoding formats (GOB, MessagePack) and uses generics for type safety.
Package codec provides a flexible framework for encoding and decoding data structures. It supports multiple encoding formats (GOB, MessagePack) and uses generics for type safety.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ERR_UnknownCodecMethod is returned when an unsupported codec method is specified. ErrUnknownCodecMethod = errors.New("unknown codec method") // ERR_GobEncodeFailed is returned when GOB serialization fails. ErrGobEncodeFailed = errors.New("gob encode failed") // ERR_GobDecodeFailed is returned when GOB deserialization fails. ErrGobDecodeFailed = errors.New("gob decode failed") // ERR_MsgPackEncodeFailed is returned when MessagePack serialization fails. ErrMsgPackEncodeFailed = errors.New("msgpack encode failed") // ERR_MsgPackDecodeFailed is returned when MessagePack deserialization fails. ErrMsgPackDecodeFailed = errors.New("msgpack decode failed") // ERR_Base64DecodeFailed is returned when Base64 decoding of the input string fails. ErrBase64DecodeFailed = errors.New("base64 decode failed") // Status_CodecError is a pre-defined gRPC status for codec-related errors. Status_CodecError = status.New(codes.Internal, "codec error") )
Standardized errors for the codec package.
Functions ¶
func Decode ¶
Decode deserializes a string back into a specific type T using the globally configured default codec. The string is expected to be a Base64 representation of the binary data (GOB or MessagePack).
func Encode ¶
Encode serializes a value of any type into a string using the globally configured default codec. The value is first encoded into a binary format (GOB or MessagePack) and then into a Base64 string.
func ToStatus ¶
ToStatus converts a codec-related wrapper error into a gRPC status.Status. This is useful for providing detailed, structured error information to gRPC clients.
err: The wrapped error containing the original error and a descriptive message. Returns a gRPC status with detailed violation information, or nil if the input error is nil.
func WithCodecMethod ¶
func WithCodecMethod(method CodecMethod)
WithCodecMethod sets the global default encoding method for the package. This function uses sync.Once to ensure that the codec method can only be set once, preventing inconsistent encoding/decoding formats during runtime. It should be called during the application's initialization phase.
Example:
func main() {
codec.WithCodecMethod(codec.MSGPACK)
// ... rest of the application
}
Types ¶
type Codec ¶
type Codec[T any] interface { // Encode serializes a given value of type T into a string. Encode(v T) (string, error) // Decode deserializes a string back into a value of type T. Decode(s string) (T, error) // Method returns the specific encoding method used by the codec. Method() CodecMethod }
Codec defines the interface for any encoding/decoding implementation. It uses generics to ensure type safety between encoding and decoding operations.
type CodecMethod ¶
type CodecMethod string
CodecMethod is a type that defines the available encoding formats. Using a custom type provides better type safety than using plain strings.
const ( GOB CodecMethod = "gob" // GOB is a Go-specific binary encoding format. MSGPACK CodecMethod = "msgpack" // MessagePack is a fast, compact binary serialization format. )
Constants for the supported encoding methods.