otuv
%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)
Marked As Solved
gregvaughn
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
Also Liked
ityonemo
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.
brettbeatty
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: "forum.elixirforum.com",
fragment: nil,
host: "forum.elixirforum.com",
path: nil,
port: 80,
query: nil,
scheme: "http",
userinfo: nil
}
Two downsides to this approach compared to the previous:
- This solution won’t replace any missing keys.
- This approach doesn’t let you create the struct dynamically (not without wrapping it in
struct/0at least, which at that point you might as well do the other).








