%Struct{} vs %{__struct__: Struct}

Here’s one approach. I chose the URI struct because it’s small and in the std lib.

iex(39)> u = URI.parse "http://forum.elixirforum.com"
%URI{
  authority: "forum.elixirforum.com",
  fragment: nil,
  host: "forum.elixirforum.com",
  path: nil,
  port: 80,
  query: nil,
  scheme: "http",
  userinfo: nil
}
iex(40)> m = Map.put(u, :y, 5)
%{
  __struct__: URI,
  authority: "forum.elixirforum.com",
  fragment: nil,
  host: "forum.elixirforum.com",
  path: nil,
  port: 80,
  query: nil,
  scheme: "http",
  userinfo: nil,
  y: 5
}
iex(41)> s = struct(m.__struct__, Map.from_struct(m))
%URI{
  authority: "forum.elixirforum.com",
  fragment: nil,
  host: "forum.elixirforum.com",
  path: nil,
  port: 80,
  query: nil,
  scheme: "http",
  userinfo: nil
}

struct/2 ignores keys that are not specified in the struct itself