Skip to content

Instantly share code, notes, and snippets.

@mkreyman
Last active June 23, 2018 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkreyman/76e0637e8c88e4144ea98fac33a3a64a to your computer and use it in GitHub Desktop.
Save mkreyman/76e0637e8c88e4144ea98fac33a3a64a to your computer and use it in GitHub Desktop.
Creating a struct from a map with string keys
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment