Skip to content

Commit

Permalink
Delete Ecto deps from generating UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed Apr 17, 2024
1 parent 195afac commit 32b9b3b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Changelog for MishkaDeveloperTools 0.1.6

### Features:

- [x] Add `Crypto` helper module
- [x] Add new optional dependencies and their wrappers [`nimble_totp`, `joken`, `jason`, `plug`, `bcrypt_elixir`, `pbkdf2_elixir`, `argon2_elixir`]

# Changelog for MishkaDeveloperTools 0.1.5

---
Expand Down
33 changes: 32 additions & 1 deletion lib/helper/uuid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,37 @@ defmodule MishkaDeveloperTools.Helper.UUID do
@type t :: <<_::288>>
@type raw :: <<_::128>>

defmodule HelperEcto.CastError do
@moduledoc """
Raised when a changeset can't cast a value.
"""
defexception [:message, :type, :value]

def exception(opts) do
type = Keyword.fetch!(opts, :type)
value = Keyword.fetch!(opts, :value)
msg = opts[:message] || "cannot cast #{inspect(value)} to #{format(type)}"
%__MODULE__{message: msg, type: type, value: value}
end

@doc """
Format type for error messaging and logs.
"""
def format({composite, type}) when composite in [:array, :map] do
"{#{inspect(composite)}, #{format(type)}}"
end

def format({:parameterized, type, params}) do
if function_exported?(type, :format, 1) do
apply(type, :format, [params])
else
"##{inspect(type)}<#{inspect(params)}>"
end
end

def format(type), do: inspect(type)
end

@spec cast(t | raw | any) :: {:ok, t} | :error
def cast(uuid)

Expand All @@ -36,7 +67,7 @@ defmodule MishkaDeveloperTools.Helper.UUID do
def cast!(uuid) do
case cast(uuid) do
{:ok, hex_uuid} -> hex_uuid
:error -> raise Ecto.CastError, type: __MODULE__, value: uuid
:error -> raise HelperEcto.CastError, type: __MODULE__, value: uuid
end
end

Expand Down

0 comments on commit 32b9b3b

Please sign in to comment.