Ecto TypedStruct plugin

Hello #{user.name} :D,

this is TypedStruct plugin to generate __changeset__ function on structure that can be used in ecto by Ecto.Changeset.cast method

I have phoenix app that does not use database and wanted some alternative to Ecto.Schema (also I’m using dialyzer extensiveli, which is + for ts) and in the end I found out that typedstruct plugin system is easiest way to go

You can add this plugin to your typedstruct definition

defmodule Sample do
  typedstruct do
      plugin TypedStructEctoChangeset

      field :age, integer()
      field :name, String.t()
  end
end

And then you can cast this struct using changeset

iex> Ecto.Changeset.cast(%Sample{}, %{"age" => 23, "name" => "John Doe"}, [:age, :name])
%Ecto.Changeset...

For now it missing support for embeds…

Thank’s for feedback

1 Like