mudasobwa

mudasobwa

Creator of Cure

Get a struct definition from `__before_compile__/2` callback

Prior to v1.18.0 I used the following vode to reach the struct definition in a module from __before_compile__/2 callback.

    defp fields(module) do
      module
      |> Module.get_attribute(:__struct__, %{})
      |> Map.delete(:__struct__)
      |> Map.keys()
    end

I know that’s the undocumented, internal feature, but I did not manage to find a better way of doing this. In v1.18.0 this :__struct__ module attribute has gone. By digging down the rabbit hole, I managed to do the following:

    defp fields(module) do
      with {set, _bag} <- module |> :elixir_module.data_tables(),
           true <- :ets.member(set, {:elixir, :struct}),
           [{{:elixir, :struct}, [_ | _] = fields}] <- :ets.lookup(set, {:elixir, :struct}) do
        for %{field: field} <- fields, do: field
      end
    end

While it works, it looks extremely hacky and might break again tomorrow. Is there the correct way of doing that that I am missing?

Marked As Solved

LostKobrakai

LostKobrakai

This function is also capable of expanding structs defined under the module being compiled.

That might be the new way to go then based on the changelog.

Also Liked

josevalim

josevalim

Creator of Elixir

And if you need to support previous versions, there is Macro.struct!.

Last Post!

josevalim

josevalim

Creator of Elixir

And if you need to support previous versions, there is Macro.struct!.

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New

We're in Beta

About us Mission Statement