-
Notifications
You must be signed in to change notification settings - Fork 10
Description
Hello,
I just discovered exceptional
and I see you're trying to introduce a few ideas from Haskell to Elixir, which is nice. With that given said, I was wondering if you could give me a few ideas on something I'm trying to accomplish.
I'm currently working with Phoenix and I was thinking about creating a library that will allow me to write my controllers using the following flow:
defmodule AuthController do
# --- types -----------------------------
defmodule SMS do
defstruct code: nil,
end
defmodule Flow do
defstruct conn: nil, params: nil, data: nil
end
# --- controller methods -----------------------------
def authenticate(conn, params) do
flow = %Flow{conn: conn, params: params, data: %{}}
flow
|> initialize(Convertion.params_to_sms_struct)
# The following steps will apply a series
# of transformations to flow.data. When a step fails
# the pipeline will be stopped and the error will be
# written to conn
|> success(Data.sanitize) # Sanitize data
|> otherwise("bad data") # Stop, write error to conn
|> success(Auth.Verify.sms?) # Verify match in DB
|> otherwise("invalid code") # Stop, write error to conn
# All steps were successful, write final output
|> finish(:json, fn flow ->
conn |> json(%{token: flow.data.token})
end)
end
end
The main reason I want to do this is because it improves readability, and allows one to think about the flow as a series of linear transformations.
What do you think could be a good approach? My intuition tells me a monad would do the trick, but I must admit, I'm not extremely familiar with monads, monoids and the likes. So I'm still exploring possible solutions, and also learning. (:
Thank you!