Documentation
¶
Index ¶
Constants ¶
const ( // MetadataKeyVersion is the key for the event message version. // It indicates the version of the event schema for compatibility checks. MetadataKeyVersion = "version" // MetadataKeySource is the key for the event message source. // It indicates where the event originated from (e.g., "user-service", "payment-api"). MetadataKeySource = "source" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseEventMessage ¶
type BaseEventMessage[T any] struct { EventType string `json:"event_type"` // Type of event (e.g., "user.created") Timestamp time.Time `json:"timestamp"` // When the event occurred Payload T `json:"payload"` // Typed business data Metadata map[string]string `json:"metadata"` // Additional context and headers }
BaseEventMessage provides a standard implementation of EventMessage[T]. It supports JSON marshaling/unmarshaling and includes common event fields like type, timestamp, payload, and metadata.
This struct can be used directly or embedded in custom event message types for consistent event structure across applications.
func (*BaseEventMessage[T]) GetEventType ¶
func (m *BaseEventMessage[T]) GetEventType() string
GetEventType returns the event type identifier. Used for event routing and handler selection.
func (*BaseEventMessage[T]) GetMetadata ¶
func (m *BaseEventMessage[T]) GetMetadata() map[string]string
GetMetadata returns event metadata and context information. Includes version, source, correlation IDs, and other contextual data.
func (*BaseEventMessage[T]) GetPayload ¶
func (m *BaseEventMessage[T]) GetPayload() T
GetPayload returns the typed event business data. Contains the actual information relevant to the event.
func (*BaseEventMessage[T]) GetTimestamp ¶
func (m *BaseEventMessage[T]) GetTimestamp() time.Time
GetTimestamp returns when the event occurred. Essential for event ordering and temporal processing.
func (*BaseEventMessage[T]) GetVersion ¶
func (m *BaseEventMessage[T]) GetVersion() string
GetVersion extracts the version from metadata using MetadataKeyVersion. Returns empty string if version is not set in metadata.
type EventHandler ¶
type EventHandler[T any] interface { // BeforeHandle performs pre-processing before the main event handling logic. // Used for validation, authentication, enrichment, or preparation tasks. BeforeHandle(ctx context.Context, msg EventMessage[T]) error // AfterHandle performs post-processing after the main event handling logic. // Runs regardless of main processing success/failure, suitable for cleanup, // callbacks, logging, and side effects. AfterHandle(ctx context.Context, msg EventMessage[T], eventResult error) error // UnmarshalEventMessage converts raw event data into a typed EventMessage. // Handles deserialization from various formats (JSON, XML, protobuf, etc.) // and returns strongly-typed event message for processing. UnmarshalEventMessage(data []byte) (EventMessage[T], error) }
EventHandler defines the interface for processing events with lifecycle hooks. Implementations provide custom logic for event unmarshaling, pre-processing, and post-processing while maintaining consistent event handling patterns.
Lifecycle Flow:
- UnmarshalEventMessage() - Convert raw data to typed event message
- BeforeHandle() - Pre-processing and validation
- [External event processing logic]
- AfterHandle() - Post-processing and cleanup
type EventMessage ¶
type EventMessage[T any] interface { // GetVersion returns the event schema version for compatibility checks. // Used to handle different versions of the same event type. GetVersion() string // GetEventType returns the type of event (e.g., "user.created", "order.updated"). // Used for routing and processing logic. GetEventType() string // GetTimestamp returns when the event occurred. // Used for ordering, TTL checks, and temporal processing. GetTimestamp() time.Time // GetPayload returns the typed event data containing business information. // The type T is determined by the EventHandler's generic parameter. GetPayload() T // GetMetadata returns event metadata and context as key-value pairs. // Contains additional information like source, correlation IDs, etc. GetMetadata() map[string]string }
EventMessage defines the interface for accessing event data and metadata. This interface provides a consistent way to retrieve event information regardless of the underlying message format or source (HTTP, message queue, webhook, etc.).
The interface uses generics to provide type-safe access to event payloads while maintaining flexibility for different event types and sources.
func BaseEventMessageUnmarshaller ¶
func BaseEventMessageUnmarshaller[T any](data []byte) (EventMessage[T], error)
BaseEventMessageUnmarshaller unmarshals JSON data into a BaseEventMessage[T]. This is a convenience function for creating BaseEventMessage instances from JSON bytes.
Parameters:
- data: JSON bytes containing event data
Returns:
- EventMessage[T]: Unmarshaled event message
- error: JSON parsing error if data is invalid
Example:
data := []byte(`{"event_type":"user.created","timestamp":"2023-01-01T00:00:00Z","payload":{"id":123},"metadata":{"source":"api"}}`)
msg, err := BaseEventMessageUnmarshaller[User](data)
Directories
¶
| Path | Synopsis |
|---|---|
|
custom_handler
|
|
|
example
|
|
|
gin_callback
command
|
|
|
gin_callback_with_custom_logger
command
|
|
|
Package event_handler_mocks is a generated GoMock package.
|
Package event_handler_mocks is a generated GoMock package. |