%Struct{} vs %{__struct__: Struct}

Hi,

Lets say I have a struct, lets say named Struct with the singular field x (default 0).

When I create it I get &Struct{x: 0}
Since they are maps I then use Map.put to add something else like y: 5 I get %{struct: S, x: 0, y: 5} and it is no longer an Stuct

Is there some way to purge any non struct fields and restore it to an Struct?

(Here the whole idea is obviously stupid)

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

iex(39)> u = URI.parse "http://elixirforum.com"
%URI{
  authority: "elixirforum.com",
  fragment: nil,
  host: "elixirforum.com",
  path: nil,
  port: 80,
  query: nil,
  scheme: "http",
  userinfo: nil
}
iex(40)> m = Map.put(u, :y, 5)
%{
  __struct__: URI,
  authority: "elixirforum.com",
  fragment: nil,
  host: "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: "elixirforum.com",
  fragment: nil,
  host: "elixirforum.com",
  path: nil,
  port: 80,
  query: nil,
  scheme: "http",
  userinfo: nil
}

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

2 Likes

I prefer the previous answer, but another option would get a list of keys expected in the struct and take them from the map.

Building on the other example:

iex> Map.take(m, Map.keys(%URI{}))
%URI{
  authority: "elixirforum.com",
  fragment: nil,
  host: "elixirforum.com",
  path: nil,
  port: 80,
  query: nil,
  scheme: "http",
  userinfo: nil
}

Two downsides to this approach compared to the previous:

  1. This solution won’t replace any missing keys.
  2. This approach doesn’t let you create the struct dynamically (not without wrapping it in struct/0 at least, which at that point you might as well do the other).
1 Like

Looking at these answers I realized that my existing code is a bit backwards and what I really wanted can be done better with this solution.

Thank you both for replies!

you can also use map cons, if you would like for your code to crash when you try to assign a nonexistent field, lets you catch errors in tests (how good is your test coverage? lol)

defmodule S do
  defstruct [x: 0]
  def do_something(val = %S{}) do
     %{val | y: "oops"}
  end
end

will crash on do_something. Whenever it’s a struct, I prefer using map cons for that bailout behaviour.

3 Likes