codec

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package codec provides message encoding/decoding for the wt framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CBOR

type CBOR struct{}

CBOR is a lightweight CBOR (Concise Binary Object Representation) codec. Implements RFC 8949 major types for basic Go types. For full CBOR support, use a dedicated library like github.com/fxamacker/cbor/v2.

func (CBOR) Marshal

func (CBOR) Marshal(v any) ([]byte, error)

func (CBOR) Name

func (CBOR) Name() string

func (CBOR) Unmarshal

func (CBOR) Unmarshal(data []byte, v any) error

type Codec

type Codec interface {
	// Marshal encodes a value into bytes.
	Marshal(v any) ([]byte, error)
	// Unmarshal decodes bytes into a value.
	Unmarshal(data []byte, v any) error
	// Name returns the codec name (e.g., "json", "protobuf").
	Name() string
}

Codec defines the interface for message serialization.

type JSON

type JSON struct{}

JSON is a codec using encoding/json.

func (JSON) Marshal

func (JSON) Marshal(v any) ([]byte, error)

func (JSON) Name

func (JSON) Name() string

func (JSON) Unmarshal

func (JSON) Unmarshal(data []byte, v any) error

type MsgPack

type MsgPack struct{}

MsgPack is a lightweight MessagePack codec. It supports basic types: string, int, float, bool, nil, []byte, slices, maps, and structs. For production use with complex types, consider importing a full msgpack library.

func (MsgPack) Marshal

func (MsgPack) Marshal(v any) ([]byte, error)

func (MsgPack) Name

func (MsgPack) Name() string

func (MsgPack) Unmarshal

func (MsgPack) Unmarshal(data []byte, v any) error

type Registry

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

Registry holds named codecs.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a Registry with JSON as the default.

func (*Registry) Default

func (r *Registry) Default() Codec

Default returns the fallback codec.

func (*Registry) Get

func (r *Registry) Get(name string) (Codec, error)

Get returns a codec by name.

func (*Registry) Register

func (r *Registry) Register(c Codec)

Register adds a codec.

func (*Registry) SetDefault

func (r *Registry) SetDefault(name string) error

SetDefault sets the default codec by name.

Jump to

Keyboard shortcuts

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