Construct v2.1.10 Construct.Type behaviour View Source

Type-coercion module, originally copied and modified from Ecto.Type and behaviour to implement your own types.

Defining custom types

defmodule CustomType do
  @behaviour Construct.Type

  def cast(value) do
    {:ok, value}
  end
end

Link to this section Summary

Functions

Behaves like cast/3, but without options provided to nested types

Casts a value to the given type

Checks if we have a primitive type

Returns typespec AST for given type

Returns typespec AST for given term

Callbacks

Casts the given input to the custom type

Link to this section Types

Link to this type builtin() View Source
builtin() ::
  :integer
  | :float
  | :boolean
  | :string
  | :binary
  | :pid
  | :reference
  | :decimal
  | :utc_datetime
  | :naive_datetime
  | :date
  | :time
  | :any
  | :array
  | {:array, t()}
  | :map
  | {:map, t()}
  | :struct
Link to this type cast_ret() View Source
cast_ret() :: {:ok, term()} | {:error, term()} | :error

Link to this section Functions

Link to this function cast(type, term) View Source
cast(t(), term()) :: cast_ret() | any()

Behaves like cast/3, but without options provided to nested types.

Link to this function cast(type, term, opts) View Source
cast(t(), term(), options) :: cast_ret() | any()
when options: [{:make_map, boolean()}]

Casts a value to the given type.

iex> cast(:any, "whatever")
{:ok, "whatever"}

iex> cast(:any, nil)
{:ok, nil}
iex> cast(:string, nil)
:error

iex> cast(:integer, 1)
{:ok, 1}
iex> cast(:integer, "1")
{:ok, 1}
iex> cast(:integer, "1.0")
:error

iex> cast(:float, 1.0)
{:ok, 1.0}
iex> cast(:float, 1)
{:ok, 1.0}
iex> cast(:float, "1")
{:ok, 1.0}
iex> cast(:float, "1.0")
{:ok, 1.0}
iex> cast(:float, "1-foo")
:error

iex> cast(:boolean, true)
{:ok, true}
iex> cast(:boolean, false)
{:ok, false}
iex> cast(:boolean, "1")
{:ok, true}
iex> cast(:boolean, "0")
{:ok, false}
iex> cast(:boolean, "whatever")
:error

iex> cast(:string, "beef")
{:ok, "beef"}
iex> cast(:binary, "beef")
{:ok, "beef"}

iex> cast(:decimal, Decimal.new(1.0))
{:ok, Decimal.new(1.0)}
iex> cast(:decimal, Decimal.new("1.0"))
{:ok, Decimal.new(1.0)}
iex> cast(:decimal, 1.0)
{:ok, Decimal.new(1.0)}
iex> cast(:decimal, "1.0")
{:ok, Decimal.new(1.0)}

iex> cast({:array, :integer}, [1, 2, 3])
{:ok, [1, 2, 3]}
iex> cast({:array, :integer}, ["1", "2", "3"])
{:ok, [1, 2, 3]}
iex> cast({:array, :string}, [1, 2, 3])
:error
iex> cast(:string, [1, 2, 3])
:error
Link to this function primitive?(type) View Source
primitive?(t()) :: boolean()

Checks if we have a primitive type.

iex> primitive?(:string)
true
iex> primitive?(Another)
false

iex> primitive?({:array, :string})
true
iex> primitive?({:array, Another})
true

iex> primitive?([Another, {:array, :integer}])
false

Returns typespec AST for given type

iex> spec([CommaList, {:array, :integer}]) |> Macro.to_string() “list(:integer)”

iex> spec({:array, :string}) |> Macro.to_string() “list(String.t())”

iex> spec({:map, CustomType}) |> Macro.to_string() “%{optional(term) => CustomType.t()}”

iex> spec(:string) |> Macro.to_string() “String.t()”

iex> spec(CustomType) |> Macro.to_string() “CustomType.t()”

Returns typespec AST for given term

iex> typeof(nil) |> Macro.to_string() “nil”

iex> typeof(1.42) |> Macro.to_string() “float()”

iex> typeof(“string”) |> Macro.to_string() “String.t()”

iex> typeof(CustomType) |> Macro.to_string() “CustomType.t()”

iex> typeof(&NaiveDateTime.utc_now/0) |> Macro.to_string() “NaiveDateTime.t()”

Link to this section Callbacks

Casts the given input to the custom type.