# `AshBackendDrivenOptimisticUi.Compiler`
[🔗](https://github.com/Undeadlol1/ash_backend_driven_optimistic_ui/blob/v0.1.0/lib/ash_backend_driven_optimistic_ui/compiler.ex#L1)

Compiles an unhydrated `Ash.Expr` term to JSON Logic, as plain maps and
lists ready for `Jason`.

Variables: an attribute, calculation or aggregate reads `record.<name>`, an
argument reads `input.<name>`, a context value reads `ctx.<name>` (a context
path is dotted, `ctx.copy.title`), `event(:key)` reads `event.<key>`, the
payload of the pushed event a reaction answers.

Standard operators: `+ - * /` (and unary `-`), `== != < > <= >=`, `cat`
(from `<>`, nested ones flattened), `in`, `and`, `or`, `!` (from `not`), `if`; `is_nil(x)` becomes
`x == nil`; `has_arg(:name)` becomes `has`, true when the input carries the
key, even with a null value, so a rule can leave alone a field the caller
did not send.

Every compared value that is not a number literal is cast to a number with
`+`, because Ash sends decimals as text and standard JSON Logic would
compare two such texts as text (`"10.5" >= "9"` is false). Every joined
value that is not a text literal is wrapped in `to_string`, because
standard `cat` prints an empty value as `null` and an Ash developer expects
nothing.

Custom operators the client's evaluator implements on top of JSON Logic:

  * `abs` — `abs(x)`
  * `round` — `round(x)` and `round(x, places)`
  * `to_string` — `to_string(x)`
  * `now` — `now()`, the current UTC time as ISO 8601 text
  * `has` — `{"has": "input.name"}`, whether the path's last key is present

Decimals become floats. Anything else, including references through a
relationship and the actor, is `{:error, {:unsupported, node}}`.

# `json_logic`

```elixir
@type json_logic() ::
  number()
  | String.t()
  | boolean()
  | nil
  | [json_logic()]
  | %{required(String.t()) =&gt; json_logic()}
```

# `compile`

```elixir
@spec compile(term()) :: {:ok, json_logic()} | {:error, {:unsupported, term()}}
```

Compiles `expression`, or names the first node the client cannot run.

---

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