Hello, I am developing a macro to create nested struct in a module or (module → module …), I took the original code from this repo, because it had been abandoned and I want many options for validation etc, I move it inside my project, you can see it in this link.
The problem:
To create a nested struct, I create a new module in one module. For example (A.B.C) is made. Since struct B cannot be called in A at compile time, I just want to create a field named module in its parent struct.
But it seems that the parent has already been compiled and does not allow me to do this.
Please see this part of code:
in this part I create another module, but how can define a filed which struct type with options
defmodule A do
defstruct name: "", :b
defmodule B do
defstruct name: ""
end
end
I tried to use @before_compile {__CALLER__.module, :add_parent_field_of_sub_field}
to add module attribute
like using this
== Compilation error in file lib/macros/guarded_struct.ex ==
** (ArgumentError) cannot set attribute @before_compile inside function/macro
Even I use this macro inside sub_field
macro, to define a field, but it has no effect on my code
Example code I want finally:
defmodule TestNestedStruct do
use GuardedStruct
guardedstruct do
field(:title, String.t())
field(:subject, String.t())
sub_field(:oop, struct(), enforce: true) do
field(:title, String.t())
field(:fam, String.t())
sub_field(:soos, struct(), enforce: true) do
field(:fam, String.t())
end
field(:site, String.t())
end
field(:site, String.t())
end
end
If I create a field has same name with module name, I can check it inside my builder function.
I have no idea how can do it, thank you in advance