<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="37242" data-post-id="37242">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mihai-ciupina" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mihai-ciupina
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I cannot figure it out which solution is better, the one presented or the one José Valim created? Seems they are doing slightly different things.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="37242" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-37242" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="37242"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="110086" data-post-id="110086">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="philosodad" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/philosodad/120/12515_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  philosodad
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Or you could use the <a href="https://hex.pm/packages/morphix" rel="nofollow">Morphix</a> library.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="110086" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-110086" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="110086"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="112776" data-post-id="112776">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="i-n-g-m-a-r" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  i-n-g-m-a-r
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This <code>decode as</code> stuff is pretty cool.<br>
So why is it not part of Phoenix? (or am I missing something)<br>
This won’t work:</p>
<pre><code>defmodule Stuff do
  defstruct foo: nil, bar: nil
end

def handle_in("send_stuff", %Stuff{} = my_stuff, socket), do: something_with(my_stuff)
</code></pre>
<p>The incoming json is already decoded, so I would have to do something like:</p>
<pre><code>def handle_in("send_stuff", %{} = my_stuff, socket) do
  my_stuff
  |&gt; Poison.encode
  |&gt; (fn ({:ok, json}) -&gt; Poison.decode(json, as: %Stuff{}) end).()
  |&gt; something_with
end
</code></pre>
<p>My solution is to not use structs, which I don’t like, I would love to use structs.<br>
<code>%Stuff{} = my_stuff</code> is a lot more concise than <code>%{"foo" =&gt; foo, "bar" =&gt; bar}</code></p>
<p>In case of many parameters, the function definitions become unreadable and ugly,<br>
which is not an enjoyable kind of pattern matching.</p>
<p>Am I missing something?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112776" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-112776" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112776"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="112778" data-post-id="112778">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="i-n-g-m-a-r" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  i-n-g-m-a-r
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The <code>decode as</code>  stuff will not compile in conjunction with <code>@enforce_keys</code>.<br>
Probably this is related to the absence of the <code>%Stuff{} = my_stuff</code> feature.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112778" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-112778" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112778"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="112782" data-post-id="112782">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="peerreynders" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/120/5826_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  peerreynders
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>At the risk of stating the obvious</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Stuff do
  defstruct foo: nil, bar: nil

  def from_map(%{"foo" =&gt; foo, "bar" =&gt; bar}),
    do: %Stuff{foo: foo, bar: bar}

  # alternatively - return some error
  def from_map(m),
    do: m

  #...

end

# ...

def handle_in("send_stuff", stuff, socket) do
  with %Stuff{} = my_stuff &lt;- Stuff.from_map(stuff) do
    something_with(my_stuff)

  else
     # do something else with "stuff" argument ...

  end
end

# ... or

def handle_in("send_stuff", stuff, socket) do
  case Stuff.from_map(stuff) do
    %Stuff{} = my_stuff -&gt;
      something_with(my_stuff)

    # other options ...
    _ -&gt;
       # ...

  end
end

</code></pre>
<p><a href="https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1" rel="noopener nofollow ugc"><code>Kernel.with/1</code></a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112782" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-112782" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112782"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="112788" data-post-id="112788">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="i-n-g-m-a-r" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  i-n-g-m-a-r
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Tnx for your suggestions, much appreciated.<br>
I’m trying to avoid boilerplate code in struct definitions and<br>
if/else/with/case statements in functions (I prefer pattern matching).</p>
<p>I am thinking about something like this, using a helper function:</p>
<pre><code>def to_struct(%{} = params, kind) do
  params
  |&gt; Poison.encode
  |&gt; (fn {:ok, json} -&gt; json end).()
  |&gt; Poison.decode(%{as: kind, keys: :atoms!})
  |&gt; (fn {:ok, decoded} -&gt; decoded end).()
end

def handle_in("send_stuff", %{} = m, socket) do
  m |&gt; to_struct(%Stuff{}) |&gt; something_with
end

defp something_with(%Stuff{} = stuff), do: ...
</code></pre>
<p>Or maybe I should just skip decoding/encoding and do something like this:</p>
<pre><code>// javascript
let data = { "json": JSON.stringify(actual_stuff) };
// push...

// elixir
def handle_in("send_stuff", %{"json" =&gt; json}, socket) do
  json |&gt; Poison.decode(%{as: %Stuff{}, keys: :atoms!}) |&gt; something_with
end
</code></pre>
<p>Any thoughts?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112788" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-112788" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112788"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="112789" data-post-id="112789">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="amnu3387" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  amnu3387
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Not sure you want to, but you could also use an ecto embedded schema definition, which would allow you to just cast the json into a “structure” (including type conversion)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112789" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-112789" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112789"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="112790" data-post-id="112790">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="i-n-g-m-a-r" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  i-n-g-m-a-r
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Surely ecto provides some very nice functionality that could be used in this context.<br>
Also I would never persist anything without using ecto, embedded or otherwise.</p>
<p>I was thinking about defining embedded modules with structs inside a module that uses Phoenix.Channel.<br>
The structs would tell me about the kind of stuff coming in and the function definitions would be short.<br>
So it’s more about clarity than anything else.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112790" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-112790" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112790"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="112791" data-post-id="112791">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="peerreynders" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/120/5826_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  peerreynders
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="i-n-g-m-a-r" data-post="18" data-topic="4543">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/c67d28/48.png" class="avatar"> i-n-g-m-a-r:</div>
<blockquote>
<p>Any thoughts?</p>
</blockquote>
</aside>
<p>I must be missing something - why does <code>Poison</code> have get involved in <code>to_struct</code>? I suspect it’s because you really want to use the <a href="https://github.com/devinus/poison/blob/master/lib/poison/decoder.ex#L20" rel="noopener nofollow ugc"><code>Poison.Decode.transform/2</code></a> functionality.</p>
<p>Given that the transform process relies on <a href="https://github.com/devinus/poison/blob/master/test/poison/decoder_test.exs#L112" rel="noopener nofollow ugc">empty structs (possibly nested)</a> I’d be tempted to just code the necessary boilerplate conversions without <code>Poison</code> - that code can go into a separate module from the <code>struct</code> definition. If there is an opportunity to eliminate the boilerplate in the future, great - otherwise <img src="https://forum.elixirforum.com/images/emoji/apple/man_shrugging.png?v=15" title=":man_shrugging:" class="emoji" alt=":man_shrugging:" loading="lazy" width="20" height="20"> and move on …</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112791" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-112791" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112791"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #20"></div>
  </section>
</div>
    <div class="postbit" id="112793" data-post-id="112793">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="i-n-g-m-a-r" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  i-n-g-m-a-r
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Well that’s because <code>handle_in</code> receives a map with string keys.<br>
<a href="https://forum.elixirforum.com/u/voughtdq" rel="nofollow">voughtdq</a> came up with Poison.decode .. as a nice solution (map to struct).<br>
Using Poison.decode eliminates the need for boilerplate code in the struct definition, yay!<br>
So that’s the only reason why I would want to use it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112793" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-get-struct-from-map-elixir/4543/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-112793" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112793"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/4543/load_more?page=3">Load more posts (14 remaining)</a>
</div></template></turbo-stream>