-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
set_types.ex
163 lines (139 loc) · 4.66 KB
/
set_types.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
defmodule Ash.Resource.Transformers.SetTypes do
@moduledoc "Sets the `source` key on relationships to be the resource they were defined on"
use Ash.Dsl.Transformer
alias Ash.Dsl.Transformer
def transform(_resource, dsl_state) do
with {:ok, dsl_state} <- set_attribute_types(dsl_state),
{:ok, dsl_state} <- set_argument_types(dsl_state),
{:ok, dsl_state} <- set_calculation_types(dsl_state) do
{:ok, dsl_state}
end
end
defp set_attribute_types(dsl_state) do
dsl_state
|> Transformer.get_entities([:attributes])
|> Enum.reduce_while({:ok, dsl_state}, fn attribute, {:ok, dsl_state} ->
type = Ash.Type.get_type(attribute.type)
case validate_constraints(type, attribute.constraints) do
{:ok, constraints} ->
{:cont,
{:ok,
Transformer.replace_entity(
dsl_state,
[:attributes],
%{attribute | type: type, constraints: constraints},
fn replacing ->
replacing.name == attribute.name
end
)}}
{:error, error} ->
{:halt, {:error, error}}
end
end)
end
defp set_argument_types(dsl_state) do
dsl_state
|> Transformer.get_entities([:actions])
|> Enum.reduce_while({:ok, dsl_state}, fn action, {:ok, dsl_state} ->
new_arguments =
action.arguments
|> Enum.reduce_while({:ok, []}, fn argument, {:ok, args} ->
type = Ash.Type.get_type(argument.type)
case validate_constraints(type, argument.constraints) do
{:ok, constraints} ->
{:cont, {:ok, [%{argument | type: type, constraints: constraints} | args]}}
{:error, error} ->
{:halt, {:error, error}}
end
end)
case new_arguments do
{:ok, new_args} ->
{:cont,
{:ok,
Transformer.replace_entity(
dsl_state,
[:actions],
%{action | arguments: Enum.reverse(new_args)},
fn replacing ->
replacing.name == action.name && replacing.type == action.type
end
)}}
{:error, error} ->
{:halt, {:error, error}}
end
end)
end
defp set_calculation_types(dsl_state) do
dsl_state
|> Transformer.get_entities([:calculations])
|> Enum.reduce_while({:ok, dsl_state}, fn calculation, {:ok, dsl_state} ->
new_arguments =
calculation.arguments
|> Enum.reduce_while({:ok, []}, fn argument, {:ok, args} ->
type = Ash.Type.get_type(argument.type)
case validate_constraints(type, argument.constraints) do
{:ok, constraints} ->
{:cont, {:ok, [%{argument | type: type, constraints: constraints} | args]}}
{:error, error} ->
{:halt, {:error, error}}
end
end)
case new_arguments do
{:ok, new_args} ->
type = Ash.Type.get_type(calculation.type)
{:cont,
{:ok,
Transformer.replace_entity(
dsl_state,
[:calculations],
%{calculation | arguments: Enum.reverse(new_args), type: type},
fn replacing ->
replacing.name == calculation.name
end
)}}
{:error, error} ->
{:error, error}
end
end)
end
def validate_constraints(type, constraints) do
case type do
{:array, type} ->
with {:ok, new_constraints} <-
Ash.OptionsHelpers.validate(
Keyword.delete(constraints, :items),
Ash.Type.array_constraints(type)
),
{:ok, item_constraints} <- validate_item_constraints(type, constraints) do
if item_constraints do
{:ok, Keyword.put(new_constraints, :items, item_constraints)}
else
{:ok, new_constraints}
end
end
type ->
schema = Ash.Type.constraints(type)
case Ash.OptionsHelpers.validate(constraints, schema) do
{:ok, constraints} ->
{:ok, constraints}
{:error, error} ->
{:error, error}
end
end
end
defp validate_item_constraints(type, constraints) do
if Keyword.has_key?(constraints, :items) do
schema = Ash.Type.constraints(type)
case Ash.OptionsHelpers.validate(constraints[:items], schema) do
{:ok, item_constraints} ->
{:ok, item_constraints}
{:error, error} ->
{:error, error}
end
else
{:ok, nil}
end
end
def after?(Ash.Resource.Transformers.BelongsToAttribute), do: true
def after?(_), do: false
end