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
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217
Let’s say I have a map with required and optional k...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
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
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New








