Ecto.UUID — Ecto v3.12.5 (original) (raw)
An Ecto type for UUID strings.
Summary
Types
A raw binary representation of a UUID.
A hex-encoded UUID string.
Functions
Generates a random, version 4 UUID in the binary format.
Casts either a string in the canonical, human-readable UUID format or a 16-byte binary to a UUID in its canonical, human-readable UUID format.
Converts a string representing a UUID into a raw binary.
Same as dump/1 but raises Ecto.ArgumentError
on invalid arguments.
Generates a random, version 4 UUID.
Converts a binary UUID into a string.
Same as load/1 but raises Ecto.ArgumentError
on invalid arguments.
Types
@type raw() :: <<_::128>>
A raw binary representation of a UUID.
A hex-encoded UUID string.
Functions
@spec bingenerate() :: raw()
Generates a random, version 4 UUID in the binary format.
@spec cast(t() | raw() | any()) :: {:ok, t()} | :error
Casts either a string in the canonical, human-readable UUID format or a 16-byte binary to a UUID in its canonical, human-readable UUID format.
If uuid
is neither of these, :error
will be returned.
Since both binaries and strings are represented as binaries, this means some strings you may not expect are actually also valid UUIDs in their binary form and so will be casted into their string form.
If you need further-restricted behavior or validation, you should define your own custom Ecto.Type. There is also Ecto.UUID.load/1 if you only want to process raw
UUIDs, which may be a more suitable reverse operation toEcto.UUID.dump/1.
Examples
iex> Ecto.UUID.cast(<<0x60, 0x1D, 0x74, 0xE4, 0xA8, 0xD3, 0x4B, 0x6E,
...> 0x83, 0x65, 0xED, 0xDB, 0x4C, 0x89, 0x33, 0x27>>)
{:ok, "601d74e4-a8d3-4b6e-8365-eddb4c893327"}
iex> Ecto.UUID.cast("601d74e4-a8d3-4b6e-8365-eddb4c893327")
{:ok, "601d74e4-a8d3-4b6e-8365-eddb4c893327"}
iex> Ecto.UUID.cast("warehouse worker")
{:ok, "77617265-686f-7573-6520-776f726b6572"}
@spec cast!(t() | raw() | any()) :: t()
Same as cast/1 but raises Ecto.CastError on invalid arguments.
@spec dump(uuid_string :: t() | any()) :: {:ok, raw()} | :error
Converts a string representing a UUID into a raw binary.
@spec dump!(t() | any()) :: raw()
Same as dump/1 but raises Ecto.ArgumentError
on invalid arguments.
Callback implementation for Ecto.Type.embed_as/1.
Callback implementation for Ecto.Type.equal?/2.
Generates a random, version 4 UUID.
@spec load(raw() | any()) :: {:ok, t()} | :error
Converts a binary UUID into a string.
@spec load!(raw() | any()) :: t()
Same as load/1 but raises Ecto.ArgumentError
on invalid arguments.