-
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
Showing
5 changed files
with
240 additions
and
131 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
defmodule TypedStructor.Definer.Defstruct do | ||
@moduledoc """ | ||
A definer to define a struct and a type for a given definition. | ||
## Additional options for `typed_structor` | ||
* `:define_struct` - if `false`, the type will be defined, but the struct will not be defined. Defaults to `true`. | ||
## Usage | ||
defmodule MyStruct do | ||
use TypedStructor | ||
typed_structor definer: :defstruct, define_struct: false do | ||
field :name, String.t() | ||
field :age, integer() | ||
end | ||
end | ||
""" | ||
|
||
@doc """ | ||
Defines a struct and a type for a given definition. | ||
""" | ||
defmacro define(definition) do | ||
quote do | ||
unquote(__MODULE__).__struct_ast__(unquote(definition)) | ||
unquote(__MODULE__).__type_ast__(unquote(definition)) | ||
end | ||
end | ||
|
||
@doc false | ||
defmacro __struct_ast__(definition) do | ||
ast = | ||
quote do | ||
{fields, enforce_keys} = | ||
Enum.map_reduce(unquote(definition).fields, [], fn field, acc -> | ||
name = Keyword.fetch!(field, :name) | ||
default = Keyword.get(field, :default) | ||
|
||
if Keyword.get(field, :enforce, false) and not Keyword.has_key?(field, :default) do | ||
{{name, default}, [name | acc]} | ||
else | ||
{{name, default}, acc} | ||
end | ||
end) | ||
|
||
@enforce_keys Enum.reverse(enforce_keys) | ||
defstruct fields | ||
end | ||
|
||
quote do | ||
if Keyword.get(unquote(definition).options, :define_struct, true) do | ||
unquote(ast) | ||
end | ||
end | ||
end | ||
|
||
@doc false | ||
defmacro __type_ast__(definition) do | ||
quote bind_quoted: [definition: definition] do | ||
fields = | ||
Enum.reduce(definition.fields, [], fn field, acc -> | ||
name = Keyword.fetch!(field, :name) | ||
type = Keyword.fetch!(field, :type) | ||
|
||
if Keyword.get(field, :enforce, false) or Keyword.has_key?(field, :default) do | ||
[{name, type} | acc] | ||
else | ||
[{name, quote(do: unquote(type) | nil)} | acc] | ||
end | ||
end) | ||
|
||
type_name = Keyword.get(definition.options, :type_name, :t) | ||
|
||
parameters = | ||
Enum.map( | ||
definition.parameters, | ||
fn parameter -> | ||
parameter | ||
|> Keyword.fetch!(:name) | ||
|> Macro.var(__MODULE__) | ||
end | ||
) | ||
|
||
case Keyword.get(definition.options, :type_kind, :type) do | ||
:type -> | ||
@type unquote(type_name)(unquote_splicing(parameters)) :: %__MODULE__{ | ||
unquote_splicing(fields) | ||
} | ||
|
||
:opaque -> | ||
@opaque unquote(type_name)(unquote_splicing(parameters)) :: %__MODULE__{ | ||
unquote_splicing(fields) | ||
} | ||
|
||
:typep -> | ||
@typep unquote(type_name)(unquote_splicing(parameters)) :: %__MODULE__{ | ||
unquote_splicing(fields) | ||
} | ||
end | ||
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
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,85 @@ | ||
defmodule DefinerTest do | ||
@compile {:no_warn_undefined, __MODULE__.Struct} | ||
|
||
use TypedStructor.TestCase, async: true | ||
|
||
describe "defstruct" do | ||
@tag :tmp_dir | ||
test "works", ctx do | ||
expected_types = | ||
with_tmpmodule Struct, ctx do | ||
@type t() :: %__MODULE__{ | ||
age: integer() | nil, | ||
name: String.t() | nil | ||
} | ||
|
||
defstruct [:age, :name] | ||
after | ||
fetch_types!(Struct) | ||
end | ||
|
||
generated_types = | ||
with_tmpmodule Struct, ctx do | ||
use TypedStructor | ||
|
||
typed_structor definer: :defstruct do | ||
field :name, String.t() | ||
field :age, integer() | ||
end | ||
after | ||
assert %{__struct__: Struct, name: nil, age: nil} === struct(Struct) | ||
|
||
fetch_types!(Struct) | ||
end | ||
|
||
assert_type expected_types, generated_types | ||
end | ||
|
||
@tag :tmp_dir | ||
test "define_struct false", ctx do | ||
deftmpmodule Struct, ctx do | ||
use TypedStructor | ||
|
||
typed_structor define_struct: false do | ||
parameter :age | ||
|
||
field :name, String.t() | ||
field :age, age | ||
end | ||
|
||
defstruct name: "Phil", age: 20 | ||
end | ||
|
||
assert %{__struct__: Struct, name: "Phil", age: 20} === struct(Struct) | ||
after | ||
cleanup_modules([__MODULE__.Struct], ctx.tmp_dir) | ||
end | ||
|
||
@tag :tmp_dir | ||
test "works with Ecto.Schema", ctx do | ||
deftmpmodule Struct, ctx do | ||
use TypedStructor | ||
|
||
typed_structor define_struct: false do | ||
parameter :age | ||
|
||
field :name, String.t() | ||
field :age, age | ||
end | ||
|
||
use Ecto.Schema | ||
|
||
schema "source" do | ||
field :name, :string | ||
field :age, :integer, default: 20 | ||
end | ||
end | ||
|
||
assert [:id, :name, :age] === Struct.__schema__(:fields) | ||
|
||
assert match?(%{__struct__: Struct, id: nil, name: nil, age: 20}, struct(Struct)) | ||
after | ||
cleanup_modules([__MODULE__.Struct], ctx.tmp_dir) | ||
end | ||
end | ||
end |
Oops, something went wrong.