events

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

tundra-events

A typed event schema — a generic, transport-independent Envelope wrapping typed payloads.

What

Model an event as an Envelope (id, type, source, time, raw-JSON data) carrying a typed Payload. Build one with New(payload, opts…), serialize with Bytes, Parse raw bytes back, and Decode into the concrete payload type registered for its Type. A built-in SecretEvent covers the secret.* types; consumers register their own via Register. It is a pure, generic event-schema primitive: it models any source (Event Center, message bus, webhook) and names no particular project.

Why

Every event-consuming service otherwise hand-rolls its own JSON event model with bespoke reconnect/filter logic and no shared shape. tundra-events is the one shared envelope so producers and consumers agree on the wire format, and the id field doubles as the idempotency key for at-least-once delivery.

Install

go get github.com/pleme-io/tundra-events

Nix (via substrate):

outputs = { self, nixpkgs, substrate, ... }:
  (import substrate.goLibraryFlakeBuilder { inherit nixpkgs; }) {
    name = "tundra-events"; version = "0.1.0"; src = self;
  };

Usage

Built on: errors-go (typed, code-carrying errors).

env, err := events.New(
    events.SecretEvent{Kind: events.SecretUpdated, Path: "/db/prod", Version: 3},
    events.WithID("evt-42"), events.WithSource("event-center"),
)
if err != nil { return errs.Exit(err) }

b, _ := env.Bytes()         // publish over AMQP/HTTP/NATS

parsed, _ := events.Parse(b) // consumer side
payload, err := events.Decode(parsed)
se := payload.(*events.SecretEvent)

Register a custom event type:

events.Register("custom.thing", func() events.Payload { return &MyPayload{} })

Configuration

None — it is a pure library. A consumer that loads broker/topic settings from a configured source uses shikumi-go for that config; tundra-events only models the event itself.

Release

Pull-model (Go modules): an annotated vX.Y.Z tag is the release; proxy.golang.org + pkg.go.dev index it. See the GSDS module delivery FSM.

Documentation

Overview

Package events is the fleet's one typed event schema — a generic Envelope wrapping a typed payload — so no event-consuming tool hand-rolls a bespoke JSON event model with ad-hoc reconnect/filter logic. The Envelope is the transport-independent shape; payloads are typed and registered by event type.

It is a pure, generic event-schema primitive: it models any event source (an Event Center, a message bus, a webhook) and never names or imports any particular project.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(t Type, factory func() Payload)

Register associates an event Type with a factory for its payload type. It is the seam consumers use to add their own typed events. Built-in secret.* types are registered in init.

Types

type Envelope

type Envelope struct {
	// ID is a unique event id (idempotency key for at-least-once delivery).
	ID string `json:"id"`
	// Type selects the payload shape.
	Type Type `json:"type"`
	// Source names the emitter (a URL/hostname/logical name).
	Source string `json:"source"`
	// Time is when the event occurred (UTC).
	Time time.Time `json:"time"`
	// Data is the typed payload, carried as raw JSON.
	Data json.RawMessage `json:"data"`
}

Envelope is the generic, transport-independent event wrapper. It carries metadata (id, type, source, time) plus the typed payload as raw JSON so it can be decoded into the registered concrete payload type.

func New

func New(payload Payload, opts ...Option) (*Envelope, error)

New builds an Envelope around a typed payload. The Envelope's Type is derived from the payload (payload.EventType), and Data is the payload marshalled to JSON. A nil payload, or a JSON-unmarshalable payload, yields a typed error.

func Parse

func Parse(b []byte) (*Envelope, error)

Parse decodes raw bytes into an Envelope (without decoding the payload). Use Decode to obtain the typed payload.

func (*Envelope) Bytes

func (e *Envelope) Bytes() ([]byte, error)

MarshalJSON / UnmarshalJSON use the default struct tags — Envelope is a plain JSON document, so the transport (AMQP, HTTP, NATS) just carries bytes.

func (*Envelope) Validate

func (e *Envelope) Validate() error

Validate reports the first structural problem with the envelope as a typed, code-carrying error. A valid envelope has an id and a registered type.

type Option

type Option func(*Envelope)

Option configures an Envelope at construction.

func WithID

func WithID(id string) Option

WithID sets the event id.

func WithSource

func WithSource(src string) Option

WithSource sets the emitter source.

func WithTime

func WithTime(t time.Time) Option

WithTime sets the event time (defaults to time.Now().UTC() in New).

type Payload

type Payload interface {
	// EventType returns the Type this payload is the body of.
	EventType() Type
}

Payload is the marker interface every typed event payload satisfies. A payload reports the Type it belongs to so an Envelope can be built and validated from the payload alone.

func Decode

func Decode(e *Envelope) (Payload, error)

Decode validates the envelope and decodes its Data into the registered concrete payload type for e.Type.

type SecretEvent

type SecretEvent struct {
	// Kind is the specific secret event type (created/updated/deleted).
	Kind Type `json:"kind"`
	// Path is the logical secret path the event concerns.
	Path string `json:"path"`
	// Version is the new version after the change, if known.
	Version int `json:"version,omitempty"`
	// Tags are arbitrary labels attached to the secret.
	Tags []string `json:"tags,omitempty"`
}

SecretEvent is the typed payload for the secret.* event types — the generic shape a secret-lifecycle source emits. It carries no vendor-specific fields.

func (SecretEvent) EventType

func (e SecretEvent) EventType() Type

EventType returns the secret event's Kind, satisfying Payload.

type Type

type Type string

Type identifies the kind of event an Envelope carries; it selects the payload shape. New types are additive.

const (
	// SecretCreated signals a new secret was created at Payload.Path.
	SecretCreated Type = "secret.created"
	// SecretUpdated signals a secret's value rotated/changed at Payload.Path.
	SecretUpdated Type = "secret.updated"
	// SecretDeleted signals a secret was removed at Payload.Path.
	SecretDeleted Type = "secret.deleted"
)

Built-in event types. Consumers may register their own via Register.

Jump to

Keyboard shortcuts

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