Hi there,
We have a struct similar to:
defmodule MyStruct do
@type t :: %__MODULE__{
field: integer,
nested_map: %{
nested_field: integer,
},
}
defstruct [
:field,
:nested_map,
]
end
And we would like to be able to safely (without String.to_atom) convert user input like:
%{"field" => 3, "nested_map" => %{"nested_field" => 4}}
to a struct, like:
%MyStruct{field: 3, nested_map: %{nested_field: 4}}
I haven't been able to find an obvious way of doing this that doesn't incur significant duplication or work. We have tried recursively using `String.to_existing_atom/1`, but :nested_field won't be in the atom table unless some code that uses it has been loaded somewhere, which isn't always the case for us.
I've been able to ensure that the atoms are in the atom table by doing `Kernel.Typespec.beam_types(MyStruct)` but that seems like a huge hack.
Is there an idiomatic way of doing this? Or am I looking at a macro that defines the typespec, the struct and the Poison.Decoder implementation all at once?
Thanks,
Aaron