Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nested constructors #18

Open
sitch opened this issue Oct 29, 2016 · 3 comments
Open

Handle nested constructors #18

sitch opened this issue Oct 29, 2016 · 3 comments

Comments

@sitch
Copy link

sitch commented Oct 29, 2016

Would be nice to have something akin to the

%{"nestedField" => %{id: 1}}
|>  Poison.decode! as: %Model{nested_field: [%Nested{}]}

See https://github.com/devinus/poison/pull/58/files

@gamache
Copy link
Contributor

gamache commented Jan 31, 2017

Hi, and thanks for the feature request! I like the syntax you've proposed, so I am going to use this as the active issue for this feature.

@benregn
Copy link

benregn commented Jan 3, 2022

Did this feature ever land?

@gamache
Copy link
Contributor

gamache commented Jan 4, 2022

It never did -- it wasn't a pain point here at Appcues, so we never ended up implementing this directly.

Since the constructor created by ExConstructor is marked by defoverridable, the super keyword can be used to the same effect:

iex(3)> defmodule Inner do
...(3)>   defstruct [:b]     
...(3)>   use ExConstructor
...(3)> end
{:module, Inner, <<...>>, :ok}
iex(4)> defmodule Outer do
...(4)>   defstruct [:a, :inner]    
...(4)>   use ExConstructor
...(4)>   def new(map) do
...(4)>     struct = super(map)
...(4)>     %{struct | inner: Inner.new(struct.inner)}
...(4)>   end
...(4)> end
{:module, Outer, <<...>>, :ok}
iex(5)> Outer.new(%{a: 1, inner: %{b: 99}})
%Outer{a: 1, inner: %Inner{b: 99}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants