ilya-kolpakov

ilya-kolpakov

Generating structs from constructor functions

Suppose one wants to have a bunch of structs that represent API requests. Requests have parameters and it should be possible to validate parameters when constructing the structs. This use case would be completely covered if one could specify a structure by simply providing an appropriate (validating) constructor.

Here is is somewhat contrived example:

defmodule Orders do
  import SuperStruct # hypothetical
  defsstruct market(id, quantity, type, exchange \\ nil, fill_or_kill \\ false)
  when is_binary(id) and is_int(quantity) and type in [:buy, :sell]
  and (is_binary(exchange) or is_nil(exchange)) and is_boolean(fill_or_kill)

  defsstruct limit(id, quantity, type, price, exchange \\ nill, valid_until \\ nil) #...
end 

# Create an order

# Would be nice to be able to use keyword syntax for all params
order = Orders.Market.new(id: "GOOG", quantity: 100, type: :buy)

# Processing code

case order do
    %Orders.Market{} -> # market order
    %Orders.Limit{} -> # limit order
end
def process(%Orders.Market{}=o), do: #...
def process(%Orders.Limit{}=o), do: #...

It looks like it should be possible to come up with macros that would parse the signatures and generate the appropriate struct modules (e.g. modules having custom __struct__ methods). I am wondering if there is a library for doing something similar or maybe someone looked into something similar in the past.

I briefly looked at construct, exconstructor and struct_constructor packages and I think their focus is slightly different. It looks like they all provide nice field definition macros but do not aim at parsing/repackaging constructors. Clearly, there are pros and cons to both approaches.

First Post!

axelson

axelson

Scenic Core Team

I think you’re looking for a library such as params by @vic. It helps you to validate that the parameters that you’re receiving are what you expect.

Where Next?

Popular in Discussions Top

laiboonh
Hi all, I am trying to convince my team to use liveview over the current react. What are some of the points where one should consider us...
New
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
hazardfn
I suppose this question is effectively hackney vs. ibrowse but we are at a point in our project where we have to make a choice between th...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
Owens
Hello all, I am developing a new mobile app with Flutter frontend and Phoenix backend. The mobile app has real-time task management and c...
New
Markusxmr
Since Drab has been developed for a while in the open, introducing the Liveview functionality in a way it happend appears to undermine th...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
josevalim
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
baxterw3b
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
romenigld
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement