encoding

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Encoder
	Decoder
}

Codec is a convenience interface that combines both encoding and decoding capabilities.

var DefaultJSONB Codec = &JSONB{}

type Decoder

type Decoder interface {
	Decode(data []byte, v any) error
}

Decoder is an interface for types that can convert a byte slice back into a Go value (like an event struct).

Usage:

var decoder Decoder = NewJSONB()
eventBytes := []byte(`{"amount": 100}`)
var moneyDepositedEvent MoneyDeposited
err := decoder.Decode(eventBytes, &moneyDepositedEvent)

Returns an error if decoding fails. The target `v` must be a pointer.

type DecoderFunc

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

DecoderFunc is a function type that matches the signature of the Codec.Decode method.

type Encoder

type Encoder interface {
	Encode(v any) ([]byte, error)
}

Encoder is an interface for types that can convert a Go value (like an event struct) into a byte slice for storage or transmission.

Usage:

var encoder Encoder = NewJSONB()
data, err := encoder.Encode(myEvent)

Returns a byte slice representing the encoded value and an error if the encoding fails.

type EncoderFunc

type EncoderFunc = func(v any) ([]byte, error)

EncoderFunc is a function type that matches the signature of the Codec.Encode method.

type Generic

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

Generic provides a flexible, function-based implementation of the Codec interface. It allows you to create a custom codec by simply providing the encoding and decoding functions, without needing to define a new struct.

Usage:

// Create a codec using the standard library's JSON functions.
customCodec := NewGeneric(json.Marshal, json.Unmarshal)

func NewGeneric

func NewGeneric(
	encode EncoderFunc,
	decode DecoderFunc,
) *Generic

NewGeneric creates a new Generic codec from the provided encode and decode functions.

func (*Generic) Decode

func (gb *Generic) Decode(data []byte, v any) error

Decoder calls the underlying decode function.

func (*Generic) Encode

func (gb *Generic) Encode(v any) ([]byte, error)

Encode calls the underlying encode function.

type JSONB

type JSONB struct{}

JSONB (JSON Binary) is a concrete implementation of Codec that uses the standard library's `encoding/json` package. This is the default codec used throughout the framework.

func NewJSONB

func NewJSONB() *JSONB

NewJSONB creates a new JSONB (JSON Binary) codec.

func (*JSONB) Decode

func (jb *JSONB) Decode(data []byte, v any) error

Decoder uses json.Unmarshal to decode the data into the value.

func (*JSONB) Encode

func (jb *JSONB) Encode(v any) ([]byte, error)

Encode uses json.Marshal to encode the value.

type Protobuf

type Protobuf struct{}

Protobuf is a concrete implementation of Codec for Google's Protocol Buffers. It uses the `google.golang.org/protobuf/proto` package for its operations.

Any value passed to its methods for encoding or decoding *must* implement the `proto.Message` interface. This interface is automatically implemented by structs generated by the protoc compiler.

Usage:

// Assuming `pb` is your generated protobuf package.
var codec Codec = NewProtobuf()

// Encoding
originalMsg := &pb.MyMessage{Id: "123", Body: "hello"}
data, err := codec.Encode(originalMsg)

// Decoding
var newMsg pb.MyMessage
err = codec.Decode(data, &newMsg)

func NewProtobuf

func NewProtobuf() *Protobuf

NewProtobuf creates a new Protobuf codec.

func (*Protobuf) Decode

func (ps *Protobuf) Decode(data []byte, v any) error

Decoder converts a byte slice from the Protocol Buffers wire format into a message struct using `proto.Unmarshal`.

The provided value `v` must be a pointer to a struct that implements the `proto.Message` interface (e.g., `&MyProtoMessage{}`). It will return an error if this is not the case.

func (*Protobuf) Encode

func (ps *Protobuf) Encode(v any) ([]byte, error)

Encode converts a Protocol Buffers message into its binary wire format using `proto.Marshal`.

The provided value `v` must implement the `proto.Message` interface. It will return an error if this is not the case.

Jump to

Keyboard shortcuts

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