# `Alternera.Plugin.Inputs`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_plugin_sdk/lib/alternera/plugin/inputs.ex#L1)

What a `call/3` step may read.

It wraps the exchange's view (`Plugin.Location`) with the step's
`Plugin.Signature`. `read/2` reads a location, `resolve/2` the template at
a settings path (`resolve_each/2` each of its references on its own), and `with_write/3` gives back inputs whose later reads see
a value the step writes, so a step whose rows build on each other (Set)
sees its own earlier rows. That is the step's scratch, and a step may
return it as its result (`{:ok, inputs}` or `{:ok, inputs, summary}`):
its writes are then the ones `with_write/3` made, in order, and the
runner takes the scratch's view as the exchange after the step instead
of writing them again (`Plugin.CallAdapter`). A step that returns
`{:ok, writes}` instead has the runner write those, and the scratch is
thrown away.

The runner trusts the scratch's view, so the scratch holds variables to
the tenant's size cap itself: the host puts the cap in the view
(`limits`), and `with_write/3` refuses a variable above it as the
runner's own write does. Over a view that carries no cap (a plugin's own
tests, a step that reads nothing and gets no view) the scratch holds no
variable to a size, and the runner, which then writes the step's writes
itself, does. The type is opaque: a plugin reads and writes its inputs
through these functions only, never by changing the struct, since the
runner adopts the view without checking it again.

`resolve/2` takes the template's settings path (`at`), not its text: the
signature holds the text and its `secrecy`, and a `:plain` template
refuses a secret reference (`Plugin.Template`'s `allow_secret?: false`).
An `at` the signature does not hold raises, in every mode: nothing
resolves that the signature has not declared, so a secret cannot reach a
template that was never marked.

`check_writes/2` is the boundary for what `call/3` returns, run on every
result in every mode (`Plugin.CallAdapter`): each write's kind must be
one the signature writes, and its location a real one (a part entry,
`%{in: :vars, name: :any}`, is a declaration, never a target). The
recording's secret masking relies on the parts a step declares, so a
write outside them fails the step.

In strict mode (`config :alternera_plugin_sdk, :strict_inputs, true`, which
the proxy's test suite sets) a read, a scratch write or a returned write
the signature does not declare by its exact location (or within a part
entry of its kind) raises: a plugin's own tests then show a signature that
under-declares; a `reads_any` step's other reads go through, but not a
body it neither names nor was handed as its phase's own. Outside it the exact check costs nothing, but a
body is another matter in every mode: the host's view carries only the
bodies the signature reads or writes, besides the phase's own
(`Signature.bodies/1`, `Location.only_bodies/2`), so reading another
finds nothing.

# `t`

```elixir
@opaque t()
```

# `check_writes`

```elixir
@spec check_writes(t() | Alternera.Plugin.Signature.t(), term()) ::
  :ok | {:error, String.t()}
```

Checks the writes a step returned against its signature, in every mode:
`:ok`, or `{:error, message}` for a list that is not `{location, value}`
pairs, a location that is not one (a part entry), or a write of a kind the
signature does not write. Strict mode raises besides for a location the
signature does not declare exactly or within a part entry.

# `edit_json`

```elixir
@spec edit_json(t(), Alternera.Plugin.Location.t(), (Alternera.Plugin.JsonDoc.t() -&gt;
                                                 {Alternera.Plugin.JsonDoc.t(),
                                                  acc})) ::
  {:ok, t(), acc} | {:error, String.t()}
when acc: term()
```

Edits the whole body at `location` through its JSON document, as a
write of it (the signature must write it): `fun` gets the document
(`Location.doc_to_edit/3`: the one the steps before kept, one over the
body's file for a body held in one, never read whole) and answers
`{doc, acc}`, the document it leaves and anything of its own. A
document other than the one it got goes in the scratch as the body
(`Location.edited/3`), logged as a write; the same one writes nothing.
`{:ok, inputs, acc}`, or `{:error, reason}` when the body is not there,
is empty or is not JSON, the inputs as they were. (A path write of
`Location.write/3` takes an empty body as `{}`; an edit of the whole
body does not.)

# `held`

```elixir
@spec held(t(), Alternera.Plugin.Location.t(), non_neg_integer(), (-&gt; result)) ::
  {:ok, result} | {:error, Alternera.Plugin.Location.held_error()}
when result: term()
```

`fun` run as work on `bytes` of the body at `location` that the runner
does not account for, claimed and heap-bounded (`Location.held/4`): a
`json_doc:` step's output built from a body it was handed unread.

# `json_doc`

```elixir
@spec json_doc(t(), Alternera.Plugin.Location.t()) ::
  {:ok, Alternera.Plugin.JsonDoc.t()} | {:error, String.t()}
```

The JSON document of the whole body at `location`, read as the step
reads that body (the signature must read it): the one the steps before
kept, or one built for it, over the body's file for a body held in one,
never read whole (`Location.doc_to_edit/3`). `{:error, reason}` when the
body is not there, is empty or is not JSON.

# `kept_doc`

```elixir
@spec kept_doc(t(), Alternera.Plugin.Location.t(), term()) ::
  Alternera.Plugin.JsonDoc.verdict() | nil
```

The JSON document the view keeps for `value`, read from the whole body
at `location`, its text in memory, or the verdict that the body is not
JSON (`Location.kept_doc/3`); nil for any other location, or when the
view keeps neither for these bytes. A step that edits the body through
`JsonDoc` starts from it instead of scanning the body again, and notes
what it leaves (`note_doc/3`).

# `new`

```elixir
@spec new(map() | nil, Alternera.Plugin.Signature.t()) :: t()
```

Inputs over `view` (nil reads as an empty exchange). A view's `limits`
(the tenant's variable cap, which the host puts there) hold the
scratch's variables to it; without them the scratch holds none.

# `note_doc`

```elixir
@spec note_doc(t(), Alternera.Plugin.Location.t(), Alternera.Plugin.JsonDoc.verdict()) ::
  :ok
```

Notes `doc` (or a not-JSON verdict) as the document of the whole body at
`location` (`Location.note_doc/3`), for the steps after this one to
read through when the body the step leaves is the one it was built
over. Ignored for any other location.

# `read`

```elixir
@spec read(t(), Alternera.Plugin.Location.t()) ::
  {:ok, term()}
  | :missing
  | {:error, String.t() | {:claim_refused, :busy | :size, String.t()}}
```

Reads `location` (`Location.read/2`).

# `resolve`

```elixir
@spec resolve(t(), [String.t() | non_neg_integer()]) :: String.t()
```

Resolves the template the step's settings hold at `at` (`["values", 0,
"value"]`, as `Plugin.Signature` records it) against the exchange
(`Template.resolve/3`), refusing a secret reference when the template is
`:plain`. Raises `ArgumentError` for an `at` that holds no template.

# `resolve_each`

```elixir
@spec resolve_each(t(), [String.t() | non_neg_integer()]) :: [
  {:text, String.t()} | {:reference, String.t(), String.t()}
]
```

The template at `at`, as `resolve/2` finds it, with each reference
resolved on its own: a list of `{:text, text}` for the text around them
and `{:reference, reference, value}` for each `${…}` and tenant variable
(`{{NAME}}`, `{{secret.NAME}}`), in order. A step that writes the values
into something with a grammar of its own encodes each one (Request's URL,
whose values are percent-encoded so they stay data). Joining the texts
and values gives what `resolve/2` gives when no value holds a reference
of its own.

# `view`

```elixir
@spec view(t()) :: map()
```

The exchange's view as a whole, for a step whose signature `reads_any`
(`Plugin.Signature`): a script whose text reaches the exchange as
tables. Raises `ArgumentError` for any other step, in every mode: a step
that names its reads reads them with `read/2`.

# `with_text`

```elixir
@spec with_text(t(), Alternera.Plugin.Location.t(), (binary() -&gt; result)) ::
  {:ok, result} | :missing | {:error, Alternera.Plugin.Location.held_error()}
when result: term()
```

`fun` on the whole body at `location` as text, the read and `fun`
claimed and bounded for a body the runner handed unread
(`Location.with_text/3`): what a step that reads its body through its
document (`json_doc:`) does where it needs the text after all.

# `with_write`

```elixir
@spec with_write(t(), Alternera.Plugin.Location.t(), term()) ::
  {:ok, t()} | {:error, term()}
```

Inputs whose reads see `value` at `location`, the write logged as one of
the step's (`scratch/1`); `{:error, reason}` when the view refuses the
write, which is not logged. A `nil` value writes nothing and is not
logged, as the runner skips a returned `{location, nil}` write.

---

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