Tyyppi.Struct (tyyppi v0.11.8) View Source

Creates the typed struct with spec bound to each field.

Usage

See Tyyppi.Example.Struct for the example of why and how to use Tyyppi.Struct.

Example

iex> defmodule MyStruct do
...>   @type my_type :: :ok | {:error, term()}
...>   Tyyppi.Struct.defstruct foo: atom(), bar: GenServer.on_start(), baz: my_type()
...> end
iex> types = MyStruct.types()
...> types[:foo]
%Tyyppi.T{
  definition: {:type, 0, :atom, []},
  module: nil,
  name: nil,
  params: [],
  source: nil,
  type: :built_in
}
...> types[:baz]
%Tyyppi.T{
  definition: {:type, 0, :union, [
    {:atom, 0, :ok},
    {:type, 0, :tuple, [
      {:atom, 0, :error}, {:type, 0, :term, []}]}]},
  module: Test.Tyyppi.Struct.MyStruct,
  name: :my_type,
  params: [],
  quoted: {{:., [], [Test.Tyyppi.Struct.MyStruct, :my_type]}, [], []},
  source: :user_type,
  type: :type
}

Defaults

Since there is no place for default values in the struct declaration, where types are first class citizens, defaults might be specified through @defaults module attribute. Omitted fields there will be considered having nil default value.

iex> %Tyyppi.Example.Struct{}
%Tyyppi.Example.Struct{
  bar: {:ok, :erlang.list_to_pid('<0.0.0>')}, baz: {:error, :reason}, foo: nil}

Upserts

iex> {ex, pid} = {%Tyyppi.Example.Struct{}, :erlang.list_to_pid('<0.0.0>')}
iex> Tyyppi.Example.Struct.update(ex, foo: :foo, bar: {:ok, pid}, baz: :ok)
{:ok, %Tyyppi.Example.Struct{
  bar: {:ok, :erlang.list_to_pid('<0.0.0>')},
  baz: :ok,
  foo: :foo}}
iex> Tyyppi.Example.Struct.update(ex, foo: :foo, bar: {:ok, pid}, baz: 42)
{:error, [baz: [type: [expected: "Tyyppi.Example.Struct.my_type()", got: 42]]]}

Access

iex> pid = :erlang.list_to_pid('<0.0.0>')
iex> ex = %Tyyppi.Example.Struct{foo: :foo, bar: {:ok, pid}, baz: :ok}
iex> put_in(ex, [:foo], :foo_sna)
%Tyyppi.Example.Struct{
  bar: {:ok, :erlang.list_to_pid('<0.0.0>')},
  baz: :ok,
  foo: :foo_sna}
iex> put_in(ex, [:foo], 42)
** (ArgumentError) could not put/update key :foo with value 42 ([foo: [type: [expected: "atom()", got: 42]]])

Link to this section Summary

Functions

Declares a typed struct. The accepted argument is the keyword of {field_name, type} tuples. See Tyyppi.Example.Struct for an example.

Puts the value to target under specified key, if passes validation

Puts the value to target under specified key, if passes validation, raises otherwise

Updates the value in target under specified key, if passes validation

Updates the value in target under specified key, if passes validation, raises otherwise

Link to this section Functions

Link to this macro

defstruct(definition)

View Source (macro)

Declares a typed struct. The accepted argument is the keyword of {field_name, type} tuples. See Tyyppi.Example.Struct for an example.

Specs

put(target :: struct, key :: atom(), value :: any()) ::
  {:ok, struct} | {:error, keyword()}
when struct: %{required(atom()) => any()}

Puts the value to target under specified key, if passes validation

Link to this function

put!(target, key, value)

View Source

Specs

put!(target :: struct, key :: atom(), value :: any()) :: struct
when struct: %{required(atom()) => any()}

Puts the value to target under specified key, if passes validation, raises otherwise

Link to this function

update(target, key, fun)

View Source

Specs

update(target :: struct, key :: atom(), updater :: (any() -> any())) ::
  {:ok, struct} | {:error, any()}
when struct: %{__struct__: atom()}

Updates the value in target under specified key, if passes validation

Link to this function

update!(target, key, fun)

View Source

Specs

update!(target :: struct, key :: atom(), updater :: (any() -> any())) ::
  struct | no_return()
when struct: %{__struct__: atom()}

Updates the value in target under specified key, if passes validation, raises otherwise