event

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package event defines the transport-agnostic message envelope exchanged between services. It knows nothing about Kafka: a broker client is responsible for mapping an Event onto whatever its transport calls a topic, a key and a value.

Index

Constants

View Source
const TopicOrderCreated = "orders.created"

TopicOrderCreated carries an event per completed purchase order.

Variables

View Source
var ErrMalformedEvent = errors.New("malformed event")

ErrMalformedEvent is returned when an Event is missing a required field.

Functions

This section is empty.

Types

type Event

type Event struct {
	// Topic names the stream this event belongs to. Required.
	Topic string `json:"topic"`

	// Key determines ordering. Events sharing a key are delivered to consumers
	// in publish order; events with different keys have no relative ordering
	// guarantee, because a broker is free to place them on separate partitions.
	// The key is therefore a domain decision — it declares what must stay
	// ordered with respect to what — and is required for that reason.
	Key string `json:"key"`

	// ID uniquely identifies this event and is the basis for consumer-side
	// deduplication. Delivery is at-least-once, so a consumer may see the same
	// ID more than once and must treat a repeat as a no-op.
	//
	// It is generated once, when the event is created, and must survive a
	// republish unchanged — an ID minted at publish time would differ on every
	// retry and defeat deduplication entirely.
	ID string `json:"id"`

	// OccurredAt is when the event happened, which is not necessarily when it
	// was published or consumed.
	OccurredAt time.Time `json:"occurred_at"`

	// Data is the domain payload, JSON-encoded on the wire. On a consumed event
	// it holds the raw JSON; use DecodeData to unmarshal it into a concrete type.
	Data any `json:"data"`
}

Event is a single message.

Topic and Key are routing metadata and are carried by the transport rather than the payload; ID, OccurredAt and Data are the payload and travel together as the encoded value. See Encode for the wire format.

func Decode

func Decode(topic, key string, value []byte) (*Event, error)

Decode reconstructs an Event from a transport's topic, key and value. The resulting Data holds raw JSON — call DecodeData to interpret it.

func New

func New(topic, key string, data any) *Event

New builds an Event with a freshly generated ID and the current time.

func (*Event) DecodeData

func (e *Event) DecodeData(v any) error

DecodeData unmarshals the event payload into v.

func (*Event) Encode

func (e *Event) Encode() ([]byte, error)

Encode serializes the event's payload for transmission.

func (*Event) Validate

func (e *Event) Validate() error

Validate reports whether the event carries the fields a transport needs.

Jump to

Keyboard shortcuts

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