States, snapshots and events carry the evoq contracts (PR 2 of #4) #11

Closed
mercurius wants to merge 0 commits from mercurius/4-pr2-state-events into main
Member

Purpose (feature): phoenix's states, aggregates and events carry the same contracts as mcl-bookclub's: evoq_state, snapshots, and typed events with from_map/1. This is PR 2 of #4 (gap table H.2).

Refs #4

What changes

  • 4 states:
    • @behaviour :evoq_state
    • to_map/1 / from_map/1 (the stream id is required)
    • a typed @type t, @specs, and @moduledoc text instead of @moduledoc false
  • 4 aggregates: snapshot/1 / from_snapshot/1, so evoq now snapshots them (evoq_aggregate.erl:757). Until now every load replayed the whole stream. They also get @specs on every callback.
  • 9 events: from_map/1 with E's required keys, a typed t, and @spec/@doc on every callback.
  • Tests:
    • test/snapshot_test.exs (32): a round trip per state, a snapshot per aggregate, and a load resumed from a snapshot that ends where a full replay ends
    • test/event_map_test.exs (37): a round trip and a missing-key refusal per event

Where it differs from mcl-bookclub, and why

  1. Optional struct fields default to "" and 0, E's record and maps:get defaults, so from_map(%{id => x}) == new(x), as in E. Nothing in phoenix read a nil field. new/1 is unchanged.
  2. @type t, not @opaque: the handlers read state fields directly. Getters are out of scope.
  3. stream_id/1 is kept on the aggregates, with @doc and @spec added.
  4. event_data/1 is unchanged: changing it would change behaviour.
  5. E's one state round-trip test is covered for all 4 states by snapshot_test. E has no event from_map tests, so those are new.

Checks (msi00, ci image)

  • format, compile --warnings-as-errors, credo --strict (633 mods/funs): clean
  • dialyzer: passed, with no new ignore entries
  • mix test: 126/7/11/18/7, 0 failures

10 mutants, all killed (to_map drops, from_map accepting a missing id, snapshot omissions, from_snapshot not restoring a field, event from_map gaps).

Purpose (feature): phoenix's states, aggregates and events carry the same contracts as mcl-bookclub's: `evoq_state`, snapshots, and typed events with `from_map/1`. This is PR 2 of #4 (gap table H.2). Refs #4 ## What changes - **4 states:** - `@behaviour :evoq_state` - `to_map/1` / `from_map/1` (the stream id is required) - a typed `@type t`, `@spec`s, and `@moduledoc` text instead of `@moduledoc false` - **4 aggregates:** `snapshot/1` / `from_snapshot/1`, so evoq now snapshots them (`evoq_aggregate.erl:757`). Until now every load replayed the whole stream. They also get `@spec`s on every callback. - **9 events:** `from_map/1` with E's required keys, a typed `t`, and `@spec`/`@doc` on every callback. - **Tests:** - `test/snapshot_test.exs` (32): a round trip per state, a snapshot per aggregate, and a load resumed from a snapshot that ends where a full replay ends - `test/event_map_test.exs` (37): a round trip and a missing-key refusal per event ## Where it differs from mcl-bookclub, and why 1. Optional struct fields default to `""` and `0`, E's record and `maps:get` defaults, so `from_map(%{id => x}) == new(x)`, as in E. Nothing in phoenix read a nil field. `new/1` is unchanged. 2. `@type t`, not `@opaque`: the handlers read state fields directly. Getters are out of scope. 3. `stream_id/1` is kept on the aggregates, with `@doc` and `@spec` added. 4. `event_data/1` is unchanged: changing it would change behaviour. 5. E's one state round-trip test is covered for all 4 states by snapshot_test. E has no event `from_map` tests, so those are new. ## Checks (msi00, ci image) - format, compile `--warnings-as-errors`, credo `--strict` (633 mods/funs): clean - dialyzer: passed, with no new ignore entries - mix test: 126/7/11/18/7, 0 failures 10 mutants, all killed (to_map drops, from_map accepting a missing id, snapshot omissions, from_snapshot not restoring a field, event from_map gaps).
The 4 state modules declare @behaviour :evoq_state and gain to_map/1
and from_map/1 (id required, every other field defaults to the struct),
typed @type t and @specs. Their struct defaults move to the Erlang
record defaults ("" and 0), so from_map/1 of a bare id is new/1.

The 4 aggregates export snapshot/1 and from_snapshot/1 over that map:
evoq saves a snapshot only when snapshot/1 is exported, so until now
every load replayed the whole stream.

The `#` comment headers become @moduledoc text. test/snapshot_test.exs
round-trips every state the stream passes through, resumes a load from
a snapshot at every point of the history, and pins from_map's refusal.

Mirrors mcl-bookclub (0ce055a) apps/host_bookclub/src/:
bookclub_state.erl, book_state.erl, member_state.erl, reading_state.erl
(to_map/from_map), bookclub_aggregate.erl, book_aggregate.erl,
member_aggregate.erl, reading_aggregate.erl (snapshot/from_snapshot).

Refs macula-services/mcl-bookclub-phoenix#4
The 9 events gain from_map/1 (the optional evoq_event callback), a typed
@type t and @specs on every callback. from_map/1 requires the keys the
Erlang heads require (club_id and name for bookclub_archived_v1, the
stream id for the others) and refuses the rest with
:missing_required_fields; any other absent field takes the struct
default, "" or 0, as the Erlang maps:get defaults do.

The `#` comment headers become @moduledoc text. test/event_map_test.exs
round-trips each event, checks the defaults, and refuses a map without
each required key.

Mirrors mcl-bookclub (0ce055a) apps/host_bookclub/src/*/: from_map/1 in
bookclub_initiated_v1.erl, bookclub_archived_v1.erl, party_planned_v1.erl,
member_registered_v1.erl, member_unregistered_v1.erl, book_procured_v1.erl,
book_retired_v1.erl, reading_started_v1.erl, reading_finished_v1.erl.

Refs macula-services/mcl-bookclub-phoenix#4
Merge remote-tracking branch 'origin/main' into mercurius/4-pr2-state-events
All checks were successful
c4 model / c4-model-1 (pull_request) Successful in 4s
c4 model / c4-model (pull_request) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (pull_request) Successful in 7m5s
c4 model / c4-model-1 (push) Successful in 2s
c4 model / c4-model (push) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (push) Successful in 9m14s
0a964e9ac2
Member

Landed by fast-forward: phoenix main is 0a964e9ac2, this PR's head (PASS r=1 Uranus, CI 6116 + 6117). Closed rather than merged: this repository does not allow the manually-merged style.

Landed by fast-forward: phoenix main is 0a964e9ac201d4e990fbab8497e06908ddb71415, this PR's head (PASS r=1 Uranus, CI 6116 + 6117). Closed rather than merged: this repository does not allow the manually-merged style.
terra closed this pull request 2026-12-01 05:46:59 +00:00
All checks were successful
c4 model / c4-model-1 (pull_request) Successful in 4s
c4 model / c4-model (pull_request) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (pull_request) Successful in 7m5s
c4 model / c4-model-1 (push) Successful in 2s
c4 model / c4-model (push) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (push) Successful in 9m14s

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
macula-services/mcl-bookclub-phoenix!11
No description provided.