-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
uuid.ex
61 lines (51 loc) · 1.46 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
61
defmodule Ecto.UUID do
@moduledoc """
An Ecto type for UUIDs strings.
In contrast to the `:uuid` type, `Ecto.UUID` works
with UUID as strings instead of binary data.
"""
@behaviour Ecto.Type
@doc """
The Ecto primitive type.
"""
def type, do: :uuid
@doc """
UUIDs are never blank.
"""
def blank?(_), do: false
@doc """
Casts to UUID.
"""
def cast(<< _::64, ?-, _::32, ?-, _::32, ?-, _::32, ?-, _::96 >> = u), do: {:ok, u}
def cast(uuid = << _::128 >>), do: load(uuid)
def cast(_), do: :error
@doc """
Converts an string representing a UUID into a binary.
"""
def dump(<< u0::64, ?-, u1::32, ?-, u2::32, ?-, u3::32, ?-, u4::96 >>) do
Base.decode16(<< u0::64, u1::32, u2::32, u3::32, u4::96 >>, case: :mixed)
end
def dump(_), do: :error
@doc """
Converts a binary UUID into a string.
"""
def load(uuid = << _::128 >>) do
{:ok, encode(uuid)}
end
def load(_), do: :error
@doc """
Generates a version 4 (random) UUID.
"""
def generate do
<<u0::48, _::4, u1::12, _::2, u2::62>> = :crypto.strong_rand_bytes(16)
<<u0::48, 4::4, u1::12, 2::2, u2::62>> |> encode
end
defp encode(<<u0::32, u1::16, u2::16, u3::16, u4::48>>) do
hex_pad(u0, 8) <> "-" <> hex_pad(u1, 4) <> "-" <> hex_pad(u2, 4) <>
"-" <> hex_pad(u3, 4) <> "-" <> hex_pad(u4, 12)
end
defp hex_pad(hex, count) do
hex = Integer.to_string(hex, 16)
:binary.copy("0", count - byte_size(hex)) <> hex
end
end