View Source Reactor.Dsl.Argument (reactor v0.9.1)

The struct used to store argument DSL entities.

See Reactor.step.argument.

Summary

Functions

The element template helper for the Reactor DSL.

The input template helper for the Reactor DSL.

The result template helper for the Reactor DSL.

The value template helper for the Reactor DSL.

Types

@type t() :: %Reactor.Dsl.Argument{
  __identifier__: any(),
  name: atom(),
  source: Reactor.Template.t(),
  transform: nil | (any() -> any()) | {module(), keyword()} | mfa()
}

Functions

Link to this function

element(name, sub_path \\ [])

View Source
@spec element(any(), [any()]) :: Reactor.Template.Element.t()

The element template helper for the Reactor DSL.

Example

defmodule ExampleReactor do
  use Reactor

  input :numbers

  map :double_numbers do
    source input(:numbers)

    step :double do
      argument :number, element(:double_numbers)

      run fn args, _, _ ->
        {:ok, args.number * 2}
      end
    end

    return :double
  end
end
Link to this function

input(input_name, sub_path \\ [])

View Source
@spec input(atom(), [any()]) :: Reactor.Template.Input.t()

The input template helper for the Reactor DSL.

Example

defmodule ExampleReactor do
  use Reactor

  input :name

  step :greet do
    # here: --------↓↓↓↓↓
    argument :name, input(:name)
    run fn
      %{name: nil}, _, _ -> {:ok, "Hello, World!"}
      %{name: name}, _, _ -> {:ok, "Hello, #{name}!"}
    end
  end
end

Extracting nested values

You can provide a list of keys to extract from a data structure, similar to Kernel.get_in/2 with the condition that the input value is either a struct or implements the Access protocol.

Link to this function

result(step_name, sub_path \\ [])

View Source
@spec result(atom(), [any()]) :: Reactor.Template.Result.t()

The result template helper for the Reactor DSL.

Example

defmodule ExampleReactor do
  use Reactor

  step :whom do
    run fn ->
      {:ok, Enum.random(["Marty", "Doc", "Jennifer", "Lorraine", "George", nil])}
    end
  end

  step :greet do
    # here: --------↓↓↓↓↓↓
    argument :name, result(:whom)
    run fn
      %{name: nil}, _, _ -> {:ok, "Hello, World!"}
      %{name: name}, _, _ -> {:ok, "Hello, #{name}!"}
    end
  end
end

Extracting nested values

You can provide a list of keys to extract from a data structure, similar to Kernel.get_in/2 with the condition that the result is either a struct or implements the Access protocol.

@spec value(any()) :: Reactor.Template.Value.t()

The value template helper for the Reactor DSL.

Example

defmodule ExampleReactor do
  use Reactor

  input :number

  step :times_three do
    argument :lhs, input(:number)
    # here: -------↓↓↓↓↓
    argument :rhs, value(3)

    run fn args, _, _ ->
      {:ok, args.lhs * args.rhs}
    end
  end
end