-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jclem/add-ecto-type-impl
Add Ecto.Type implementation
- Loading branch information
Showing
5 changed files
with
118 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,40 @@ | ||
defmodule Base62UUIDField do | ||
@moduledoc """ | ||
Documentation for Base62UUIDField. | ||
A field that is stored as a v4 UUID but presented as a Base62-encoded binary string. | ||
""" | ||
|
||
@doc """ | ||
Hello world. | ||
@behaviour Ecto.Type | ||
|
||
## Examples | ||
@spec type() :: :string | ||
def type, do: :string | ||
|
||
iex> Base62UUIDField.hello() | ||
:world | ||
@spec cast(any) :: {:ok, String.t()} | :error | ||
def cast(val) when is_binary(val), do: {:ok, val} | ||
def cast(_), do: :error | ||
|
||
""" | ||
def hello do | ||
:world | ||
@spec dump(any) :: {:ok, any} | :error | ||
def dump(val) when is_binary(val) do | ||
with {:ok, decoded} <- Base62UUID.decode(val) do | ||
Ecto.UUID.dump(decoded) | ||
else | ||
_ -> :error | ||
end | ||
end | ||
|
||
def dump(_), do: :error | ||
|
||
@spec load(any) :: {:ok, any} | :error | ||
def load(val) when is_binary(val) do | ||
with {:ok, loaded} <- Ecto.UUID.load(val), | ||
{:ok, decoded} <- Base62UUID.encode(loaded) do | ||
{:ok, decoded} | ||
else | ||
_ -> :error | ||
end | ||
end | ||
|
||
def load(_), do: :error | ||
|
||
@spec autogenerate() :: String.t() | ||
def autogenerate, do: Base62UUID.generate() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,55 @@ | ||
defmodule Base62UUIDFieldTest do | ||
use ExUnit.Case | ||
doctest Base62UUIDField | ||
use ExUnit.Case, async: true | ||
|
||
test "greets the world" do | ||
assert Base62UUIDField.hello() == :world | ||
setup do | ||
uuid = Ecto.UUID.generate() | ||
{:ok, ecto_dumped_uuid} = Ecto.UUID.dump(uuid) | ||
{:ok, uuid: uuid, encoded_uuid: Base62UUID.encode!(uuid), ecto_dumped_uuid: ecto_dumped_uuid} | ||
end | ||
|
||
test ".autogenerate generates a Base62 UUID" do | ||
assert String.length(Base62UUIDField.autogenerate()) == 22 | ||
end | ||
|
||
test ".type is a string" do | ||
assert Base62UUIDField.type() == :string | ||
end | ||
|
||
test ".cast casts to a string" do | ||
assert Base62UUIDField.cast("1") == {:ok, "1"} | ||
end | ||
|
||
test ".cast on a non-binary returns an error" do | ||
assert Base62UUIDField.cast(1) == :error | ||
end | ||
|
||
test ".dump decodes the value to a UUID", %{ | ||
encoded_uuid: encoded_uuid, | ||
ecto_dumped_uuid: ecto_dumped_uuid | ||
} do | ||
assert Base62UUIDField.dump(encoded_uuid) == {:ok, ecto_dumped_uuid} | ||
end | ||
|
||
test ".dump on a non-binary returns an error" do | ||
assert Base62UUIDField.dump(1) == :error | ||
end | ||
|
||
test ".dump returns an error for a non-UUID" do | ||
assert Base62UUIDField.dump("!") == :error | ||
end | ||
|
||
test ".load encodes the value to a Base62 UUID", %{ | ||
ecto_dumped_uuid: ecto_dumped_uuid, | ||
encoded_uuid: encoded_uuid | ||
} do | ||
assert Base62UUIDField.load(ecto_dumped_uuid) == {:ok, encoded_uuid} | ||
end | ||
|
||
test ".load on a non-binary returns an error" do | ||
assert Base62UUIDField.load(1) == :error | ||
end | ||
|
||
test ".load returns an error for a non-UUID" do | ||
assert Base62UUIDField.load("not-a-uuid") == :error | ||
end | ||
end |