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

https://hexdocs.pm/elixir/1.18.0-rc.0/Macro.html#struct_info!/2

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!.

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement