Skip to content

Commit

Permalink
Fix docs around run (#131)
Browse files Browse the repository at this point in the history
* docs: Correct usage of anon run function

The docs incorrectly suggested and showed usages of `run` with arity 3.
But `run` only allows for arity 1 and 2. This patch updates the docs to
show the correct usage and labeling for the function.

Fixes #128

* chore: Fix missing argument test

This test was warning when run. It unfortunately was getting a false
positive as written due to calling `.name` on a atom rather than getting
the `:name` property of the `:current_step` map.

This change fixes the test and looks for the correct error message.
  • Loading branch information
ekosz authored Oct 3, 2024
1 parent 3391135 commit 192920a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
14 changes: 7 additions & 7 deletions documentation/dsls/DSL:-Reactor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ map :double_numbers do
step :double do
argument :number, element(:double_numbers)
run fn %{number: number}, _, _ ->
run fn %{number: number}, _ ->
{:ok, number * 2}
end
end
Expand All @@ -1068,7 +1068,7 @@ end

```
step :get_subscriptions do
run fn _, _, _ ->
run fn _, _ ->
Stripe.Subscription.list()
end
end
Expand All @@ -1079,7 +1079,7 @@ map :cancel_subscriptions do
step :cancel do
argument :sub_id, element(:cancel_subscriptions, [:id])
run fn args, _, _ ->
run fn args, _ ->
Stripe.Subscription.cancel(arg.sub_id, %{prorate: true, invoice_now: true})
end
end
Expand Down Expand Up @@ -1272,10 +1272,10 @@ end

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`run`](#reactor-step-run){: #reactor-step-run } | `(any -> any) \| mfa \| (any, any -> any) \| mfa` | | Provide an anonymous function which implements the `run/3` callback. Cannot be provided at the same time as the `impl` argument. |
| [`undo`](#reactor-step-undo){: #reactor-step-undo } | `(any -> any) \| mfa \| (any, any -> any) \| mfa \| (any, any, any -> any) \| mfa` | | Provide an anonymous function which implements the `undo/4` callback. Cannot be provided at the same time as the `impl` argument. |
| [`compensate`](#reactor-step-compensate){: #reactor-step-compensate } | `(any -> any) \| mfa \| (any, any -> any) \| mfa \| (any, any, any -> any) \| mfa` | | Provide an anonymous function which implements the `undo/4` callback. Cannot be provided at the same time as the `impl` argument. |
| [`max_retries`](#reactor-step-max_retries){: #reactor-step-max_retries } | `:infinity \| non_neg_integer` | `:infinity` | The maximum number of times that the step can be retried before failing. Only used when the result of the `compensate/4` callback is `:retry`. |
| [`run`](#reactor-step-run){: #reactor-step-run } | `(any -> any) \| mfa \| (any, any -> any) \| mfa` | | Provide an anonymous function which implements a `run/1-2` callback. Cannot be provided at the same time as the `impl` argument. |
| [`undo`](#reactor-step-undo){: #reactor-step-undo } | `(any -> any) \| mfa \| (any, any -> any) \| mfa \| (any, any, any -> any) \| mfa` | | Provide an anonymous function which implements a `undo/1-3` callback. Cannot be provided at the same time as the `impl` argument. |
| [`compensate`](#reactor-step-compensate){: #reactor-step-compensate } | `(any -> any) \| mfa \| (any, any -> any) \| mfa \| (any, any, any -> any) \| mfa` | | Provide an anonymous function which implements a `compensate/1-3` callback. Cannot be provided at the same time as the `impl` argument. |
| [`max_retries`](#reactor-step-max_retries){: #reactor-step-max_retries } | `:infinity \| non_neg_integer` | `:infinity` | The maximum number of times that the step can be retried before failing. Only used when the result of the `compensate` callback is `:retry`. |
| [`async?`](#reactor-step-async?){: #reactor-step-async? } | `boolean` | `true` | When set to true the step will be executed asynchronously via Reactor's `TaskSupervisor`. |
| [`transform`](#reactor-step-transform){: #reactor-step-transform } | `(any -> any) \| module \| nil` | | An optional transformation function which can be used to modify the entire argument map before it is passed to the step. |

Expand Down
6 changes: 3 additions & 3 deletions lib/reactor/dsl/argument.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ defmodule Reactor.Dsl.Argument do
use Reactor
step :whom do
run fn ->
run fn _, _ ->
{:ok, Enum.random(["Marty", "Doc", "Jennifer", "Lorraine", "George", nil])}
end
end
Expand Down Expand Up @@ -107,7 +107,7 @@ defmodule Reactor.Dsl.Argument do
# here: -------↓↓↓↓↓
argument :rhs, value(3)
run fn args, _, _ ->
run fn args, _ ->
{:ok, args.lhs * args.rhs}
end
end
Expand All @@ -134,7 +134,7 @@ defmodule Reactor.Dsl.Argument do
step :double do
argument :number, element(:double_numbers)
run fn args, _, _ ->
run fn args, _ ->
{:ok, args.number * 2}
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/reactor/dsl/map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ defmodule Reactor.Dsl.Map do
step :double do
argument :number, element(:double_numbers)
run fn %{number: number}, _, _ ->
run fn %{number: number}, _ ->
{:ok, number * 2}
end
end
end
""",
"""
step :get_subscriptions do
run fn _, _, _ ->
run fn _, _ ->
Stripe.Subscription.list()
end
end
Expand All @@ -81,7 +81,7 @@ defmodule Reactor.Dsl.Map do
step :cancel do
argument :sub_id, element(:cancel_subscriptions, [:id])
run fn args, _, _ ->
run fn args, _ ->
Stripe.Subscription.cancel(arg.sub_id, %{prorate: true, invoice_now: true})
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/reactor/dsl/step.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,29 @@ defmodule Reactor.Dsl.Step do
type: {:or, [{:mfa_or_fun, 1}, {:mfa_or_fun, 2}]},
required: false,
doc: """
Provide an anonymous function which implements the `run/3` callback. Cannot be provided at the same time as the `impl` argument.
Provide an anonymous function which implements a `run/1-2` callback. Cannot be provided at the same time as the `impl` argument.
"""
],
undo: [
type: {:or, [{:mfa_or_fun, 1}, {:mfa_or_fun, 2}, {:mfa_or_fun, 3}]},
required: false,
doc: """
Provide an anonymous function which implements the `undo/4` callback. Cannot be provided at the same time as the `impl` argument.
Provide an anonymous function which implements a `undo/1-3` callback. Cannot be provided at the same time as the `impl` argument.
"""
],
compensate: [
type: {:or, [{:mfa_or_fun, 1}, {:mfa_or_fun, 2}, {:mfa_or_fun, 3}]},
required: false,
doc: """
Provide an anonymous function which implements the `undo/4` callback. Cannot be provided at the same time as the `impl` argument.
Provide an anonymous function which implements a `compensate/1-3` callback. Cannot be provided at the same time as the `impl` argument.
"""
],
max_retries: [
type: {:or, [{:in, [:infinity]}, :non_neg_integer]},
required: false,
default: :infinity,
doc: """
The maximum number of times that the step can be retried before failing. Only used when the result of the `compensate/4` callback is `:retry`.
The maximum number of times that the step can be retried before failing. Only used when the result of the `compensate` callback is `:retry`.
"""
],
async?: [
Expand Down
2 changes: 1 addition & 1 deletion test/reactor/step/anon_fn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Reactor.Step.AnonFnTest do
assert Spark.implements_behaviour?(Reactor.Step.AnonFn, Reactor.Step)
end

describe "run/3" do
describe "run/2" do
test "it can handle 2 arity anonymous functions" do
fun = fn arguments, _ ->
arguments.first_name
Expand Down
4 changes: 2 additions & 2 deletions test/reactor/step/transform_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ defmodule Reactor.Step.TransformTest do

describe "run/3" do
test "when the value argument is missing" do
assert {:error, error} = run(%{}, %{current_step: :current_step}, [])
assert Exception.message(error) =~ "argument is missing"
assert {:error, error} = run(%{}, %{current_step: %{name: :current_step}}, [])
assert Exception.message(error) =~ "Missing Argument Error"
end

test "it applies the transform" do
Expand Down

0 comments on commit 192920a

Please sign in to comment.