Skip to content

Commit 5555a69

Browse files
committed
Remove tagged_unions
1 parent ae71995 commit 5555a69

File tree

2 files changed

+2
-63
lines changed

2 files changed

+2
-63
lines changed

lib/random/default_random.ex

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,6 @@ defmodule AshRandomParams.DefaultRandom do
1515
end)
1616
end
1717

18-
# Union
19-
def random(
20-
%{type: _union_type, constraints: [{:types, type_infos} | _]},
21-
_opts,
22-
%{action_context: action_context} = context
23-
) do
24-
ash_opts = action_context |> Ash.Context.to_opts()
25-
26-
{_subtype_tag, type_info} =
27-
case context do
28-
%{subtype_tag: subtype_tag} ->
29-
type_infos
30-
|> Enum.find(fn
31-
{^subtype_tag, _type_info} -> true
32-
{_, _} -> false
33-
end)
34-
35-
%{} ->
36-
type_infos |> Enum.random()
37-
end
38-
39-
subtype = type_info |> Keyword.fetch!(:type)
40-
tag = type_info |> Keyword.fetch!(:tag)
41-
tag_value = type_info |> Keyword.fetch!(:tag_value)
42-
attr_names = subtype |> Ash.Resource.Info.attributes() |> Enum.map(& &1.name)
43-
44-
# primary_create = subtype |> Ash.Resource.Info.primary_action(:create)
45-
46-
subtype
47-
|> Ash.Changeset.for_create(:fac_rec, %{}, ash_opts)
48-
|> Ash.create!(ash_opts)
49-
|> Map.from_struct()
50-
|> Map.take(attr_names)
51-
|> Map.put(tag, tag_value)
52-
end
53-
5418
# One of atoms
5519
def random(%{type: Ash.Type.Atom, constraints: constraints}, _opts, _context) do
5620
constraints |> Keyword.fetch!(:one_of) |> Enum.random()

lib/transformer.ex

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ 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-
tagged_unions = Builder.build_action_argument(:tagged_unions, :map, default: %{})
1918

2019
dsl_state
2120
|> Builder.add_action(:action, :random_params,
2221
returns: :map,
23-
arguments: [action, init_params, unfill, fill, tagged_unions],
22+
arguments: [action, init_params, unfill, fill],
2423
run: &__MODULE__.do_random_params/2
2524
)
2625
|> Builder.add_interface(:random_params, args: [:action, {:optional, :init_params}])
@@ -45,8 +44,7 @@ defmodule AshRandomParams.Transformer do
4544
action: action,
4645
init_params: init_params,
4746
unfill: unfill,
48-
fill: fill,
49-
tagged_unions: tagged_unions
47+
fill: fill
5048
}
5149
} =
5250
input,
@@ -57,7 +55,6 @@ defmodule AshRandomParams.Transformer do
5755

5856
action = Ash.Resource.Info.action(resource, action)
5957
init_keys = init_params |> Map.keys()
60-
union_keys = tagged_unions |> Map.values()
6158

6259
belongs_to_attrs =
6360
resource
@@ -97,7 +94,6 @@ defmodule AshRandomParams.Transformer do
9794
candidates
9895
|> Enum.reject(&(&1.name in rel_names))
9996
|> Enum.reject(&(&1.name in belongs_to_attrs))
100-
|> Enum.reject(&(&1.name in union_keys))
10197
|> Enum.reject(&(&1.name in unfill))
10298
|> Enum.reject(& &1.allow_nil?)
10399

@@ -124,27 +120,6 @@ defmodule AshRandomParams.Transformer do
124120

125121
result_params = placeholder |> Map.merge(init_params) |> Map.merge(generated_params)
126122

127-
union_params =
128-
tagged_unions
129-
|> Enum.reject(fn {_tag_field, union_field} -> union_field in init_keys end)
130-
|> Map.new(fn {tag_field, union_field} ->
131-
union_attr = resource |> Ash.Resource.Info.attribute(union_field)
132-
subtype_tag = result_params |> Map.fetch!(tag_field)
133-
134-
{random_mod, random_opts} = random = AshRandomParams.Info.random_params_random!(resource)
135-
136-
union_value =
137-
random_mod.random(union_attr, random_opts, %{
138-
random: random,
139-
action_context: ctx,
140-
subtype_tag: subtype_tag
141-
})
142-
143-
{union_field, union_value}
144-
end)
145-
146-
result_params = Map.merge(result_params, union_params)
147-
148123
{:ok, result_params}
149124
end
150125

0 commit comments

Comments
 (0)