Documentation
¶
Overview ¶
Package msgpack provides a MessagePack serializer implementation for mink.
MessagePack is a binary serialization format that produces smaller payloads than JSON while maintaining similar flexibility. It's particularly useful for high-throughput event sourcing applications.
Basic usage:
serializer := msgpack.NewSerializer()
serializer.Register("OrderCreated", OrderCreated{})
data, err := serializer.Serialize(OrderCreated{OrderID: "123"})
event, err := serializer.Deserialize(data, "OrderCreated")
Index ¶
- type SerializationError
- type Serializer
- func (s *Serializer) Count() int
- func (s *Serializer) Deserialize(data []byte, eventType string) (interface{}, error)
- func (s *Serializer) Lookup(eventType string) (reflect.Type, bool)
- func (s *Serializer) Register(eventType string, example interface{})
- func (s *Serializer) RegisterAll(examples ...interface{})
- func (s *Serializer) RegisteredTypes() []string
- func (s *Serializer) Serialize(event interface{}) ([]byte, error)
- type SerializerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SerializationError ¶
type SerializationError struct {
EventType string
Operation string // "serialize" or "deserialize"
Err error
}
SerializationError represents a serialization or deserialization error.
func (*SerializationError) Error ¶
func (e *SerializationError) Error() string
Error implements the error interface.
func (*SerializationError) Unwrap ¶
func (e *SerializationError) Unwrap() error
Unwrap returns the underlying error.
type Serializer ¶
type Serializer struct {
// contains filtered or unexported fields
}
Serializer is a MessagePack implementation of mink.Serializer. It provides efficient binary serialization with type registry support.
func NewSerializer ¶
func NewSerializer() *Serializer
NewSerializer creates a new MessagePack Serializer with an empty registry.
func NewSerializerWithOptions ¶
func NewSerializerWithOptions(opts ...SerializerOption) *Serializer
NewSerializerWithOptions creates a new Serializer with the given options.
func (*Serializer) Count ¶
func (s *Serializer) Count() int
Count returns the number of registered event types.
func (*Serializer) Deserialize ¶
func (s *Serializer) Deserialize(data []byte, eventType string) (interface{}, error)
Deserialize converts MessagePack bytes back to an event. If the event type is registered, returns a value of that type. Otherwise, returns a map[string]interface{}.
func (*Serializer) Lookup ¶
func (s *Serializer) Lookup(eventType string) (reflect.Type, bool)
Lookup returns the Go type for the given event type name. Returns nil and false if the type is not registered.
func (*Serializer) Register ¶
func (s *Serializer) Register(eventType string, example interface{})
Register adds a mapping from eventType to the Go type of the example. The example should be a value (not a pointer) of the event type.
func (*Serializer) RegisterAll ¶
func (s *Serializer) RegisterAll(examples ...interface{})
RegisterAll registers multiple events using their struct names as type names. Each example should be a value (not a pointer) of the event type.
func (*Serializer) RegisteredTypes ¶
func (s *Serializer) RegisteredTypes() []string
RegisteredTypes returns a slice of all registered event type names.
func (*Serializer) Serialize ¶
func (s *Serializer) Serialize(event interface{}) ([]byte, error)
Serialize converts an event to MessagePack bytes.
type SerializerOption ¶
type SerializerOption func(*Serializer)
SerializerOption configures a Serializer.
func WithRegistry ¶
func WithRegistry(registry map[string]reflect.Type) SerializerOption
WithRegistry sets the initial type registry.