Skip to content

Commit 05dc7cd

Browse files
authored
fix: support private_arguments in code interface and bulk actions (#2133)
1 parent 6f9983a commit 05dc7cd

File tree

9 files changed

+268
-11
lines changed

9 files changed

+268
-11
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ shell.nix
3636

3737
.vscode
3838
.elixir-tools
39+
40+
# Personal notes and planning documents
41+
notes/

lib/ash.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,12 @@ defmodule Ash do
404404
type: :map,
405405
doc: "Context to set on each changeset"
406406
],
407+
private_arguments: [
408+
type: :map,
409+
default: %{},
410+
doc:
411+
"Private argument values to set on each changeset before validations and changes are run."
412+
],
407413
sorted?: [
408414
type: :boolean,
409415
default: false,

lib/ash/actions/create/bulk.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ defmodule Ash.Actions.Create.Bulk do
393393
|> Ash.Actions.Helpers.add_context(opts)
394394
|> Ash.Changeset.set_context(opts[:context] || %{})
395395
|> Ash.Changeset.prepare_changeset_for_action(action, opts)
396+
|> Ash.Changeset.set_private_arguments_for_action(opts[:private_arguments] || %{})
396397
|> then(fn changeset ->
397398
if opts[:after_action] do
398399
Ash.Changeset.after_action(changeset, opts[:after_action])
@@ -706,6 +707,7 @@ defmodule Ash.Actions.Create.Bulk do
706707
) do
707708
base
708709
|> Ash.Changeset.put_context(:bulk_create, %{index: index})
710+
|> Ash.Changeset.set_private_arguments_for_action(opts[:private_arguments] || %{})
709711
|> handle_params(
710712
Keyword.get(opts, :assume_casted?, false),
711713
action,

lib/ash/actions/destroy/bulk.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,7 @@ defmodule Ash.Actions.Destroy.Bulk do
12491249
|> Ash.Actions.Helpers.add_context(opts)
12501250
|> Ash.Changeset.set_context(opts[:context] || %{})
12511251
|> Ash.Changeset.prepare_changeset_for_action(action, opts)
1252+
|> Ash.Changeset.set_private_arguments_for_action(opts[:private_arguments] || %{})
12521253
|> Ash.Changeset.set_arguments(arguments)
12531254
|> then(fn changeset ->
12541255
changeset =
@@ -1669,6 +1670,7 @@ defmodule Ash.Actions.Destroy.Bulk do
16691670
|> Ash.Changeset.new()
16701671
|> Map.put(:domain, domain)
16711672
|> Ash.Changeset.prepare_changeset_for_action(action, opts)
1673+
|> Ash.Changeset.set_private_arguments_for_action(opts[:private_arguments] || %{})
16721674
|> Ash.Changeset.put_context(:bulk_destroy, %{index: index})
16731675
|> Ash.Changeset.set_context(opts[:context] || %{})
16741676
|> handle_params(

lib/ash/actions/update/bulk.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,7 @@ defmodule Ash.Actions.Update.Bulk do
16101610
|> Ash.Actions.Helpers.add_context(opts)
16111611
|> Ash.Changeset.set_context(opts[:context] || %{})
16121612
|> Ash.Changeset.prepare_changeset_for_action(action, opts)
1613+
|> Ash.Changeset.set_private_arguments_for_action(opts[:private_arguments] || %{})
16131614
|> Ash.Changeset.set_arguments(arguments)
16141615
|> then(fn changeset ->
16151616
changeset =
@@ -1976,6 +1977,7 @@ defmodule Ash.Actions.Update.Bulk do
19761977
|> Ash.Changeset.new()
19771978
|> Map.put(:domain, domain)
19781979
|> Ash.Changeset.prepare_changeset_for_action(action, opts)
1980+
|> Ash.Changeset.set_private_arguments_for_action(opts[:private_arguments] || %{})
19791981
|> Ash.Changeset.put_context(context_key, %{index: index})
19801982
|> Ash.Changeset.set_context(opts[:context] || %{})
19811983
|> Ash.Changeset.atomic_update(opts[:atomic_update] || [])

lib/ash/changeset/changeset.ex

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ defmodule Ash.Changeset do
813813
|> Map.put(:no_atomic_constraints, opts[:no_atomic_constraints] || [])
814814
|> Map.put(:action_type, action.type)
815815
|> Map.put(:atomics, opts[:atomics] || [])
816+
|> set_private_arguments_for_action(opts[:private_arguments] || %{})
816817
|> Ash.Changeset.set_tenant(opts[:tenant])
817818

818819
{changeset, _opts} =
@@ -2044,13 +2045,9 @@ defmodule Ash.Changeset do
20442045

20452046
Ash.Tracer.set_metadata(opts[:tracer], :changeset, metadata)
20462047

2047-
changeset =
2048-
Enum.reduce(opts[:private_arguments] || %{}, changeset, fn {k, v}, changeset ->
2049-
set_private_argument_for_action(changeset, k, v)
2050-
end)
2051-
20522048
changeset
20532049
|> Map.put(:action, action)
2050+
|> set_private_arguments_for_action(opts[:private_arguments] || %{})
20542051
|> handle_errors(action.error_handler)
20552052
|> set_actor(opts)
20562053
|> set_authorize(opts)
@@ -5876,6 +5873,13 @@ defmodule Ash.Changeset do
58765873
)
58775874
end
58785875

5876+
@doc false
5877+
def set_private_arguments_for_action(changeset, arguments) do
5878+
Enum.reduce(arguments, changeset, fn {k, v}, changeset ->
5879+
set_private_argument_for_action(changeset, k, v)
5880+
end)
5881+
end
5882+
58795883
defp set_private_argument_for_action(changeset, argument, value) do
58805884
do_set_private_argument(
58815885
changeset,

lib/ash/code_interface.ex

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,15 @@ defmodule Ash.CodeInterface do
716716
resolve_subject =
717717
quote do
718718
{input_opts, opts} =
719-
Keyword.split(opts, [:input, :actor, :tenant, :authorize?, :tracer, :scope])
719+
Keyword.split(opts, [
720+
:input,
721+
:actor,
722+
:tenant,
723+
:authorize?,
724+
:tracer,
725+
:scope,
726+
:private_arguments
727+
])
720728

721729
{input, input_opts} = Keyword.pop(input_opts, :input)
722730

@@ -880,7 +888,8 @@ defmodule Ash.CodeInterface do
880888
:authorize?,
881889
:tracer,
882890
:context,
883-
:skip_unknown_inputs
891+
:skip_unknown_inputs,
892+
:private_arguments
884893
])
885894

886895
changeset_opts = Keyword.put(changeset_opts, :domain, unquote(domain))
@@ -993,7 +1002,8 @@ defmodule Ash.CodeInterface do
9931002
:authorize?,
9941003
:tracer,
9951004
:context,
996-
:skip_unknown_inputs
1005+
:skip_unknown_inputs,
1006+
:private_arguments
9971007
])
9981008

9991009
changeset_opts = Keyword.put(changeset_opts, :domain, unquote(domain))
@@ -1053,7 +1063,8 @@ defmodule Ash.CodeInterface do
10531063
:scope,
10541064
:tracer,
10551065
:context,
1056-
:skip_unknown_inputs
1066+
:skip_unknown_inputs,
1067+
:private_arguments
10571068
])
10581069

10591070
changeset_opts = Keyword.put(changeset_opts, :domain, unquote(domain))
@@ -1261,7 +1272,8 @@ defmodule Ash.CodeInterface do
12611272
:authorize?,
12621273
:tracer,
12631274
:context,
1264-
:skip_unknown_inputs
1275+
:skip_unknown_inputs,
1276+
:private_arguments
12651277
])
12661278

12671279
changeset_opts = Keyword.put(changeset_opts, :domain, unquote(domain))
@@ -1321,7 +1333,8 @@ defmodule Ash.CodeInterface do
13211333
:authorize?,
13221334
:tracer,
13231335
:context,
1324-
:skip_unknown_inputs
1336+
:skip_unknown_inputs,
1337+
:private_arguments
13251338
])
13261339

13271340
changeset_opts = Keyword.put(changeset_opts, :domain, unquote(domain))

lib/ash/resource/interface.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ defmodule Ash.Resource.Interface do
6969
doc:
7070
"Options passed to `Ash.bulk_create`, if a list or stream of inputs is provided.",
7171
keys: Keyword.drop(Ash.bulk_create_opts(), Keyword.keys(Ash.create_opts()))
72+
],
73+
private_arguments: [
74+
type: :map,
75+
doc: "Private argument values to set before validations and changes.",
76+
default: %{}
7277
]
7378
)
7479
|> Keyword.drop([:domain])
@@ -86,6 +91,11 @@ defmodule Ash.Resource.Interface do
8691
"Options passed to `Ash.bulk_update`, if a query, list, or stream of inputs is provided.",
8792
keys:
8893
Keyword.drop(Ash.bulk_update_opts(), Keyword.keys(Ash.update_opts()) ++ [:resource])
94+
],
95+
private_arguments: [
96+
type: :map,
97+
doc: "Private argument values to set before validations and changes.",
98+
default: %{}
8999
]
90100
)
91101
|> Keyword.drop([:domain])
@@ -106,6 +116,11 @@ defmodule Ash.Resource.Interface do
106116
Ash.bulk_destroy_opts(),
107117
Keyword.keys(Ash.destroy_opts()) ++ [:resource]
108118
)
119+
],
120+
private_arguments: [
121+
type: :map,
122+
doc: "Private argument values to set before validations and changes.",
123+
default: %{}
109124
]
110125
)
111126
|> Keyword.drop([:domain])
@@ -166,6 +181,13 @@ defmodule Ash.Resource.Interface do
166181
use Spark.Options.Validator,
167182
schema:
168183
Ash.run_action_opts()
184+
|> Keyword.merge(
185+
private_arguments: [
186+
type: :map,
187+
doc: "Private argument values to set before validations and changes.",
188+
default: %{}
189+
]
190+
)
169191
|> Keyword.drop([:domain])
170192
end
171193

0 commit comments

Comments
 (0)