Packages
phoenix_gen_api
2.20.0
2.22.0
2.21.1
2.21.0
2.20.1
2.20.0
2.19.0
2.18.1
2.18.0
2.17.1
2.17.0
2.16.0
2.15.0
2.14.0
2.13.0
2.12.0
2.11.0
2.10.0
2.9.1
2.9.0
2.8.0
2.7.0
2.6.1
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.1.1
2.1.0
2.0.1
2.0.0
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
0.2.1
0.2.0
0.1.1
0.1.0
0.0.16
0.0.15
0.0.14
0.0.13
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
A library for fast develop APIs in Elixir cluster, using Phoenix Channels for transport data, auto pull api configs from service nodes.
Current section
Files
Jump to
Current section
Files
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