Tyyppi.Struct (tyyppi v0.3.1) View Source

Creates the typed struct with spec bound to each field.

Usage

See Tyyppi.Example 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{}
%Tyyppi.Example{
  bar: :erlang.list_to_pid('<0.0.0>'),
  baz: {:error, :reason}, foo: :default}

Upserts

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

Access

iex> pid = :erlang.list_to_pid('<0.0.0>')
iex> ex = %Tyyppi.Example{foo: :foo, bar: {:ok, pid}, baz: :ok}
iex> put_in(ex, [:foo], :foo_sna)
%Tyyppi.Example{
  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; reason: validation failed ({:foo, :type})

Link to this section Summary

Functions

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

Specs

put(target :: struct, key :: atom(), value :: any()) ::
  {:ok, struct} | {:error, any()}
when struct: %{__struct__: atom()}

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 | no_return()
when struct: %{__struct__: atom()}

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