Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Codec ¶
Codec is a convenience interface that combines both encoding and decoding capabilities.
type Decoder ¶
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 ¶
DecoderFunc is a function type that matches the signature of the Codec.Decode method.
type Encoder ¶
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 ¶
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.
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.
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 (*Protobuf) Decode ¶
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.