-
Notifications
You must be signed in to change notification settings - Fork 1
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
Aleksei Matiushkin
committed
Feb 5, 2024
1 parent
7aa2a79
commit 875ea2b
Showing
8 changed files
with
119 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
defmodule Estructura.Coercer do | ||
@moduledoc """ | ||
Behaviour for coercion delegates. Instead of implementing the coercion handlers | ||
in `Estructura.Nested` inplace, one might do | ||
```elixir | ||
coerce do | ||
defdelegate foo.bar.created_at(value), to: :date | ||
end | ||
``` | ||
""" | ||
@callback coerce(value) :: {:ok, value} | {:error, any()} when value: term() | ||
end | ||
|
||
defmodule Estructura.Coercers.Integer do | ||
@moduledoc "Default coercer for `:integer`, coercing strings and floats by rounding" | ||
|
||
@behaviour Estructura.Coercer | ||
@impl Estructura.Coercer | ||
|
||
def coerce(value) when is_integer(value), do: {:ok, value} | ||
|
||
def coerce(value) when is_binary(value) do | ||
case Integer.parse(value) do | ||
{int, ""} -> {:ok, int} | ||
{_int, remainder} -> {:error, "Trailing garbage: ‹#{remainder}›"} | ||
:error -> {:error, "Invalid value: ‹#{inspect(value)}›"} | ||
end | ||
end | ||
|
||
def coerce(value) when is_float(value), do: {:ok, round(value)} | ||
end | ||
|
||
defmodule Estructura.Coercers.Date do | ||
@moduledoc "Default coercer for `:date`, coercing strings (_ISO8601_) and integers (_epoch_)" | ||
|
||
@behaviour Estructura.Coercer | ||
@impl Estructura.Coercer | ||
|
||
def coerce(%Date{} = value), do: {:ok, value} | ||
|
||
def coerce(value) when is_binary(value) do | ||
Date.from_iso8601(value) | ||
end | ||
end | ||
|
||
defmodule Estructura.Coercers.Datetime do | ||
@moduledoc "Default coercer for `:datetime`, coercing strings (_ISO8601_) and integers (_epoch_)" | ||
|
||
@behaviour Estructura.Coercer | ||
@impl Estructura.Coercer | ||
|
||
def coerce(%DateTime{} = value), do: {:ok, value} | ||
|
||
def coerce(value) when is_binary(value) do | ||
case DateTime.from_iso8601(value) do | ||
{:ok, result, 0} -> {:ok, result} | ||
{:ok, _result, offset} -> {:error, "Unsupported offset (#{offset})"} | ||
error -> error | ||
end | ||
end | ||
|
||
def coerce(value) when is_integer(value) do | ||
DateTime.from_unix(value) | ||
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
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