# `VRL`
[🔗](https://github.com/Ziinc/vrl-ex/blob/v0.1.0/lib/vrl.ex#L1)

Elixir bindings for [Vector Remap Language](https://vrl.dev).

Compile a program once with `compile/1`, then run it against many events
with `run/3`. The full VRL standard library is available, except for
network functions.

    iex> {:ok, program} = VRL.compile(".message = upcase!(.message)")
    iex> VRL.run(program, %{"message" => "hello"})
    {:ok, %VRL.Result{value: "HELLO", event: %{"message" => "HELLO"}}}

## Value conversion

| Elixir                   | VRL       |
| ------------------------ | --------- |
| binary                   | string    |
| integer (64-bit)         | integer   |
| float                    | float     |
| `true` / `false`         | boolean   |
| `nil`                    | null      |
| map                      | object    |
| list                     | array     |
| `DateTime`               | timestamp |
| other atoms              | string    |

Map keys may be binaries or atoms; returned maps always have binary keys.
Timestamps are returned as UTC `DateTime`s with microsecond precision.
Other structs are passed as maps, keeping `"__struct__"` unless the
`:strip_structs` option is set.

## Integer overflow

VRL integer arithmetic (`+`, `-`, `*`) wraps around on 64-bit overflow
without an error or warning. This is upstream VRL behaviour:

    iex> {:ok, program} = VRL.compile("9223372036854775807 + 1")
    iex> {:ok, %VRL.Result{value: value}} = VRL.run(program)
    iex> value
    -9223372036854775808

# `event`

```elixir
@type event() :: term()
```

# `run_error`

```elixir
@type run_error() :: String.t() | {:abort, String.t()}
```

# `compile`

```elixir
@spec compile(String.t()) :: {:ok, VRL.Program.t()} | {:error, String.t()}
```

Compiles VRL `source` into a reusable `VRL.Program`.

Returns `{:error, diagnostics}` with human-readable diagnostics when the
program is invalid.

# `max_depth`

```elixir
@spec max_depth() :: pos_integer()
```

Maximum nesting of maps and lists accepted by and returned from `run/3`.

Deeper values return `{:error, "maximum nesting depth of 128 exceeded"}`.

# `run`

```elixir
@spec run(VRL.Program.t(), event(), keyword()) ::
  {:ok, VRL.Result.t()} | {:error, run_error() | :timeout}
```

Runs a compiled `program` against `event`.

Returns `{:ok, %VRL.Result{}}` with the program's resulting value and the
transformed event. Returns `{:error, {:abort, message}}` when the program
calls `abort`, and `{:error, :timeout}` when it exceeds the timeout.

## Options

  * `:timeout` - maximum run time in milliseconds, or `:infinity`.
    Defaults to `5000`. Checked between VRL expressions, so a single
    long-running function call can overrun it.
  * `:strip_structs` - when `true`, structs other than `DateTime` are
    passed as plain maps without the `"__struct__"` key. Defaults to `false`.

---

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