michaelwa

michaelwa

Converting Erlang Record into Maps and Maps into Erlang Record

Hello All,

I am missing the boat on what I think should be a simple conversion process.

Given:

defmodule User do
  require Record
  Record.defrecord(:user, name: "meg", age: "25")
end

Record to map is fairly straightforward:

user_map = Enum.into(user(record), %{})

But this is not optimal because for every ‘defrecord’ declaration you would need to create a conversion function new ‘defrecord’ type. E.g. “user” above. But at least there is a solution.

However, for the return trip from map to record I do not see any path.

This works:

record = user([age: 45, name: "Mike"])
# -> {:user, "Mike", 45}

But this does not:

attrs = [age: 45, name: "Mike"]
record = user(attrs)

** !?!?!? **

I did get this to work but this seems just a tiny bit convoluted.

defmodule Test.This do 
  def map_to_record(map) do
    [ hd | _ ] = Enum.map(
      [map],
      fn %{age: age, name: name}
      ->
        {:user, age, name}
      end)  
    hd
    end
end

And again this requires a custom function for every ‘defrecord’ declaration to move between maps to records.

Thanks in advanced!

Most Liked

al2o3cr

al2o3cr

Records on the Erlang side are a compile-time-only concern - at runtime, they are just tuples. In Elixir the macros created by defrecord contain the information necessary to understand where name is in {:user, "meg", "25"}.

That’s also why your “reverse” case fails: the argument to user/1 needs to be a compile-time keyword list, but user(attrs) happens at runtime.

You’d need to write out the list explicitly:

attrs = [age: 45, name: "Mike"]
record = user(age: attrs[:age], name: attrs[:name])

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
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

Other popular topics Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

We're in Beta

About us Mission Statement