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
Last Post!
ntd23
Popular in Questions
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
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
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Other popular topics
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









