-
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
6 changed files
with
113 additions
and
79 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,76 @@ | ||
# Implement reflection functions | ||
|
||
Define a plugin that generates reflection functions that | ||
can be used to access the fields and parameters of a struct. | ||
|
||
## Implement | ||
|
||
```elixir | ||
defmodule Guides.Plugins.Reflection do | ||
use TypedStructor.Plugin | ||
|
||
@impl TypedStructor.Plugin | ||
defmacro after_definition(definition, _opts) do | ||
quote bind_quoted: [definition: definition] do | ||
fields = Enum.map(definition.fields, &Keyword.fetch!(&1, :name)) | ||
|
||
enforced_fields = | ||
definition.fields | ||
|> Stream.filter(&Keyword.get(&1, :enforce, false)) | ||
|> Stream.map(&Keyword.fetch!(&1, :name)) | ||
|> Enum.to_list() | ||
|
||
def __typed_structor__(:fields), do: unquote(fields) | ||
def __typed_structor__(:parameters), do: unquote(definition.parameters) | ||
def __typed_structor__(:enforced_fields), do: unquote(enforced_fields) | ||
|
||
for field <- definition.fields do | ||
name = Keyword.fetch!(field, :name) | ||
type = field |> Keyword.fetch!(:type) |> Macro.escape() | ||
|
||
def __typed_structor__(:type, unquote(name)), do: unquote(type) | ||
def __typed_structor__(:field, unquote(name)), do: unquote(Macro.escape(field)) | ||
end | ||
end | ||
end | ||
end | ||
``` | ||
|
||
## Usage | ||
```elixir | ||
defmodule User do | ||
use TypedStructor | ||
|
||
typed_structor do | ||
plugin Guides.Plugins.Reflection | ||
|
||
parameter :age | ||
|
||
field :name, String.t(), enforce: true | ||
field :age, age, default: 20 | ||
end | ||
end | ||
|
||
defmodule MyApp do | ||
use TypedStructor | ||
|
||
typed_structor module: User, enforce: true do | ||
plugin Guides.Plugins.Reflection | ||
|
||
field :name, String.t() | ||
field :age, integer() | ||
end | ||
end | ||
``` | ||
|
||
```elixir | ||
iex> User.__typed_structor__(:fields) | ||
[:name, :age] | ||
iex> User.__typed_structor__(:parameters) | ||
[:age] | ||
|
||
iex> MyApp.User.__typed_structor__(:enforced_fields) | ||
[:name, :age] | ||
iex> Macro.to_string(User.__typed_structor__(:type, :age)) | ||
"age" | ||
``` |
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,35 @@ | ||
defmodule Guides.Plugins.ReflectionTest do | ||
use TypedStructor.GuideCase, | ||
async: true, | ||
guide: "reflection.md" | ||
|
||
test "generates reflection functions" do | ||
assert [:name, :age] === User.__typed_structor__(:fields) | ||
assert [:name, :age] === MyApp.User.__typed_structor__(:fields) | ||
|
||
assert [:age] === User.__typed_structor__(:parameters) | ||
assert [] === MyApp.User.__typed_structor__(:parameters) | ||
|
||
assert [:name] === User.__typed_structor__(:enforced_fields) | ||
assert [:name, :age] === MyApp.User.__typed_structor__(:enforced_fields) | ||
|
||
assert "String.t()" === Macro.to_string(User.__typed_structor__(:type, :name)) | ||
assert "age" === Macro.to_string(User.__typed_structor__(:type, :age)) | ||
|
||
assert "String.t()" === Macro.to_string(MyApp.User.__typed_structor__(:type, :name)) | ||
assert "integer()" === Macro.to_string(MyApp.User.__typed_structor__(:type, :age)) | ||
|
||
assert [enforce: true, name: :name, type: type] = User.__typed_structor__(:field, :name) | ||
assert "String.t()" === Macro.to_string(type) | ||
|
||
assert [enforce: true, name: :age, type: type] = | ||
MyApp.User.__typed_structor__(:field, :age) | ||
|
||
assert "integer()" === Macro.to_string(type) | ||
|
||
assert [enforce: true, name: :name, type: type] = User.__typed_structor__(:field, :name) | ||
assert "String.t()" === Macro.to_string(type) | ||
assert [default: 20, name: :age, type: type] = User.__typed_structor__(:field, :age) | ||
assert "age" === Macro.to_string(type) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.