-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
union.ex
310 lines (259 loc) · 8.65 KB
/
union.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
defmodule Ash.Type.Union do
@constraints [
types: [
type: {:custom, __MODULE__, :union_types, []},
doc: """
The types to be unioned, a map of an identifier for the enum value to its configuration.
When using `tag` and `tag_value` we are referring to a map key that must equal a certain value
in order for the value to be considered an instance of that type.
For example:
types: [
int: [
type: :integer,
constraints: [
max: 10
]
],
object: [
type: MyObjectType,
tag: :type,
tag_value: "my_object"
],
other_object: [
type: MyOtherObjectType,
tag: :type,
tag_value: "my_other_object"
],
other_object_without_type: [
type: MyOtherObjectTypeWithoutType,
tag: :type,
tag_value: nil
]
]
IMPORTANT:
This is stored as a map under the hood. Filters over the data will need to take this into account.
Additionally, if you are not using a tag, a value will be considered to be of the given type if it successfully casts.
This means that, for example, if you try to cast `"10"` as a union of a string and an integer, it will end up as `"10"` because
it is a string. If you put the integer type ahead of the string type, it will cast first and `10` will be the value.
"""
]
]
@doc false
def union_types(value) do
{:ok,
Enum.reduce(value, [], fn {name, config}, types ->
config =
Keyword.update!(config, :type, fn type ->
Ash.Type.get_type(type)
end)
if config[:type] == Ash.Union do
config
|> Keyword.update!(:constraints, fn constraints ->
Keyword.update!(constraints, :types, fn types ->
{:ok, types} = union_types(types)
types
end)
end)
|> Keyword.get(:constraints)
|> Keyword.get(:types)
|> Enum.reduce(types, fn {new_name, new_config}, types ->
if types[new_name] do
raise "Detected a conflict in nested union type names. They must be unique all the way down."
else
Keyword.put(types, new_name, new_config)
end
end)
else
Keyword.put(types, name, config)
end
end)
|> Keyword.new(fn {key, config} ->
type = Ash.Type.get_type(config[:type])
Code.ensure_compiled!(unwrap_type(type))
if !Ash.Type.ash_type?(type) do
raise """
Unknown type in union type \"#{key}\": #{inspect(config[:type])}
"""
end
schema = Ash.Type.constraints(type)
constraints = Spark.OptionsHelpers.validate!(config[:constraints] || [], schema)
{key, config |> Keyword.put(:constraints, constraints) |> Keyword.put(:type, type)}
end)}
end
defp unwrap_type({:array, type}), do: unwrap_type(type)
defp unwrap_type(type), do: type
@moduledoc """
A union between multiple types, distinguished with a tag or by attempting to validate.
## Constraints
#{Spark.OptionsHelpers.docs(@constraints)}
"""
use Ash.Type
@impl true
def constraints, do: @constraints
## Find the minimal supported type?
@impl true
def storage_type, do: :map
@impl true
def cast_input(nil, _), do: {:ok, nil}
def cast_input(%Ash.Union{value: value, type: type_name}, constraints) do
type = constraints[:types][type_name][:type]
inner_constraints = constraints[:types][type_name][:constraints] || []
case Ash.Type.cast_input(type, value, inner_constraints) do
{:ok, value} ->
{:ok, %Ash.Union{value: value, type: type_name}}
error ->
error
end
end
def cast_input(value, constraints) do
types = constraints[:types] || []
types
|> Enum.sort_by(fn {_type_name, config} -> config[:tag] end)
|> Enum.reverse()
|> Enum.reduce_while({:error, []}, fn {type_name, config}, {:error, errors} ->
type = config[:type]
if is_map(value) && config[:tag] do
tag_value = config[:tag_value]
value =
if Ash.Type.embedded_type?(type) && !is_struct(value) do
Enum.reduce(Ash.Resource.Info.attributes(type), value, fn attr, value ->
with {:array, _nested_type} <- attr.type,
true <- has_key?(value, attr.name) do
update_key(value, attr.name, fn value ->
if is_map(value) && !is_struct(value) do
Keyword.values(value)
else
value
end
end)
else
_ ->
value
end
end)
else
value
end
if (Map.get(value, config[:tag]) || Map.get(value, to_string(config[:tag]))) == tag_value do
case Ash.Type.cast_input(type, value, config[:constraints] || []) do
{:ok, value} ->
case Ash.Type.apply_constraints(type, value, config[:constraints]) do
{:ok, value} ->
{:halt,
{:ok,
%Ash.Union{
value: value,
type: type_name
}}}
{:error, other} ->
{:halt, {:error, "is not a valid #{type_name}: #{inspect(other)}"}}
end
{:error, other} ->
{:halt, {:error, "is not a valid #{type_name}: #{inspect(other)}"}}
:error ->
{:halt, {:error, "is not a valid #{type_name}"}}
end
else
{:cont,
{:error,
Keyword.put(errors, type_name, "#{config[:tag]} does not equal #{config[:tag_value]}")}}
end
else
if config[:tag] do
{:cont, {:error, Keyword.put(errors, type_name, "is not a map")}}
else
case Ash.Type.cast_input(type, value, config[:constraints] || []) do
{:ok, value} ->
case Ash.Type.apply_constraints(type, value, config[:constraints]) do
{:ok, value} ->
{:halt,
{:ok,
%Ash.Union{
value: value,
type: type_name
}}}
{:error, other} ->
{:cont, {:error, Keyword.put(errors, type_name, other)}}
end
{:error, other} ->
{:cont, {:error, Keyword.put(errors, type_name, other)}}
:error ->
{:cont, {:error, Keyword.put(errors, type_name, "is invalid")}}
end
end
end
end)
|> case do
{:error, errors} when is_binary(errors) ->
{:error, errors}
{:error, errors} ->
{:error, error_message(errors)}
{:ok, value} ->
{:ok, value}
value ->
value
end
end
defp has_key?(map, key) do
Map.has_key?(map, key) || Map.has_key?(map, to_string(key))
end
defp update_key(map, key, func) do
cond do
Map.has_key?(map, key) ->
Map.update!(map, key, func)
Map.has_key?(map, to_string(key)) ->
Map.update!(map, to_string(key), func)
true ->
map
end
end
defp error_message(errors) do
"No union type matched\n" <>
Enum.map_join(errors, "\n", fn {key, errors} ->
" #{key}: #{inspect(errors)}"
end)
end
@impl true
def cast_stored(nil, _), do: {:ok, nil}
def cast_stored(%{"type" => type, "value" => value}, constraints) do
type =
if is_binary(type) do
String.to_existing_atom(type)
else
type
end
types = constraints[:types] || []
case Keyword.fetch(types, type) do
{:ok, config} ->
case Ash.Type.cast_stored(config[:type], value, config[:constraints]) do
{:ok, casted_value} ->
{:ok,
%Ash.Union{
value: casted_value,
type: type
}}
other ->
other
end
other ->
other
end
end
def cast_stored(_, _), do: :error
@impl true
def dump_to_native(nil, _), do: {:ok, nil}
def dump_to_native(%Ash.Union{value: value, type: type_name}, constraints) do
type = constraints[:types][type_name][:type]
constraints = constraints[:types][type_name][:constraints] || []
if type do
case Ash.Type.dump_to_native(type, value, constraints) do
{:ok, value} ->
{:ok, %{"type" => type_name, "value" => value}}
other ->
other
end
else
:error
end
end
end