Documentation
¶
Overview ¶
Package protobuf provides a Protocol Buffers serializer for mink events.
Protocol Buffers is a language-neutral, platform-neutral extensible mechanism for serializing structured data developed by Google. It offers smaller payloads and faster serialization compared to JSON.
Usage:
// Create a new serializer
s := protobuf.NewSerializer()
// Register event types (must implement proto.Message)
s.Register("OrderCreated", &pb.OrderCreated{})
s.Register("ItemAdded", &pb.ItemAdded{})
// Serialize an event
data, err := s.Serialize(event)
// Deserialize an event
result, err := s.Deserialize(data, "OrderCreated")
Note: Only types that implement proto.Message can be serialized with this serializer. For non-protobuf types, use the JSON or MessagePack serializers.
Index ¶
- Variables
- 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) MustRegister(eventType string, event interface{})
- func (s *Serializer) MustRegisterAll(events ...interface{})
- func (s *Serializer) Register(eventType string, event interface{}) error
- func (s *Serializer) RegisterAll(events ...interface{}) error
- func (s *Serializer) RegisteredTypes() []string
- func (s *Serializer) Serialize(event interface{}) ([]byte, error)
- type SerializerOption
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNilEvent indicates an attempt to serialize a nil event. ErrNilEvent = errors.New("mink/protobuf: cannot serialize nil event") // ErrEmptyData indicates an attempt to deserialize empty data. ErrEmptyData = errors.New("mink/protobuf: cannot deserialize empty data") // ErrNotProtoMessage indicates the event does not implement proto.Message. ErrNotProtoMessage = errors.New("mink/protobuf: event must implement proto.Message") // ErrTypeNotRegistered indicates the event type is not registered. ErrTypeNotRegistered = errors.New("mink/protobuf: event type not registered") )
Functions ¶
This section is empty.
Types ¶
type SerializationError ¶
type SerializationError struct {
// EventType is the name of the event type that failed.
EventType string
// Operation is either "serialize" or "deserialize".
Operation string
// Cause is the underlying error.
Cause error
}
SerializationError provides detailed error information for serialization failures.
func (*SerializationError) Error ¶
func (e *SerializationError) Error() string
Error returns the error message.
func (*SerializationError) Is ¶
func (e *SerializationError) Is(target error) bool
Is checks if the target error matches.
func (*SerializationError) Unwrap ¶
func (e *SerializationError) Unwrap() error
Unwrap returns the underlying error.
type Serializer ¶
type Serializer struct {
// contains filtered or unexported fields
}
Serializer implements the mink.Serializer interface using Protocol Buffers. It maintains a registry of event types for deserialization.
func NewSerializer ¶
func NewSerializer() *Serializer
NewSerializer creates a new Protocol Buffers serializer with an empty registry.
func NewSerializerWithOptions ¶
func NewSerializerWithOptions(opts ...SerializerOption) *Serializer
NewSerializerWithOptions creates a new serializer with the specified 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 Protocol Buffers binary data back to an event. The eventType must be registered for typed deserialization. If the type is not registered, returns ErrTypeNotRegistered.
Note: Protocol Buffers can produce empty byte slices for messages with all default/zero values. This is valid and will be deserialized correctly.
func (*Serializer) Lookup ¶
func (s *Serializer) Lookup(eventType string) (reflect.Type, bool)
Lookup returns the registered type for the given event type name.
func (*Serializer) MustRegister ¶
func (s *Serializer) MustRegister(eventType string, event interface{})
MustRegister registers an event type and panics on error.
func (*Serializer) MustRegisterAll ¶
func (s *Serializer) MustRegisterAll(events ...interface{})
MustRegisterAll registers multiple event types and panics on error.
func (*Serializer) Register ¶
func (s *Serializer) Register(eventType string, event interface{}) error
Register adds an event type to the registry. The event must implement proto.Message. If a type with the same name already exists, it will be overwritten.
func (*Serializer) RegisterAll ¶
func (s *Serializer) RegisterAll(events ...interface{}) error
RegisterAll registers multiple event types by inferring their names from type names. All events must implement proto.Message.
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 Protocol Buffers binary format. The event must implement proto.Message.
type SerializerOption ¶
type SerializerOption func(*Serializer)
SerializerOption configures the Serializer.
func WithRegistry ¶
func WithRegistry(registry map[string]reflect.Type) SerializerOption
WithRegistry initializes the serializer with a pre-configured registry. The map should contain event type names as keys and proto.Message types as values.