-
-
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.
- Loading branch information
Giovanni Visciano
committed
Aug 21, 2021
1 parent
4345282
commit 463922d
Showing
6 changed files
with
202 additions
and
52 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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
defmodule DataSpecs.Loaders.Extra do | ||
@moduledoc """ | ||
Type loaders for Elixir types | ||
""" | ||
|
||
alias DataSpecs.{Loaders, Types} | ||
|
||
@spec mapset(Types.value(), Types.custom_type_loaders(), [Types.type_loader_fun()]) :: | ||
{:error, Types.reason()} | {:ok, MapSet.t()} | ||
|
||
@doc """ | ||
Type loader for Elixir MapSet.t(T). | ||
Expect an Enumarable value of type T, returns a MapSet.t(T). | ||
""" | ||
def mapset(value, custom_type_loaders, [type_params_loader]) do | ||
case Enumerable.impl_for(value) do | ||
nil -> | ||
{:error, ["can't convert #{inspect(value)} to a MapSet.t/1, value not enumerable"]} | ||
|
||
_ -> | ||
value | ||
|> Enum.to_list() | ||
|> Loaders.list(custom_type_loaders, [type_params_loader]) | ||
|> case do | ||
{:ok, loaded_value} -> | ||
{:ok, MapSet.new(loaded_value)} | ||
|
||
{:error, errors} -> | ||
{:error, ["can't convert #{inspect(value)} to a MapSet.t/1", errors]} | ||
end | ||
end | ||
end | ||
|
||
@spec isodatetime(Types.value(), Types.custom_type_loaders(), [Types.type_loader_fun()]) :: | ||
{:error, Types.reason()} | {:ok, DateTime.t()} | ||
|
||
@doc """ | ||
Type loader for Elixir DateTime.t(). | ||
Expect an iso8601 datetime string value, returns a DateTime.t(). | ||
""" | ||
def isodatetime(value, _custom_type_loaders, []) do | ||
with {:is_binary, true} <- {:is_binary, is_binary(value)}, | ||
{:from_iso8601, {:ok, datetime, _}} <- {:from_iso8601, DateTime.from_iso8601(value)} do | ||
{:ok, datetime} | ||
else | ||
{:is_binary, false} -> | ||
{:error, ["can't convert #{inspect(value)} to a DateTime.t/0"]} | ||
|
||
{:from_iso8601, {:error, reason}} -> | ||
{:error, ["can't convert #{inspect(value)} to a DateTime.t/0 (#{inspect(reason)})"]} | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule DataSpecs.Types do | ||
@moduledoc """ | ||
Common types | ||
""" | ||
|
||
@type value() :: any() | ||
@type reason :: [String.t() | reason()] | ||
@type type_id :: atom() | ||
@type type_ref :: {module(), type_id()} | ||
@type custom_type_ref :: {module(), type_id(), arity()} | ||
@type type_loader_fun :: (value(), custom_type_loaders(), [type_loader_fun] -> value()) | ||
@type custom_type_loaders :: %{custom_type_ref() => type_loader_fun()} | ||
end |
Oops, something went wrong.