In Elixir how do you initialize a struct with a ma

2019-01-11 12:00发布

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?

3条回答
时光不老,我们不散
2楼-- · 2019-01-11 12:39

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楼-- · 2019-01-11 12:48

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' }
查看更多
该账号已被封号
4楼-- · 2019-01-11 12:55

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"}
查看更多
登录 后发表回答