Current section

Files

Jump to
phoenix_gen_api lib phoenix_gen_api helpers impl_helper impl.ex
Raw

lib/phoenix_gen_api/helpers/impl_helper/impl.ex

defmodule PhoenixGenApi.ImplHelper do
@moduledoc """
Macro to generate simple implementation of protocol.
Support for easy to use with general encoder.
Utility macro to generate implementation for struct.
The target struct must have `encode!/2` function in module.
Usage:
```Elixir
use PhoenixGenApi.ImplHelper, encoder: JSON.Encoder, impl: [AModule1, AModule2, ...]
```
Using macro without option in `use` keyword.
Target module must have `encode!/2` function
Generate implementation from struct for JSON.Encoder like this:
```Elixir
require PhoenixGenApi.ImplHelper
gen_impl JSON.Encoder, AModule
```
"""
defmacro __using__(opts) do
quote location: :keep, bind_quoted: [opts: opts] do
list_module = Keyword.get(opts, :impl, [])
encoder = Keyword.get(opts, :encoder)
if encoder == nil, do: raise("missing encoder option")
for module <- list_module do
PhoenixGenApi.ImplHelper.gen_impl(encoder, module)
end
end
end
defmacro gen_impl(encoder, module) do
quote do
defimpl unquote(encoder), for: unquote(module) do
def encode(data = %unquote(module){}, opts) do
data
|> unquote(module).encode!(opts)
end
end
end
end
end