fix: set flow argument defaults

This commit is contained in:
Zach Daniel 2023-04-27 13:42:18 -04:00
parent fbc341b3a0
commit fbd2b2f50c
3 changed files with 31 additions and 8 deletions

View file

@ -408,7 +408,7 @@ defmodule Ash.DataLayer.Mnesia do
@doc false
@impl true
def transaction(_, func, _timeout, _reason) do
case Mnesia.transaction(func) do
case Mnesia.transaction(func, 1) do
{:atomic, result} ->
{:ok, result}

View file

@ -168,12 +168,37 @@ defmodule Ash.Flow do
defp cast_input(flow, params) do
arguments = Ash.Flow.Info.arguments(flow)
Enum.reduce_while(params, {:ok, %{}}, fn {name, value}, {:ok, acc} ->
case Enum.find(arguments, &(&1.name == name || to_string(&1.name) == name)) do
nil ->
{:cont, {:ok, acc}}
Enum.reduce_while(arguments, {:ok, %{}}, fn arg, {:ok, acc} ->
value =
case Map.fetch(params, arg.name) do
:error ->
Map.fetch(params, to_string(arg.name))
arg ->
other ->
other
end
case value do
:error ->
if not is_nil(arg.default) do
value =
case arg.default do
{m, f, a} ->
apply(m, f, a)
fun when is_function(fun, 0) ->
fun.()
value ->
value
end
{:cont, {:ok, Map.put(acc, arg.name, value)}}
else
{:cont, {:ok, acc}}
end
{:ok, value} ->
with {:ok, value} <-
Ash.Type.Helpers.cast_input(arg.type, value, arg.constraints, flow),
{:constrained, {:ok, casted}}

View file

@ -4,9 +4,7 @@ defmodule Ash.Test.Flow.Flows.Halting do
flow do
argument :on_step, :atom do
allow_nil? false
constraints one_of: [:a, :b, :c]
default :a
end
returns :c