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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here is another take on the topic</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MapFromDeepStruct do
  def from_deep_struct(%{} = map), do: convert(map)

  defp convert(data) when is_struct(data) do
    data |&gt; Map.from_struct() |&gt; convert()
  end

  defp convert(data) when is_map(data) do
    for {key, value} &lt;- data, reduce: %{} do
      acc -&gt;
        case key do
          :__meta__ -&gt;
            acc

          other -&gt;
            Map.put(acc, other, convert(value))
        end
    end
  end

  defp convert(other), do: other
end
</code></pre>
<p>The tests are here <a href="https://gist.github.com/mpugach/2d264c063db9bc91af0f7c7ec8175037" class="inline-onebox" rel="noopener nofollow ugc">Convert deeply nested Elixir struct into map · GitHub</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="210646" 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/convert-a-nested-struct-into-a-nested-map/23814/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-210646" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="210646"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="231909" data-post-id="231909">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Another option is to use <a href="https://hex.pm/packages/nestru" rel="nofollow">Nestru</a>. It supports transformation in both ways. Encoding from nested struct into a map can be like the following.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Nadia.Model.Chat do
  @derive Nestru.Encoder
  defstruct [:first_name, :id, :last_name, :photo, :title, :type, :username]
end

defmodule Nadia.Model.User do
  @derive Nestru.Encoder
  defstruct [:first_name, :id, :last_name, :username]
end

defmodule Nadia.Model.Message do
  @derive Nestru.Encoder
  defstruct [
    :audio,
    :caption,
    :channel_chat_created,
    :chat,
    :contact,
    :date,
    :delete_chat_photo,
    :document,
    :edit_date,
    :entities,
    :forward_date,
    :forward_from,
    :forward_from_chat,
    :from,
    :group_chat_created,
    :left_chat_member,
    :location,
    :message_id,
    :migrate_from_chat_id,
    :migrate_to_chat_id,
    :new_chat_member,
    :new_chat_photo,
    :new_chat_title,
    :photo,
    :pinned_message,
    :reply_to_message,
    :sticker,
    :supergroup_chat_created,
    :text,
    :venue,
    :video,
    :voice
  ]
end

struct = %{
  callback_query: nil,
  channel_post: nil,
  chosen_inline_result: nil,
  edited_message: nil,
  inline_query: nil,
  message: %Nadia.Model.Message{
    audio: nil,
    caption: nil,
    channel_chat_created: nil,
    chat: %Nadia.Model.Chat{
      first_name: "Christian",
      id: 543211234,
      last_name: "Tovar",
      photo: nil,
      title: nil,
      type: "private",
      username: "ChristianTovar"
    },
    contact: nil,
    date: 1562605521,
    delete_chat_photo: nil,
    document: nil,
    edit_date: nil,
    entities: nil,
    forward_date: nil,
    forward_from: nil,
    forward_from_chat: nil,
    from: %Nadia.Model.User{
      first_name: "Christian",
      id: 543211234,
      last_name: "Tovar",
      username: "ChristianTovar"
    },
    group_chat_created: nil,
    left_chat_member: nil,
    location: nil,
    message_id: 714,
    migrate_from_chat_id: nil,
    migrate_to_chat_id: nil,
    new_chat_member: nil,
    new_chat_photo: [],
    new_chat_title: nil,
    photo: [],
    pinned_message: nil,
    reply_to_message: nil,
    sticker: nil,
    supergroup_chat_created: nil,
    text: "google meets",
    venue: nil,
    video: nil,
    voice: nil
  },
  update_id: 412827321
}
</code></pre>
<p>encoding looks like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; Nestru.to_map(struct)
{:ok,
 %{
   callback_query: nil,
   channel_post: nil,
   chosen_inline_result: nil,
   edited_message: nil,
   inline_query: nil,
   message: %{
     audio: nil,
     caption: nil,
     channel_chat_created: nil,
     chat: %{
       first_name: "Christian",
       id: 543211234,
       last_name: "Tovar",
       photo: nil,
       title: nil,
       type: "private",
       username: "ChristianTovar"
     },
     contact: nil,
     date: 1562605521,
     delete_chat_photo: nil,
     document: nil,
     edit_date: nil,
     entities: nil,
     forward_date: nil,
     forward_from: nil,
     forward_from_chat: nil,
     from: %{first_name: "Christian", id: 543211234, last_name: "Tovar", username: "ChristianTovar"},
     group_chat_created: nil,
     left_chat_member: nil,
     location: nil,
     message_id: 714,
     migrate_from_chat_id: nil,
     migrate_to_chat_id: nil,
     new_chat_member: nil,
     new_chat_photo: [],
     new_chat_title: nil,
     photo: [],
     pinned_message: nil,
     reply_to_message: nil,
     sticker: nil,
     supergroup_chat_created: nil,
     text: "google meets",
     venue: nil,
     video: nil,
     voice: nil
   },
   update_id: 412827321
 }}
</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="231909" 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/convert-a-nested-struct-into-a-nested-map/23814/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-231909" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="231909"
                     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="241823" data-post-id="241823">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Does anyone know if Jason can do what the poison example or nestru does?</p>
<p>Looks like yes</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://jason.hexdocs.pm/Jason.Encoder.html">
  <header class="source">

      <a href="https://jason.hexdocs.pm/Jason.Encoder.html" target="_blank" rel="noopener nofollow ugc">jason.hexdocs.pm</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://jason.hexdocs.pm/Jason.Encoder.html" target="_blank" rel="noopener nofollow ugc">Jason.Encoder — jason v1.4.5</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="241823" 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/convert-a-nested-struct-into-a-nested-map/23814/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-241823" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="241823"
                     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="264396" data-post-id="264396">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I needed this functionality recently; thank you for writing it!</p>
<p>I also needed to account for lists, so I added:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp convert(data) when is_list(data) do
  Enum.map(data, fn item -&gt;
    convert(item)
  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="264396" 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/convert-a-nested-struct-into-a-nested-map/23814/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-264396" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="264396"
                     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="264403" data-post-id="264403">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Still feels a bit verbose</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def convert(data) when is_struct(data) do
  data |&gt; Map.from_struct |&gt; convert
end

def convert(data) when is_map(data) do
  Map.new(data, fn {k, v} -&gt; {k, convert(v)})
end

def convert(data) when is_list(data) do
  Enum.map(data, &amp;convert/1)
end

def convert (data), do: data
</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="264403" 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/convert-a-nested-struct-into-a-nested-map/23814/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-264403" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="264403"
                     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="340789" data-post-id="340789">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is extremely frustrating. I have a struct with struct fields and other varied fields like regexes and maps and lists and tuples.</p>
<p>I have tried jason and nestru. Both render things differently across elixir version.</p>
<p>Do i really need to handcraft the struct to json/string representation?</p>
<p>for context working on</p>
<p><a href="https://github.com/testcontainers/testcontainers-elixir/pull/125" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/testcontainers/testcontainers-elixir/pull/125</a></p>
<p>And its extremely hard to get tests to pass in CICD. Everything can be green locally on my newer elixir version 1.17 .. but in github actions tests fails beause struct hash is different</p>
<p>bah…</p>
<p>wierd thing is that hash tests passes on older elixir versions …  i think its at least map field ordering issue.</p>
<p>I mean how hard can it be</p>
<p>hmmmm … otp 26 …</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://fly.io/phoenix-files/taking-control-of-map-sort-order-in-elixir/">
  <header class="source">
      <img src="https://fly.io/static/images/favicon/favicon.ico" class="site-icon" alt="" width="48" height="48">

      <a href="https://fly.io/phoenix-files/taking-control-of-map-sort-order-in-elixir/" target="_blank" rel="noopener nofollow ugc">Fly</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/392;"><img src="https://fly.io/phoenix-files/taking-control-of-map-sort-order-in-elixir/assets/sorting-maps-in-iex-cover.webp" class="thumbnail" alt="" width="690" height="392"></div>

<h3><a href="https://fly.io/phoenix-files/taking-control-of-map-sort-order-in-elixir/" target="_blank" rel="noopener nofollow ugc">Taking Control of Map Sort Order in Elixir</a></h3>

  <p>The home for Phoenix-oriented content ranging from Ecto to LiveView and more.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>hmmmmm</p>
<p>ref <a href="https://forum.elixirforum.com/t/blog-post-taking-control-of-map-sort-order-in-elixir/55939" class="inline-onebox" rel="nofollow">Blog Post: Taking Control of Map Sort Order in Elixir</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="340789" 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/convert-a-nested-struct-into-a-nested-map/23814/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-340789" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="340789"
                     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="340791" data-post-id="340791">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>ok, solved with the MapFromDeepStruct which i shamelessly stole and transformed to a ListFromDeepStruct. Thing is, maps ordering are unstable. Lists not. Then i do inspect(list) and hash 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="340791" 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/convert-a-nested-struct-into-a-nested-map/23814/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-340791" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="340791"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>