I'm learning Elixir and came across such situation:
I have an Ecto schema and I want to make a function like "get_by" that takes a column name and it's value as an arguments like this: get_by(:id, 7)
So the working version of the function would be like this:
def get_by(column, value) do
Repo.all(
from(
r in __MODULE__,
where: field(r, ^column) == ^value,
)
)
end
I know this is fully functional but I was wondering how the field
macro works.
The original code is too hard to read for me. I was trying to play with AST in macro, but nothing seems to work. The best I had was this:
defmacro magic(var, {:^, _, [{column, _, _}]}) do
dot = {:., [], [var, column]}
{dot, [], []}
end
But this returns r.column
instead of the atom bound to column
variable.
How the macro should be written to return r.id
?
If you check the source code for Ecto.Query.API.field/2
, you’ll see that the explicit call to this function (it’s not a macro btw) raises.
That is because it makes sense only inside Ecto.Query.from/2
macro.
What you want is still possible to some extent; not with a dot notation (AFAICT, but with an Access
)
defmodule M do
defmacro magic(a1, {:^, _, [a2]}) do
quote do: unquote(a1)[unquote(a2)]
end
end
import M
{r, column} = {%{id: 42}, :id}
magic(r, ^column)
#⇒ 42
I was unable to get quote do: unquote(a1).unquote(a2)
work without absolutely nasty tricks like inplace eval.
To better understand macros, you probably should clarify for yourself what AST is available where.
I highly recommend Metaprogramming Elixir by Chris McCord.