2021-09-01 12:43:55 +00:00
|
|
|
defmodule ArchitexWeb.Request do
|
2021-08-26 12:32:24 +00:00
|
|
|
import Ecto.Changeset
|
|
|
|
|
|
|
|
alias Ecto.Changeset
|
|
|
|
|
|
|
|
@spec parse(module(), map()) :: {:ok, struct()} | {:error, Changeset.t()}
|
|
|
|
def parse(module, params) do
|
|
|
|
case apply(module, :changeset, [struct(module), params]) do
|
|
|
|
%Ecto.Changeset{valid?: true} = cs -> {:ok, apply_changes(cs)}
|
|
|
|
cs -> {:error, cs}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmacro __using__(_opts) do
|
|
|
|
quote do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
|
|
|
@spec parse(map()) :: {:ok, struct()} | {:error, Changeset.t()}
|
|
|
|
def parse(params) do
|
2021-09-01 12:43:55 +00:00
|
|
|
ArchitexWeb.Request.parse(__MODULE__, params)
|
2021-08-26 12:32:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|