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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I tend to use Ecto and embedded schemas for map → struct and boundary validation like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyApp.MyContext do
  def do_the_thing(params \\ %{}) when is_map(params) do
    with {:ok, command} &lt;- MyApp.MyContext.Commands.DoTheThing.new(params) do
      # fun business logic here
    end
  end
end

defmodule MyApp.MyContext.Commands.DoTheThing do
  use Ecto.Schema
  import Ecto.Changeset

  embedded_schema do
    field :field_1, :string
    field :field_2, :integer
  end

  def new(params) do
    command = changeset(params)
    case command.valid? do
      true  -&gt; {:ok, apply_changes(command)}
      false -&gt; {:error, command.errors}
    end
  end

  defp changeset(params) do
    %__MODULE__{}
    |&gt; cast(params, [:field_1, :field_2])
    |&gt; validate_required([:field_1, :field_2])
  end
end
</code></pre>
<p>It doesn’t feel so dirty to do this since Ecto 3.0 and the <code>:ecto_sql</code> separation.</p>
<p>I’ve found this to be a good way to accept large form inputs - maybe not as practical for smaller sets of parameters.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112798" data-batch-url="/posts/batch_likers">
                        2
                      </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/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-112798" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112798"
                     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 #22"></div>
  </section>
</div>
    <div class="postbit" id="112896" data-post-id="112896">
  <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, I understand your point, it’s probably better to use something solid like ecto.<br>
Your <code>new</code> function is similar to initializing a struct in Go, is this typical also for Elixir?</p>
<p>Most of my channel functions accept just a few parameters and <code>is_binary</code> is fine most of the time.<br>
I just don’t like defining the map as an argument and all of the guards; it doesn’t look nice.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112896" 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/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-112896" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112896"
                     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 #23"></div>
  </section>
</div>
    <div class="postbit" id="112910" data-post-id="112910">
  <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>still a little bit dirty <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<pre><code>@primary_key false
embedded_schema do ...
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112910" 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/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-112910" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112910"
                     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 #24"></div>
  </section>
</div>
    <div class="postbit" id="112938" data-post-id="112938">
  <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><a class="mention" href="/u/mrdoops" rel="nofollow">@MrDoops</a> suppose I would really like to embed my <code>structures</code> in my channel modules,<br>
and I wouldn’t want to define <code>new</code> and <code>changeset</code> functions for all of my embeds.<br>
Would it be crazy to do something like this:</p>
<pre><code>defmodule MyApp.SomeChannel do
  use Phoenix.Channel
  
  defmodule SigninData do
    use Ecto.Schema
    use MyApp.Macros.Structure
      
    @primary_key false
    embedded_schema do
      field :email, :string
      field :password, :string
    end
  end
  
  ...
  
  def handle_in("signin", %{} = params, socket) do
    params
    |&gt; SigninData.new
    |&gt; ... authenticate ...
    |&gt; ... reply ...
  end
end
</code></pre>
<p>using this macro:</p>
<pre><code>defmodule MyApp.Macros.Structure do
  import Ecto.Changeset
  
  defmacro __using__(_opts) do
    quote do
      def new(params) do
        params |&gt; MyApp.Macros.Structure.new(unquote(__CALLER__.module))
      end
    end
  end
  
  def new(params, module) when is_map(params), do: params |&gt; changeset(module) |&gt; structure
  def new(_params, _module), do: {:error, :invalid}
  
  defp changeset(params, module) do
    with fields &lt;- module.__schema__(:fields) do
      struct(module)
      |&gt; cast(params, fields)
      |&gt; validate_required(fields)
    end         
  end
  
  defp structure(%Ecto.Changeset{valid?: true} = changeset), do: {:ok, apply_changes(changeset)}
  defp structure(%Ecto.Changeset{} = changeset), do: {:error, changeset.errors}
  defp structure(_noop), do: {:error, :unprocessable_entity}
end
</code></pre>
<p>?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112938" data-batch-url="/posts/batch_likers">
                        2
                      </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/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-112938" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112938"
                     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 #25"></div>
  </section>
</div>
    <div class="postbit" id="112940" data-post-id="112940">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It’s mostly a subjective thing, but I prefer not to have the Phoenix / Web layer know how Identity / Authentication is implemented. I’d rather do something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def handle_in("signin", %{} = params, socket) do
  case Identity.authenticate(params) do
     {:ok, ...} -&gt; ...
     {:error, ...} -&gt; ...
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112940" 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/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-112940" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112940"
                     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 #26"></div>
  </section>
</div>
    <div class="postbit" id="112943" data-post-id="112943">
  <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>Fair point, I would actually also delegate the actual authentication process.<br>
I was trying to better define the channel api.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="112943" 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/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-112943" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="112943"
                     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 #27"></div>
  </section>
</div>
    <div class="postbit" id="113410" data-post-id="113410">
  <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>I’m looking into Absynthe / GraphQL to better define my api.<br>
It seems to make the structs inside channels thing irrelevant and provides a ton of functionality like user input validation.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="113410" data-batch-url="/posts/batch_likers">
                        2
                      </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/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-113410" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="113410"
                     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 #28"></div>
  </section>
</div>
    <div class="postbit" id="248212" data-post-id="248212">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Not sure if this is exactly your problem, but since I was searching for "How to convert a map to a User struct, and this topic popped up, I thought that I would add what I eventually did that worked perfectly:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Map.merge(%User{}, map_defaults)
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="248212" 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/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-248212" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="248212"
                     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 #29"></div>
  </section>
</div>
    <div class="postbit" id="248223" data-post-id="248223">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is dangerous as it doesn’t check that the keys in <code>map_defaults</code> actually correspond to keys of the struct. You may end up with a broken struct which is annoying to debug. What you should do is:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">s = struct!(User, map_defaults)
</code></pre>
<p>This will ensure that keys that are in <code>@enforce_keys</code> must be exist and there are no keys that don’t exist in the struct. Otherwise it will raise an error. There is also <code>Kernel.struct/2</code> (i.e. without exlamation mark) that will just discard unknown keys and won’t enforce <code>@enforce_keys</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="248223" data-batch-url="/posts/batch_likers">
                        4
                      </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/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-248223" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="248223"
                     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 #30"></div>
  </section>
</div>
    <div class="postbit" id="248239" data-post-id="248239">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Much better yes. I didn’t know this one. I wonder why the web searches didn’t pull this up, if it was that easy to do.  First I’ve heard of ‘struct/2’ thanks!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="248239" 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/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-248239" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="248239"
                     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 #31"></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=4">Load more posts (4 remaining)</a>
</div></template></turbo-stream>