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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="shahryarjb" data-post="11" data-topic="57583">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/shahryarjb/48/39485_2.png" class="avatar"> shahryarjb:</div>
<blockquote>
<p><code>MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop.</code></p>
</blockquote>
</aside>
<p>vs what your code do:<br>
<code>:oop</code> → <code>"oop"</code> → <code>"Oop"</code> → <code>Elixir.Oop</code></p>
<p>Something in your code tries to reference this long module where it should <code>Oop</code> or you should do other concatenation (not with <code>Elixir</code>, but for example a parent module i.e. <code>__MODULE__</code>). Since I have shared small example I have concatenated said module with <code>Example</code> module.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="297615" 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/create-sub-module-with-atom-name/57583/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-297615" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="297615"
                     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="297617" data-post-id="297617">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="shahryarjb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/shahryarjb/120/39485_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  shahryarjb
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ahh, Thank you yes you are right</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defmacro sub_field(name, _type, opts \\ [], do: block) do
    ast = register_struct(block, opts)

    name =
      name
      |&gt; Atom.to_string()
      |&gt; Macro.camelize()
      |&gt; String.to_atom()

    %Macro.Env{module: mod} = __CALLER__
    module_name = Module.concat(mod, name)

    quote do
      defmodule unquote(module_name) do
        unquote(ast)
      end
    end
  end
</code></pre>
<p>but I think it just supports one level nested, because I use <code>__CALLER__</code></p>
<p>I think it does not support</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defmodule TestNestedStruct do
    use GuardedStruct

    guardedstruct do
      field(:title, String.t())
      field(:subject, String.t())

      sub_field(:oop, String.t(), enforce: true) do
        field(:title, String.t())
        field(:fam, String.t())
        sub_field(:sop, String.t(), enforce: true) do
          field(:title, String.t())
        end
      end

      field(:site, String.t())
    end
  end
</code></pre>
<p>so I wont be able to fix it I create another post for it. thank you</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="297617" 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/create-sub-module-with-atom-name/57583/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-297617" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="297617"
                     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="297619" data-post-id="297619">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="shahryarjb" data-post="13" data-topic="57583">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/shahryarjb/48/39485_2.png" class="avatar"> shahryarjb:</div>
<blockquote>
<p>but I think it just supports one level nested, because I use <code>__CALLER__</code></p>
</blockquote>
</aside>
<p><code>__CALLER__</code> looks good for me and there should not be any problem. As always just an example to check what you worry about:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyLib do
  defmacro my_macro(name, do: block) do
    module =
      name
      |&gt; Atom.to_string()
      |&gt; Macro.camelize()
      |&gt; String.to_atom()
      |&gt; then(&amp;Module.concat(__CALLER__.module, &amp;1))

    quote do
      defmodule unquote(module) do
        _ = unquote(block)
      end
    end
  end
end

defmodule A do
  import MyLib

  my_macro :b do
    my_macro :c do
      IO.inspect(__MODULE__)
    end
  end
end
</code></pre>
<p>The above code prints <code>A.B.C</code>. <img src="https://forum.elixirforum.com/images/emoji/apple/+1.png?v=15" title=":+1:" class="emoji" alt=":+1:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="297619" 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/create-sub-module-with-atom-name/57583/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-297619" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="297619"
                     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="297641" data-post-id="297641">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="shahryarjb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/shahryarjb/120/39485_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  shahryarjb
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi again <img src="https://forum.elixirforum.com/images/emoji/apple/rose.png?v=15" title=":rose:" class="emoji" alt=":rose:" loading="lazy" width="20" height="20"> , I have a question, when I move my module and macro inside a test, it does not work, I think inside test it takes a time to compile and the functions are called faster than the macro</p>
<h3><a name="p-297641-for-example-1" class="anchor" href="#p-297641-for-example-1" aria-label="Heading link" rel="nofollow"></a>for example:</h3>
<pre data-code-wrap="elixir"><code class="lang-elixir">  test "nested macro field" do
    defmodule TestNestedStruct do
      use GuardedStruct

      guardedstruct do
        field(:title, String.t())
        field(:subject, String.t())

        sub_field(:oop, String.t(), enforce: true) do
          field(:title, String.t())
          field(:fam, String.t())

          sub_field(:soos, String.t(), enforce: true) do
            field(:fam, String.t())
          end
        end

        field(:site, String.t())
      end
    end

    IO.inspect(TestNestedStruct.__struct__())
    IO.inspect(TestNestedStruct.Oop.__struct__())
    IO.inspect(TestNestedStruct.Oop.__info__(:functions))
    IO.inspect(TestNestedStruct.Oop.Soos.__struct__())
    IO.inspect(TestNestedStruct.Oop.Soos.__info__(:functions))
    IO.inspect(TestNestedStruct.keys())

    assert %TestNestedStruct.Oop{
             fam: nil,
             title: nil
           } = TestNestedStruct.Oop.__struct__()
  end
</code></pre>
<p>The error:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">error: MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop.__struct__/0 is undefined, cannot expand struct MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
  test/guarded_struct_test.exs:542: MishkaDeveloperToolsTest.GuardedStructTest."test nested macro field"/1
</code></pre>
<hr>
<h3><a name="p-297641-but-if-i-move-the-module-out-of-test-macro-it-works-like-2" class="anchor" href="#p-297641-but-if-i-move-the-module-out-of-test-macro-it-works-like-2" aria-label="Heading link" rel="nofollow"></a>But if I move the module out of test macro, it works like:</h3>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defmodule TestNestedStruct do
    use GuardedStruct

    guardedstruct do
      field(:title, String.t())
      field(:subject, String.t())

      sub_field(:oop, String.t(), enforce: true) do
        field(:title, String.t())
        field(:fam, String.t())

        sub_field(:soos, String.t(), enforce: true) do
          field(:fam, String.t())
        end
      end

      field(:site, String.t())
    end
  end

  test "nested macro field" do
    IO.inspect(TestNestedStruct.__struct__())
    IO.inspect(TestNestedStruct.Oop.__struct__())
    IO.inspect(TestNestedStruct.Oop.__info__(:functions))
    IO.inspect(TestNestedStruct.Oop.Soos.__struct__())
    IO.inspect(TestNestedStruct.Oop.Soos.__info__(:functions))
    IO.inspect(TestNestedStruct.keys())

    assert %TestNestedStruct.Oop{
             fam: nil,
             title: nil
           } = TestNestedStruct.Oop.__struct__()
  end
</code></pre>
<h4><a name="p-297641-if-i-put-the-output-of-__struct__-inside-a-variable-it-print-the-output-but-it-shows-me-this-warning-3" class="anchor" href="#p-297641-if-i-put-the-output-of-__struct__-inside-a-variable-it-print-the-output-but-it-shows-me-this-warning-3" aria-label="Heading link" rel="nofollow"></a>If i put the output of  <code>__struct__</code> inside a variable, it print the output but it shows me this warning</h4>
<pre data-code-wrap="elixir"><code class="lang-elixir">....warning: MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop.__struct__/0 is undefined (module MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop is not available or is yet to be defined)
  test/guarded_struct_test.exs:541: MishkaDeveloperToolsTest.GuardedStructTest."test nested macro field"/1
</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="297641" 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/create-sub-module-with-atom-name/57583/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-297641" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="297641"
                     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="297643" data-post-id="297643">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="shahryarjb" data-post="15" data-topic="57583">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/shahryarjb/48/39485_2.png" class="avatar"> shahryarjb:</div>
<blockquote>
<p><code>.__struct__())</code></p>
</blockquote>
</aside>
<p>It’s not a part of public <code>API</code> and therefore no answer guarantees you a working solution using it. Why cant you use <code>%module{…}</code> notation?</p>
<aside class="quote no-group" data-username="shahryarjb" data-post="15" data-topic="57583">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/shahryarjb/48/39485_2.png" class="avatar"> shahryarjb:</div>
<blockquote>
<p>when I move my module and macro inside a test, it does not work</p>
</blockquote>
</aside>
<p>First of all why you do this? Much more common is to place it in <code>test/fixtures/my_fixture.ex</code> file …</p>
<p>There was some talk about this issue … I think it was about <code>Elixir</code> scripts i.e. <code>exs</code> files called like <code>elixir my_script.exs</code>. There was a question I think about <code>ecto</code>’s macros not working on same level where module is defined, but working inside some function … Somebody from <code>Elixir</code>’s core team (most probably <code>José Valim</code>) commented it and that it’s known limitation.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="297643" 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/create-sub-module-with-atom-name/57583/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-297643" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="297643"
                     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>