Packages
reactor
0.8.0
1.0.2
1.0.1
1.0.0
0.17.0
0.16.0
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.1
0.9.0
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
An asynchronous, graph-based execution engine
Current section
Files
Jump to
Current section
Files
lib/reactor/step/transform.ex
defmodule Reactor.Step.Transform do
@moduledoc """
The built-in step for executing input and argument transformations.
Expects a single argument named `value` which contains the value to be
transformed.
## Options
* `fun` - a one or two arity function or MFA to use to modify the `value`
argument.
> #### Tip {: .tip}
>
> This step is emitted by the builder when an argument needs to be transformed
> before being passed into a step.
>
> Most likely you will never need to use this step directly.
"""
alias Reactor.{Error.Invalid.MissingArgumentError, Error.Invalid.TransformError, Step}
use Step
@doc false
@impl true
@spec run(Reactor.inputs(), Reactor.context(), keyword) :: {:ok | :error, any}
def run(arguments, context, options) do
case Map.fetch(arguments, :value) do
{:ok, value} ->
do_transform(value, options)
:error ->
{:error,
MissingArgumentError.exception(
step: context.current_step,
argument: :value,
arguments: arguments
)}
end
end
defp do_transform(value, opts) do
case Keyword.pop(opts, :fun) do
{fun, _opts} when is_function(fun, 1) ->
{:ok, fun.(value)}
{fun, opts} when is_function(fun, 2) ->
{:ok, fun.(value, opts)}
{{m, f, a}, _opts} when is_atom(m) and is_atom(f) and is_list(a) ->
{:ok, apply(m, f, [value | a])}
end
rescue
error -> {:error, TransformError.exception(input: value, error: error)}
end
end