@@ -15,11 +15,12 @@ defmodule AshRandomParams.Transformer do
1515 init_params = Builder . build_action_argument ( :init_params , :map , default: % { } )
1616 fill = Builder . build_action_argument ( :fill , { :array , :atom } , default: [ ] )
1717 unfill = Builder . build_action_argument ( :unfill , { :array , :atom } , default: [ ] )
18+ keep_defaults? = Builder . build_action_argument ( :keep_defaults? , :boolean , default: true )
1819
1920 dsl_state
2021 |> Builder . add_action ( :action , :random_params ,
2122 returns: :map ,
22- arguments: [ action , init_params , unfill , fill ] ,
23+ arguments: [ action , init_params , unfill , fill , keep_defaults? ] ,
2324 run: & __MODULE__ . do_random_params / 2
2425 )
2526 |> Builder . add_interface ( :random_params , args: [ :action , { :optional , :init_params } ] )
@@ -44,15 +45,13 @@ defmodule AshRandomParams.Transformer do
4445 action: action ,
4546 init_params: init_params ,
4647 unfill: unfill ,
47- fill: fill
48+ fill: fill ,
49+ keep_defaults?: keep_defaults?
4850 }
4951 } =
5052 input ,
5153 ctx
5254 ) do
53- { random_mod , random_opts } =
54- random = AshRandomParams.Info . random_params_random! ( input . resource )
55-
5655 action = Ash.Resource.Info . action ( resource , action )
5756 init_keys = init_params |> Map . keys ( )
5857
@@ -75,21 +74,41 @@ defmodule AshRandomParams.Transformer do
7574
7675 candidates = accepted_attrs ++ action . arguments
7776
78- placeholder =
79- candidates
80- |> then ( fn attr_or_args ->
81- if action . type == :create do
82- attr_or_args
83- else
84- attr_or_args
85- |> Enum . reject ( & ( & 1 . name in rel_names ) )
86- |> Enum . reject ( & ( & 1 . name in belongs_to_attrs ) )
87- end
88- end )
89- |> Map . new ( & { & 1 . name , nil } )
77+ defaults =
78+ if keep_defaults? do
79+ candidates
80+ |> then ( fn attr_or_args ->
81+ if action . type == :create do
82+ attr_or_args
83+ else
84+ attr_or_args
85+ |> Enum . reject ( & ( & 1 . name in rel_names ) )
86+ |> Enum . reject ( & ( & 1 . name in belongs_to_attrs ) )
87+ end
88+ end )
89+ |> Enum . reject ( & ( & 1 . name in unfill ) )
90+ |> Enum . reject ( & ( & 1 . name in init_keys ) )
91+ |> Map . new ( fn % { name: name , default: default } ->
92+ value =
93+ case default do
94+ default when is_function ( default , 0 ) ->
95+ default . ( )
96+
97+ default ->
98+ default
99+ end
100+
101+ { name , value }
102+ end )
103+ else
104+ % { }
105+ end
90106
91107 generated_params =
92108 (
109+ { random_mod , random_opts } =
110+ random = AshRandomParams.Info . random_params_random! ( input . resource )
111+
93112 fields_by_unfill =
94113 candidates
95114 |> Enum . reject ( & ( & 1 . name in rel_names ) )
@@ -118,7 +137,7 @@ defmodule AshRandomParams.Transformer do
118137 end )
119138 )
120139
121- result_params = placeholder |> Map . merge ( init_params ) |> Map . merge ( generated_params )
140+ result_params = defaults |> Map . merge ( init_params ) |> Map . merge ( generated_params )
122141
123142 { :ok , result_params }
124143 end
0 commit comments