I'm using macros and I want to pass a dynamic identifier to an Absinthe macro enum
, wanting to generate different enum
s with a set list. Everything is inside a for
comprehension.
I've read that Kernel.apply/3
does not work on macros.
for name <- [:hello, :world] do
enum unquote(name) do
value(:approved)
end
end
Getting as a result:
** (ArgumentError) argument error
:erlang.atom_to_binary({:unquote, [line: 36], [{:name, [line: 36], nil}]}, :utf8)
for name <- [:hello, :world] do
enum name do
value(:approved)
end
end
And get:
** (ArgumentError) argument error
:erlang.atom_to_binary({:name, [line: 36], nil}, :utf8)
It seems that I can't unquote anything that I pass as the identifier of the macro enum
. Is it possible to do this?
It is possible. The problem is enum
assumes the first argument is an atom.
defmodule MacroHelper do
defmacro enum_wrapper(names, do: block) do
for name <- names do
quote do
enum unquote(name), do: unquote(block)
end
end
end
end
defmodule AbsDemo do
use Absinthe.Schema.Notation
import MacroHelper
enum_wrapper [:hello, :world] do
value :approved
end
end