Hourglass.Schema (hourglass v0.1.0)

Copy Markdown View Source

use Hourglass.Schema defines a typed Hourglass payload.

It is the default Hourglass.Codec implementation: an Ecto embedded_schema with a generated all-fields changeset/2, cast/1 + dump/1.

defmodule MyApp.Agent.Goal do
  use Hourglass.Schema

  embedded_schema do
    field :text, :string
    field :turns, :integer
  end
end

cast/1 accepts string- or atom-keyed maps (the JSON-wire shape) and returns {:ok, struct} | {:error, %Ecto.Changeset{}}. Non-map input returns {:error, changeset} instead of raising. Override changeset/2 to add validations (e.g. validate_required/2); the default casts all scalar fields and all embeds.

dump/1 returns a JSON-encodable map via Ecto.embedded_dump/2, which recurses through all embeds and invokes each embed type's dump (including polymorphic_embed's type dump that injects the __type__ discriminator).