EctoTypedSchema.TypeMapper (EctoTypedSchema v0.2.0)

Copy Markdown View Source

Maps Ecto schema field types to Elixir typespec AST.

Given an Ecto type (e.g. :string, {:array, :integer}, {:assoc, %Ecto.Association.Has{...}}), this module produces the corresponding quoted Elixir type for use in @type definitions.

Summary

Functions

Converts an Ecto field type into a quoted Elixir typespec.

Functions

to_elixir_type(ecto_type, opts \\ [])

@spec to_elixir_type(
  ecto_type(),
  keyword()
) :: Macro.t()

Converts an Ecto field type into a quoted Elixir typespec.

Recognised options

  • :type - When present, returned as-is (user-supplied type override).
  • :enum_values - List of atoms for Ecto.Enum fields; produces a union type (e.g. :active | :inactive).

All other keys in opts are ignored by this function but may be passed through for convenience.

Type Mapping

Ecto typeElixir typespec
:stringString.t()
:integerinteger()
:floatfloat()
:booleanboolean()
:binarybinary()
:bitstringbitstring()
:decimalDecimal.t()
:idinteger()
:binary_idEcto.UUID.t()
:mapmap()
:arraylist()
:dateDate.t()
:time / :time_usecTime.t()
:naive_datetime / :naive_datetime_usecNaiveDateTime.t()
:utc_datetime / :utc_datetime_usecDateTime.t()
:durationDuration.t()
{:map, inner}%{term() => inner_type}
{:array, inner}list(inner_type)
belongs_toEcto.Schema.belongs_to(Schema.t())
has_oneEcto.Schema.has_one(Schema.t())
has_manyEcto.Schema.has_many(Schema.t())
many_to_manyEcto.Schema.many_to_many(Schema.t())
embeds_oneSchema.t()
embeds_manylist(Schema.t())
Ecto.Enumunion of atom values or atom()
Other moduleModule.t()

Returns

A quoted expression representing the Elixir typespec.

Raises ArgumentError for unsupported types.