LemonCore.Events.Payload (lemon_core v0.1.0)

View Source

Shared scaffolding for typed LemonCore.Bus payloads.

A payload module declares its event type and fields; this macro supplies the struct, the strict constructor publishers use, the lenient constructor consumers use on legacy maps, and — for one deprecation cycle — an Access implementation.

defmodule LemonCore.Events.SecretChanged do
  use LemonCore.Events.Payload,
    type: :secret_changed,
    enforce: [:owner, :name, :action],
    fields: [owner: nil, name: nil, action: nil]
end

Why Access

Payloads were free-form maps until Phase 3.1, and consumers read them with payload[:key]. Access is not implemented for structs, so without this shim every consumer of a topic would have to change in the same commit as its publisher. The shim lets publishers migrate one at a time. It is temporary: see docs/platform/bus-events.md §4.3.

Callbacks each payload module gets

  • event_type/0 — the atom used as LemonCore.Event.type
  • event_version/0 — bumped only on a breaking field change
  • new/1 — strict: raises on unknown keys or missing enforced keys. For publishers.
  • from_map/1 — lenient: drops unknown keys, accepts string keys, passes structs through. For consumers reading a payload that may predate the struct.