ntd23
Get types from struct in compilation time
-
Is there way to get the types from struct defined as bellow, and generate functions based on your types; like
binary(),integer()? -
Can I get the keys of a struct that is marked as “enforced”?
So I have a struct created using TypedStruct like this:
defmodule MyStruct do
use TypedStruct
typedstruct do
field :a, binary(), enforce: true
field :b, integer()
# ....
end
end
And I want generate validation functions/macro of Vex for each field based on your type and if is enforced or not, like this:
defmodule Checker do
defmacro __usign__(opts) do
quote do
[{field, type, enforced?} | _] = my_fields = some_way_to_get_this_types()
for {field, type, _enforced?} <- my_fields do
case type do
"binary()" -> validates(field, by: [function: &is_binary/1, message: "not binary"])
"integer()" -> validates(field, by: [function: &is_integer/1, message: "not integer"])
end
end
end
end
end
The result of use Checker will be a bunch of validates injected on module, like this:
defmodule MyStruct do
use TypedStruct
use Checker
typedstruct do
field(:a, binary(), enforce: true)
field(:b, binary())
# ....
end
# this will be injected by the `use Checker`
validates(:a, by: [function: &is_binary/1, message: "not binary"])
validates(:b, by: [function: &is_integer/1, message: "not integer"])
# ... and so on
end
In my research I find this, but I dont know hot to deal with this “type tree” to generate types of like MyOtherStruct.t()
Most Liked
Popular in Questions
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New







