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

A step's To list: which of the values its plugin registers
(`Alternera.Plugin`'s `outputs/0`) go where.

Settings carry `"targets"`, a list of rows:

    [%{"output" => "status", "to" => %{"in" => "vars", "name" => "order_status"}},
     %{"output" => "headers", "key" => "etag",
       "to" => %{"in" => "vars", "name" => "order_etag"}}]

A row names one registered output, a `key` when the output is keyed
(one named value of a set, a header) and the location it is written
to (`Plugin.Location`). A step that carries a single `"target"`, the
shape every step had before, has one row of the plugin's default
output; a step carries one or the other, never both.

`parse/3` checks a list against the registration and the step's
direction:

  * every row names a registered output; a key only on a keyed one;
  * the location fits the output's kind: a status goes to a variable
    or the response status, one header to a variable or one header of
    the step's side, the whole headers to a variable (a head's headers
    are never written at once, `Location.allowed/3`), and a header only
    to a whole variable, never a path inside one (its masking marks the
    variable, `Pipelines.StepVars`); a body or any other value goes
    where the direction allows (`Location.allowed/3`, checked for every
    row);
  * one write per body and per variable: at most one row to the whole
    of it, and rows into the same one name paths neither of which leads
    into the other (`a` and `b`, never `a` and `a.b`), and never the
    whole of it;
  * no two rows to the same header or the response status.

A refusal names the row: `To row 2 (headers · etag): …`, which a
plugin returns after its name; `row_error/1` reads that back, for an
editor that shows it at the row.

`writes/2` turns the rows and the values one run produced into the
`[{location, value}]` the runner writes (`Pipelines.Runner`), in row
order; a keyed row whose key the values lack, or a nil value, writes
nothing. `selected/2` is what the same rows pick, as a map, for a
failed step's error (`Runner.Error`'s `outputs`).

# `row`

```elixir
@type row() :: %{
  output: String.t(),
  key: String.t() | nil,
  kind: :body | :status | :headers | :value,
  to: Alternera.Plugin.Location.t()
}
```

# `default_output`

```elixir
@spec default_output([Alternera.Plugin.output()]) :: Alternera.Plugin.output() | nil
```

The output a single `"target"` writes, or nil.

# `parse`

```elixir
@spec parse(map(), [Alternera.Plugin.output()], :request | :response, keyword()) ::
  {:ok, [row()]} | {:error, String.t()}
```

The rows of `settings` for a step whose plugin registers `outputs`,
running in `direction`. Options: `:default`, the location a step with
neither `"targets"` nor `"target"` writes its default output to (nil:
no row). `{:ok, rows}` or `{:error, message}`.

# `pick`

```elixir
@spec pick(row(), map()) :: term()
```

What one row picks from `values` (`writes/2`), or nil.

# `row_error`

```elixir
@spec row_error(term()) :: {non_neg_integer(), String.t()} | nil
```

The row a save error is about and the reason, or nil when it names no
row (`Plugin.RowError.parse/2` with the label `To row`). A refusal of
one row reads `To row N: reason`, or `To row N (output): reason` /
`To row N (output · key): reason` once the row names its output, rows
counted from 1, after at most one `<Plugin name>: ` (`Store: To row 2
(body): …`). The row comes back counted from 0, the reason as written.

    iex> Alternera.Plugin.Targets.row_error("Store: To row 2 (headers · etag): writes where row 1 writes")
    {1, "writes where row 1 writes"}

    iex> Alternera.Plugin.Targets.row_error("Store: this step takes one To row")
    nil

# `schema_field`

```elixir
@spec schema_field([Alternera.Plugin.output()], String.t() | nil) ::
  Alternera.Plugin.schema_field()
```

The settings field a plugin's `settings_schema/0` lists for its To list,
given what it registers. `extra` is a sentence of the plugin's own
rules, or nil.

# `selected`

```elixir
@spec selected([row()], map()) :: %{required(String.t()) =&gt; term()}
```

What the rows pick from `values`, keyed by the variable a row writes,
else by its output (`output.key` for a keyed row).

# `single_target`

```elixir
@spec single_target(map(), [Alternera.Plugin.output()]) ::
  {:ok, map()} | {:error, String.t()}
```

For a plugin that writes one value to one place (Transform, Script):
`settings` with a one-row `"targets"` of the default output read back
as the `"target"` it means, so the plugin's own target checks run on
it. A list of more than one row, a row of another output or with a
key, or `"targets"` beside `"target"`, is refused.

# `writes`

```elixir
@spec writes([row()], map()) :: [{Alternera.Plugin.Location.t(), term()}]
```

The writes the rows make from `values`, a map of output name to the
value one run produced (a keyed output's value is a map of key to
value), in row order.

---

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