# `Macaw.Overlay`
[🔗](https://github.com/dcoai/macaw/blob/v0.6.0/lib/macaw/overlay.ex#L1)

One visitor's changes to one source's rows (spec/13 §3, #453): the rows added and the
rows removed for this visitor only. It is a pure value, held in the visitor's
`%Macaw.State{}` at `:session` scope, and never written to the source: another visitor
sees the source's rows as they are, and by default a reload starts from them again.

**Readers see the source's rows with the overlay applied,** packed and columnar
(`read/4`): each removed row is retracted from the delivery's packed table by its
identity (`Macaw.Data.Table.apply/2`), and the added rows are appended as packed chunks
(`Macaw.Data.Table.append/2`), so the source's rows are never decoded and no map is built
per row of the source. The added rows are held packed, each a chunk of its own, so a read
picks the ones a request's params select by their chunks and copies nothing.

**Every row is judged before it is held** (`add/3`): its fields against the source's
schema (a field it lacks `:unknown_key`, an identity field with no value `:required`, a
value not of its type `{:type, type}`, a category value outside its declared values
`{:not_allowed, value}`), then by the host's row rules (`Macaw.Overlay.Policy`). Each
refusal is a fault at `[:row, field | path]`, so a form shows it at its field.

# `t`

```elixir
@type t() :: %Macaw.Overlay{
  added: [{list(), Macaw.Data.Table.t()}],
  header: [{String.t(), atom()}],
  identity: [String.t(), ...],
  removed: %{required(list()) =&gt; map()},
  schema: Macaw.Data.Schema.t(),
  source: String.t()
}
```

An overlay: `source` the source's name, `schema` its schema, `identity` its identity
fields, `header` its fields as `{name, type}` in name order, the columns every added row
is packed in; `added` the added rows in the order added, each `{key, table}`, its
identity's key and a one-row packed table; `removed` the source's removed rows, each its
identity's key to the identity, a map of field to value.

# `add`

```elixir
@spec add(t(), term(), Macaw.Overlay.Policy.t()) ::
  {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The overlay with `row` added, judged first (spec/13 §3.2): `{:ok, overlay}`, or
`{:error, faults}` with nothing added, each at `[:row, field | path]`.

`row` maps each field to its value, a term of the field's type, as the wire and a form
read it at the edge; a field left out is `nil`. Its faults, at once: a row that isn't a
map, `{[:row], {:type, :map}}`; a field the schema lacks, `:unknown_key`; an identity
field with no value, `:required`; a value not of its type, `{:type, type}`; a category
value outside its declared values, `{:not_allowed, value}`; a row whose identity the
overlay has added already, `{:duplicate, identity}` at `[:row, field]` of its first
identity field; and an overlay holding the policy's `max` changes, `{[:row], {:limit,
:changes, max}}`. A row that passes is judged by the policy's `rules`, whose faults are
read at `[:row | path]`; rules that answer neither `:ok` nor `{:error, faults}` are
`{[:row], {:rules, :answer}}`. A row whose identity a removed row of the source has is
added: it is the visitor's new row, and the source's stays removed.

# `clear`

```elixir
@spec clear(t()) :: t()
```

The overlay with every change taken back: the source's rows as they are.

# `dump`

```elixir
@spec dump(t()) :: %{required(String.t()) =&gt; [%{required(String.t()) =&gt; String.t()}]}
```

The overlay as text, for a carrier that holds text (spec/13 §3.5): a map of `"added"`,
each added row's fields as persistence writes them (spec/03 §3.2), and `"removed"`, each
removed row's identity the same way, both in the order held. `load/3` reads it back.

# `empty?`

```elixir
@spec empty?(t()) :: boolean()
```

Whether the overlay holds no change.

# `load`

```elixir
@spec load(t(), term(), Macaw.Overlay.Policy.t()) ::
  {:ok, t(), [Macaw.Base.Fault.t()]}
```

An overlay read back from `dump/1`'s text: `{:ok, overlay, dropped}`. The text is
untrusted, as a carrier's always is (spec/03 §3.2), so each row is read by its fields'
types and judged as `add/3` judges it, rules and all, and each identity as `remove/3`
does; one that can't be used is dropped, its faults in `dropped` at `[:added, i | path]`
or `[:removed, i | path]`, and the rest are held. Text that isn't `dump/1`'s shape is
`{[], {:type, :overlay}}` in `dropped`, and nothing is held. `overlay` is the empty
overlay it is read into.

# `new`

```elixir
@spec new(String.t(), Macaw.Data.Schema.t()) ::
  {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

An empty overlay of the source `name` of `schema`: `{:ok, overlay}`, or
`{:error, [{[], {:no_identity, name}}]}` for a schema that declares no identity, whose
rows can't be told apart to remove.

    iex> schema = Macaw.Data.Schema.new!(%{"name" => %{type: :text, identity: true}})
    iex> {:ok, overlay} = Macaw.Overlay.new("birds", schema)
    iex> Macaw.Overlay.empty?(overlay)
    true
    iex> Macaw.Overlay.new("birds", Macaw.Data.Schema.new!(%{"v" => %{type: :number}}))
    {:error, [{[], {:no_identity, "birds"}}]}

# `read`

```elixir
@spec read(t(), Macaw.Data.Table.t(), map(), map()) ::
  {:ok, Macaw.Data.Table.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

A delivery's packed table with the overlay applied (spec/13 §3.4): `{:ok, table}`, or
`{:error, faults}`.

Each removed row is retracted by its identity from `table`, and a removed row the table
doesn't hold (one the request's params or window left out) is left as it is. Then each
added row the request's params select is appended: a param `filters` maps to a field
selects the added rows whose field equals its value (each of its values, for a list),
`nil` selecting every row; a param it maps to `nil` selects every row. The rows are
appended after the source's; finishing orders, trims and reduces them with the rest
(spec/06 §3.5). An empty overlay answers `table` as it is.

The table's columns must be the overlay's: a column of another type, or another set of
columns, is `{[:overlay], {:columns, expected, given}}`, each a list of `{name, type}`;
a table of a source's chunks laid by its window (one with an origin) is `{[:overlay],
:origin}`, since the rows the visitor added belong to no slot of the source's.

# `read_row`

```elixir
@spec read_row(t(), term()) :: {:ok, map()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

A row's fields read from text by the source's schema, as a form sends them and
persistence writes them (spec/03 §3.2): `{:ok, row}`, or `{:error, faults}`, each at
`[:row, field]`. The empty string, and a field left out, is `nil`; text not of its
field's type is `{:type, type}`; a field the schema lacks `:unknown_key`. A value that
isn't text is `{:type, :text}`, and text that isn't a map `{[:row], {:type, :map}}`.
Leading and trailing white space is trimmed, as a visitor types it.

# `remove`

```elixir
@spec remove(t(), term(), Macaw.Overlay.Policy.t()) ::
  {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The overlay with the row of `identity` removed (spec/13 §3.3): `{:ok, overlay}`, or
`{:error, faults}` with nothing removed. An added row of that identity is dropped from
the overlay; any other is a source row, retracted from every read.

`identity` is a map of each identity field to its value, or, for a source of one
identity field, the value itself. Its faults: an identity of no field's value,
`{[:identity], :required}` at the field's path `[:identity, field]`; a value not of its
field's type, `{:type, type}` there; and a full overlay, `{[:identity], {:limit,
:changes, max}}`. Removing a row already removed changes nothing.

# `size`

```elixir
@spec size(t()) :: non_neg_integer()
```

How many changes the overlay holds: its added rows and its removed ones.

# `values`

```elixir
@spec values(t(), String.t()) :: MapSet.t()
```

The values the overlay's added rows hold in `field`, each spelled as a legal value is
(a category's atom by its spelling), a set: the values a visitor's own rows add to a
field's legal values (spec/07 §2.2), so a filter offers and takes them for this visitor.
`nil` values are left out.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
