-
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.
* Behaviour + sample impl for `Date` * Fix GHA * Types for date/time and IP * Types for IP/URI and SCaffold for enum/tags * Credo + Dialyzer
- Loading branch information
Showing
16 changed files
with
758 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ jobs: | |
matrix: | ||
include: | ||
- pair: | ||
otp: 24.2 | ||
otp: 25 | ||
elixir: 1.14 | ||
- pair: | ||
otp: 27 | ||
|
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,34 @@ | ||
defmodule Estructura.Nested.Type do | ||
@moduledoc """ | ||
The type to be used for coercing, validating, and generation | ||
of the implementation’s instances. | ||
""" | ||
|
||
@doc "The generator for the type" | ||
@callback generate() :: StreamData.t(any()) | ||
|
||
@doc "The generator for the type accepting options" | ||
@callback generate(keyword()) :: StreamData.t(any()) | ||
|
||
@doc "Coerces the value coming from outside" | ||
@callback coerce(term()) :: {:ok, term()} | {:error, any()} | ||
|
||
@doc "Validates the value as being correct" | ||
@callback validate(term()) :: {:ok, term()} | {:error, any()} | ||
|
||
defmodule Scaffold do | ||
@moduledoc false | ||
|
||
@callback type_module_ast(name :: module(), opts :: keyword()) :: Macro.t() | ||
|
||
@spec create(module(), module(), keyword()) :: module() | false | ||
def create(scaffold, name, options) do | ||
with true <- is_atom(scaffold), | ||
true <- Code.ensure_loaded?(scaffold), | ||
true <- function_exported?(scaffold, :type_module_ast, 2), | ||
{:module, module, _bytecode, _} <- | ||
scaffold.type_module_ast(name, options), | ||
do: module | ||
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,16 @@ | ||
defmodule Estructura.Nested.Type.Date do | ||
@moduledoc """ | ||
`Estructura` type for `Date` | ||
""" | ||
@behaviour Estructura.Nested.Type | ||
|
||
@impl true | ||
def generate(opts \\ []), do: Estructura.StreamData.date(opts) | ||
|
||
@impl true | ||
defdelegate coerce(term), to: Estructura.Coercers.Date | ||
|
||
@impl true | ||
def validate(%Date{} = term), do: {:ok, term} | ||
def validate(other), do: {:error, "Expected date, got: " <> inspect(other)} | ||
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,16 @@ | ||
defmodule Estructura.Nested.Type.DateTime do | ||
@moduledoc """ | ||
`Estructura` type for `Date` | ||
""" | ||
@behaviour Estructura.Nested.Type | ||
|
||
@impl true | ||
def generate(opts \\ []), do: Estructura.StreamData.datetime(opts) | ||
|
||
@impl true | ||
defdelegate coerce(term), to: Estructura.Coercers.DateTime | ||
|
||
@impl true | ||
def validate(%DateTime{} = term), do: {:ok, term} | ||
def validate(other), do: {:error, "Expected date, got: " <> inspect(other)} | ||
end |
Oops, something went wrong.