-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
uuid.ex
60 lines (46 loc) · 1.17 KB
/
uuid.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
defmodule Ash.Type.UUID do
@moduledoc """
Represents a UUID.
A builtin type that can be referenced via `:uuid`
"""
use Ash.Type
@impl true
def storage_type(_), do: :uuid
@impl true
def generator(_constraints) do
StreamData.repeatedly(&Ash.UUID.generate/0)
end
@impl true
def cast_input(nil, _), do: {:ok, nil}
def cast_input(value, _) when is_binary(value) do
case String.valid?(value) do
true -> Ecto.Type.cast(Ecto.UUID, String.trim(value))
false -> Ecto.Type.cast(Ecto.UUID, value)
end
end
def cast_input(value, _) do
Ecto.Type.cast(Ecto.UUID, value)
end
@impl true
def cast_stored(nil, _), do: {:ok, nil}
def cast_stored(value, constraints) do
case Ecto.Type.load(Ecto.UUID, value) do
:error ->
cast_input(value, constraints)
{:ok, value} ->
{:ok, value}
end
rescue
_e in ArgumentError ->
cast_input(value, constraints)
end
@impl true
def dump_to_embedded(value, constraints) do
cast_input(value, constraints)
end
@impl true
def dump_to_native(nil, _), do: {:ok, nil}
def dump_to_native(value, _) do
Ecto.Type.dump(Ecto.UUID, value)
end
end