Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"backoff",
"casted",
"Desugars",
"indentify",
"lvalue",
"mappish",
"noreply",
Expand Down
23 changes: 12 additions & 11 deletions documentation/dsls/DSL-Reactor.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ argument :three, value(3)

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`description`](#reactor-around-argument-description){: #reactor-around-argument-description } | `String.t` | | An optional description for the argument. |
| [`description`](#reactor-around-argument-description){: #reactor-around-argument-description } | `String.t \| nil` | | An optional description for the argument. |
| [`transform`](#reactor-around-argument-transform){: #reactor-around-argument-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the argument before it is passed to the step. |


Expand Down Expand Up @@ -470,7 +470,7 @@ argument :three, value(3)

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`description`](#reactor-collect-argument-description){: #reactor-collect-argument-description } | `String.t` | | An optional description for the argument. |
| [`description`](#reactor-collect-argument-description){: #reactor-collect-argument-description } | `String.t \| nil` | | An optional description for the argument. |
| [`transform`](#reactor-collect-argument-transform){: #reactor-collect-argument-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the argument before it is passed to the step. |


Expand Down Expand Up @@ -718,7 +718,7 @@ argument :three, value(3)

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`description`](#reactor-compose-argument-description){: #reactor-compose-argument-description } | `String.t` | | An optional description for the argument. |
| [`description`](#reactor-compose-argument-description){: #reactor-compose-argument-description } | `String.t \| nil` | | An optional description for the argument. |
| [`transform`](#reactor-compose-argument-transform){: #reactor-compose-argument-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the argument before it is passed to the step. |


Expand Down Expand Up @@ -907,6 +907,7 @@ end
| Name | Type | Default | Docs |
|------|------|---------|------|
| [`level`](#reactor-debug-level){: #reactor-debug-level } | `:emergency \| :alert \| :critical \| :error \| :warning \| :notice \| :info \| :debug` | `:debug` | The log level to send the debug information to. |
| [`description`](#reactor-debug-description){: #reactor-debug-description } | `String.t` | | An optional description for the step. |


### reactor.debug.argument
Expand Down Expand Up @@ -970,7 +971,7 @@ argument :three, value(3)

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`description`](#reactor-debug-argument-description){: #reactor-debug-argument-description } | `String.t` | | An optional description for the argument. |
| [`description`](#reactor-debug-argument-description){: #reactor-debug-argument-description } | `String.t \| nil` | | An optional description for the argument. |
| [`transform`](#reactor-debug-argument-transform){: #reactor-debug-argument-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the argument before it is passed to the step. |


Expand Down Expand Up @@ -1224,7 +1225,7 @@ argument :three, value(3)

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`description`](#reactor-flunk-argument-description){: #reactor-flunk-argument-description } | `String.t` | | An optional description for the argument. |
| [`description`](#reactor-flunk-argument-description){: #reactor-flunk-argument-description } | `String.t \| nil` | | An optional description for the argument. |
| [`transform`](#reactor-flunk-argument-transform){: #reactor-flunk-argument-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the argument before it is passed to the step. |


Expand Down Expand Up @@ -1471,7 +1472,7 @@ argument :three, value(3)

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`description`](#reactor-group-argument-description){: #reactor-group-argument-description } | `String.t` | | An optional description for the argument. |
| [`description`](#reactor-group-argument-description){: #reactor-group-argument-description } | `String.t \| nil` | | An optional description for the argument. |
| [`transform`](#reactor-group-argument-transform){: #reactor-group-argument-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the argument before it is passed to the step. |


Expand Down Expand Up @@ -1711,7 +1712,7 @@ is implemented.
### Examples
```
map :double_numbers do
input input(:numbers)
source input(:numbers)

step :double do
argument :number, element(:double_numbers)
Expand All @@ -1732,7 +1733,7 @@ step :get_subscriptions do
end

map :cancel_subscriptions do
input result(:get_subscriptions)
source result(:get_subscriptions)

step :cancel do
argument :sub_id, element(:cancel_subscriptions, [:id])
Expand Down Expand Up @@ -1827,7 +1828,7 @@ argument :three, value(3)

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`description`](#reactor-map-argument-description){: #reactor-map-argument-description } | `String.t` | | An optional description for the argument. |
| [`description`](#reactor-map-argument-description){: #reactor-map-argument-description } | `String.t \| nil` | | An optional description for the argument. |
| [`transform`](#reactor-map-argument-transform){: #reactor-map-argument-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the argument before it is passed to the step. |


Expand Down Expand Up @@ -2104,7 +2105,7 @@ argument :three, value(3)

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`description`](#reactor-step-argument-description){: #reactor-step-argument-description } | `String.t` | | An optional description for the argument. |
| [`description`](#reactor-step-argument-description){: #reactor-step-argument-description } | `String.t \| nil` | | An optional description for the argument. |
| [`transform`](#reactor-step-argument-transform){: #reactor-step-argument-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the argument before it is passed to the step. |


Expand Down Expand Up @@ -2459,7 +2460,7 @@ argument :three, value(3)

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`description`](#reactor-template-argument-description){: #reactor-template-argument-description } | `String.t` | | An optional description for the argument. |
| [`description`](#reactor-template-argument-description){: #reactor-template-argument-description } | `String.t \| nil` | | An optional description for the argument. |
| [`transform`](#reactor-template-argument-transform){: #reactor-template-argument-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the argument before it is passed to the step. |


Expand Down
2 changes: 2 additions & 0 deletions lib/reactor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule Reactor do

defstruct context: %{},
id: nil,
input_descriptions: %{},
inputs: [],
intermediate_results: %{},
middleware: [],
Expand Down Expand Up @@ -119,6 +120,7 @@ defmodule Reactor do
@type t :: %Reactor{
context: context,
id: any,
input_descriptions: %{atom => String.t()},
inputs: [atom],
intermediate_results: %{any => any},
middleware: [Reactor.Middleware.t()],
Expand Down
131 changes: 104 additions & 27 deletions lib/reactor/argument.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,47 @@ defmodule Reactor.Argument do
A step argument.
"""

defstruct name: nil, source: nil, transform: nil
defstruct description: nil, name: nil, source: nil, transform: nil

alias Reactor.{Argument, Template}
import Reactor.Template, only: :macros

@options Spark.Options.new!(
transform: [
type:
{:or,
[
nil,
{:mfa_or_fun, 1},
{:tuple, [:module, :non_empty_keyword_list]}
]},
required: false,
default: nil,
doc: """
An optional transformation function which can be used to modify the argument before it is passed to the step.
"""
],
description: [
type: {:or, [nil, :string]},
required: false,
default: nil,
doc: """
An optional description for the argument.
"""
]
)

@type transform :: nil | (any -> any) | {module, keyword} | mfa

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

@type options :: [{:description, nil | String.t()} | {:transform, transform}]

defguardp is_spark_fun_behaviour(fun)
when tuple_size(fun) == 2 and is_atom(elem(fun, 0)) and is_list(elem(fun, 1))

Expand All @@ -24,21 +54,34 @@ defmodule Reactor.Argument do
defguardp is_transform(fun)
when is_function(fun, 1) or is_spark_fun_behaviour(fun) or is_mfa(fun)

defguardp maybe_transform(fun) when is_nil(fun) or is_transform(fun)
defguardp maybe_options(options)
when is_nil(options) or is_list(options) or is_transform(options)

@doc """
Build an argument which refers to a reactor input with an optional
transformation applied.

## Options

#{Spark.Options.docs(@options)}

## Example

iex> Argument.from_input(:argument_name, :input_name, &String.to_integer/1)
iex> Argument.from_input(:argument_name, :input_name, transform: &String.to_integer/1)

"""
@spec from_input(atom, atom, nil | (any -> any)) :: Argument.t()
def from_input(name, input_name, transform \\ nil)
when is_atom(name) and maybe_transform(transform),
do: %Argument{name: name, source: %Template.Input{name: input_name}, transform: transform}
@spec from_input(atom, atom, nil | (any -> any) | options) :: Argument.t()
def from_input(name, input_name, options \\ nil)
when is_atom(name) and maybe_options(options) do
options = validate_options!(options)

%Argument{
description: options[:description],
name: name,
source: %Template.Input{name: input_name},
transform: options[:transform]
}
end

@doc """
Build an argument which refers to the result of another step with an optional
Expand All @@ -49,10 +92,18 @@ defmodule Reactor.Argument do
iex> Argument.from_result(:argument_name, :step_name, &Atom.to_string/1)

"""
@spec from_result(atom, any, nil | (any -> any)) :: Argument.t()
def from_result(name, result_name, transform \\ nil)
when is_atom(name) and maybe_transform(transform),
do: %Argument{name: name, source: %Template.Result{name: result_name}, transform: transform}
@spec from_result(atom, any, nil | (any -> any) | options) :: Argument.t()
def from_result(name, result_name, options \\ nil)
when is_atom(name) and maybe_options(options) do
options = validate_options!(options)

%Argument{
description: options[:description],
name: name,
source: %Template.Result{name: result_name},
transform: options[:transform]
}
end

@doc """
Build an argument which refers to a statically defined value.
Expand All @@ -61,9 +112,17 @@ defmodule Reactor.Argument do

iex> Argument.from_value(:argument_name, 10)
"""
@spec from_value(atom, any, nil | (any -> any)) :: Argument.t()
def from_value(name, value, transform \\ nil) when is_atom(name) and maybe_transform(transform),
do: %Argument{name: name, source: %Template.Value{value: value}, transform: transform}
@spec from_value(atom, any, nil | (any -> any) | options) :: Argument.t()
def from_value(name, value, options \\ nil) when is_atom(name) and maybe_options(options) do
options = validate_options!(options)

%Argument{
description: options[:description],
name: name,
source: %Template.Value{value: value},
transform: options[:transform]
}
end

@doc """
Build an argument which refers to to an element within a map step with an optional transformation applied.
Expand All @@ -73,14 +132,18 @@ defmodule Reactor.Argument do
iex> Argument.from_element(:argument_name, &Atom.to_string/1)

"""
@spec from_element(any, any, nil | (any -> any)) :: Argument.t()
def from_element(name, element_name, transform \\ nil)
when maybe_transform(transform),
do: %Argument{
name: name,
source: %Template.Element{name: element_name},
transform: transform
}
@spec from_element(any, any, nil | (any -> any) | options) :: Argument.t()
def from_element(name, element_name, options \\ nil)
when maybe_options(options) do
options = validate_options!(options)

%Argument{
description: options[:description],
name: name,
source: %Template.Element{name: element_name},
transform: options[:transform]
}
end

@doc """
Build an argument directly from a template.
Expand All @@ -90,10 +153,18 @@ defmodule Reactor.Argument do
iex> Argument.from_template(:argument_name, Reactor.Dsl.Argument.input(:input_name))

"""
@spec from_template(atom, Template.t(), nil | (any -> any)) :: Argument.t()
def from_template(name, template, transform \\ nil)
when is_atom(name) and is_template(template) and maybe_transform(transform),
do: %Argument{name: name, source: template, transform: transform}
@spec from_template(atom, Template.t(), nil | (any -> any) | options) :: Argument.t()
def from_template(name, template, options \\ nil)
when is_atom(name) and is_template(template) and maybe_options(options) do
options = validate_options!(options)

%Argument{
description: options[:description],
name: name,
source: template,
transform: options[:transform]
}
end

@doc """
Set a sub-path on the argument.
Expand Down Expand Up @@ -147,4 +218,10 @@ defmodule Reactor.Argument do
"""
defguard has_sub_path(argument)
when is_list(argument.source.sub_path) and argument.source.sub_path != []

defp validate_options!(options) when is_list(options),
do: Spark.Options.validate!(options, @options)

defp validate_options!(transform),
do: validate_options!(transform: transform)
end
22 changes: 11 additions & 11 deletions lib/reactor/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ defmodule Reactor.Builder do
@type async? :: {:async?, boolean | (keyword -> boolean)}

@typedoc "An optional step description"
@type description :: nil | String.t()
@type description :: {:description, nil | String.t()}

@typedoc "How many times is the step allowed to retry?"
@type max_retries :: {:max_retries, :infinity | non_neg_integer()}

@typedoc "Optionally transform all the arguments into new arguments"
@type arguments_transform ::
{:transform,
nil | (%{optional(atom) => any} -> %{optional(atom) => any}) | {module | keyword} | mfa}
nil | (%{optional(atom) => any} -> %{optional(atom) => any}) | {module, keyword} | mfa}

@type ref :: {:ref, :step_name | :make_ref}
@type guards :: {:guards, [Reactor.Guard.Build.t()]}
Expand All @@ -65,23 +65,23 @@ defmodule Reactor.Builder do
This both places the input in the Reactor for later input validation and adds
steps to the Reactor which will emit and (possibly) transform the input.
"""
@spec add_input(Reactor.t(), any, nil | (any -> any)) :: {:ok, Reactor.t()} | {:error, any}
def add_input(reactor, name, transform \\ nil)
@spec add_input(Reactor.t(), any, Builder.Input.options()) :: {:ok, Reactor.t()} | {:error, any}
def add_input(reactor, name, options \\ nil)

def add_input(reactor, _name, _transform) when not is_reactor(reactor),
def add_input(reactor, _name, _options) when not is_reactor(reactor),
do: {:error, argument_error(:reactor, "not a Reactor", reactor)}

def add_input(reactor, name, transform),
do: Builder.Input.add_input(reactor, name, transform)
def add_input(reactor, name, options),
do: Builder.Input.add_input(reactor, name, options)

@doc """
Raising version of `add_input/2..3`.
"""
@spec add_input!(Reactor.t(), any, nil | (any -> any)) :: Reactor.t() | no_return
def add_input!(reactor, name, transform \\ nil)
@spec add_input!(Reactor.t(), any, Builder.Input.options()) :: Reactor.t() | no_return
def add_input!(reactor, name, options \\ nil)

def add_input!(reactor, name, transform) do
case add_input(reactor, name, transform) do
def add_input!(reactor, name, options) do
case add_input(reactor, name, options) do
{:ok, reactor} -> reactor
{:error, reason} -> raise reason
end
Expand Down
Loading