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

馋奶兔 提交于 2019-11-26 18:16:50

问题


I know its possible to create a struct via %User{ email: 'blah@blah.com' }. But if I had a variable params = %{email: 'blah@blah.com'} 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?


回答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"}



回答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



回答3:


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

merge(map1, map2)

Example:

params
#=> %{email: "blah@blah.com"}

%User{} |> Map.merge(params)
#=> %User{ email: 'blah@blah.com' }


来源:https://stackoverflow.com/questions/30927635/in-elixir-how-do-you-initialize-a-struct-with-a-map-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!