events

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package events is the generic event contract: the Event interface, the metadata every event carries, and a Registry a consumer fills with its own event catalog to parse payloads.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event interface {
	// Type returns the event's type discriminator.
	Type() EventType

	// Metadata returns the common metadata shared by all events.
	Metadata() EventMetadata
}

Event is the interface all events implement.

type EventMetadata

type EventMetadata struct {
	// EventID is the unique identifier of the event.
	EventID string `json:"event_id"`

	// EventType is the type discriminator for the event.
	EventType EventType `json:"event_type"`

	// OccurredAt is the timestamp when the event occurred.
	OccurredAt time.Time `json:"occurred_at"`

	// TenantID is the tenant the event belongs to.
	TenantID string `json:"tenant_id"`

	// UserID is the user who triggered the event, if applicable.
	UserID string `json:"user_id,omitempty"`

	// CorrelationID links related events across services.
	CorrelationID string `json:"correlation_id,omitempty"`

	// TraceParent carries the W3C trace context (traceparent header) captured when
	// the event was produced, so a consumer can continue the originating trace —
	// e.g. request → outbox → relay publish → consumer — as one distributed
	// trace. Empty when no active trace context was present.
	TraceParent string `json:"traceparent,omitempty"`
}

EventMetadata holds common fields for all events.

type EventType

type EventType string

EventType is an event's type discriminator (e.g. "widget.created"); the consumer defines its own values.

type Factory

type Factory func() Event

Factory returns a new, zero-valued event of one type, for Registry.Parse to decode a payload into. It must return a non-nil pointer.

type Registry

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

Registry maps event types to factories, so a consumer parses its own event catalog: register each type once at the composition root, then Parse payloads. EventMetadata is the envelope every event shares; its event_type field selects the factory. The zero value is an empty Registry ready to use (NewRegistry is equivalent), and a Registry is safe for concurrent use.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func (*Registry) Parse

func (r *Registry) Parse(data []byte) (Event, error)

Parse decodes a JSON event payload into the event registered for its event_type. A malformed payload, a missing event_type, an unregistered type or a body that does not decode into the event returns a CodeInvalidInput error. The payload is decoded twice (once to read the type, once into the concrete event); a consumer whose catalog is fixed and hot can keep a single-pass parser of its own.

func (*Registry) Register

func (r *Registry) Register(eventType EventType, factory Factory) error

Register adds the factory for eventType. It returns a CodeInvalidInput error, registering nothing, for an empty eventType, a type registered twice, a nil factory, or a factory that does not return a non-nil pointer (Parse decodes into the factory's value, so anything else would fail every message of that type).

Jump to

Keyboard shortcuts

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