In Elixir how do you initialize a struct with a map variable

I know its possible to create a struct via %User{ email: '[email protected]' }. But if I had a variable params = %{email: '[email protected]'} is there a way to create that struct using that variable for eg, %User{ params }.

This gives an error, just wondering if you can explode it or some other way?


Solution 1:

You should use the struct/2 function. From the docs:

defmodule User do
  defstruct name: "john"
end

struct(User)
#=> %User{name: "john"}

opts = [name: "meg"]
user = struct(User, opts)
#=> %User{name: "meg"}

struct(user, unknown: "value")
#=> %User{name: "meg"}

Solution 2:

The previous answers are both good, with one caveat: The keys in the struct are atoms, the keys in your hash might be strings. Using the struct() method will only copy over the keys that match, and strings won't match to the atoms. Example:

defmodule User do
  defstruct name: "john"
end

opts = %{"name" => "meg"}
user = struct(User, opts)
#=> %User{name: "john"}

Using merge is odd, too, because it will "undo" the struct nature of the Map:

user = Map.merge(%User{}, opts)
#=> %{:__struct__ => User, :name => "john", "name" => "meg"}

Found this on the elixir-lang-talk Google Group, from Jose himself:

https://groups.google.com/d/msg/elixir-lang-talk/6geXOLUeIpI/L9einu4EEAAJ

That's pretty much the way to go except you can do everything in one pass:

def to_struct(kind, attrs) do
  struct = struct(kind)
  Enum.reduce Map.to_list(struct), struct, fn {k, _}, acc ->
    case Map.fetch(attrs, Atom.to_string(k)) do
      {:ok, v} -> %{acc | k => v}
      :error -> acc
    end
  end
end

Solution 3:

Another way of doing it using Map.merge/2:

merge(map1, map2)

Example:

params
#=> %{email: "[email protected]"}

%User{} |> Map.merge(params)
#=> %User{ email: '[email protected]' }