TypedStructEctoChangeset (typed_struct_ecto_changeset v1.1.0)

A TypedStruct plugin to integrate ecto changeset, that lets you use a typedstruct module as an Ecto schema module when casting changesets.

Embeds and assoc are not yet supported, but you can use Ecto.Type implementation instead

The plugin works by generating a __changeset__/0 function in the invoking module, which is called by Ecto.Changeset.cast/3 to cast types.

examples

Examples

If this module is defined:

defmodule TypedStructModule do
  use TypedStruct

  typedstruct do
    plugin TypedStructEctoChangeset

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

Then we can:

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

notes

Notes

Supports as many Ecto types as possible including :decimal, :date, :time, :datetime, from their corresponding typespecs Decimal.t(), etc.

Also converts any(), term() or map() types in field specifications to :map, and list types, like [integer()] to e.g. {:array, :integer}.

You can supply a :usec_times option to the plugin. If the option is true, then Time.t(), DateTime.t() and NaiveDateTime.t() fields will produce the Ecto types :time_usec, :datetime_usec and naive_datetime_usec.

For example:

defmodule TypedStructModule do
  use TypedStruct

  typedstruct do
    plugin TypedStructEctoChangeset, usec_times: true

    field :time_with_usec, Time.t()
    field :updated_at_with_usec, NaiveDateTime.t()
  end
end

Link to this section Summary

Link to this section Functions

Link to this function

field(name, type, opts, env)

Callback implementation for TypedStruct.Plugin.field/4.