View Source Icon.Schema.Type behaviour (ICON 2.0 SDK v0.1.1)

This module defines a behaviour for schema types.

This types are compatible with Icon.Schema defined schemas.

Behaviour

The behaviour is simplified version of Ecto.Type. The only callbacks to implement are:

  • load/1 for loading the data from ICON 2.0 protocol format.
  • dump/1 for dumping the data into ICON 2.0 protocol format.

e.g. we can implement an ICON 2.0 boolean as follows:

defmodule Bool do
  use Icon.Schema.Type

  @impl Icon.Schema.Type
  def load("0x0"), do: {:ok, false}
  def load("0x1"), do: {:ok, true}
  def load(_), do: :error

  @impl Icon.Schema.Type
  def dump(false), do: {:ok, "0x0"}
  def dump(true), do: {:ok, "0x1"}
  def dump(_), do: :error
end

Delegated type

Sometimes we need want to have an alias for a type for documentation purposes. That can be accomplished by delegating the callbacks to another type e.g. if we want to highlight an :integer is in loop (1 ICX = 10¹⁸ loop), we can do the following:

defmodule Loop do
  use Icon.Schema.Type, delegate_to: Icon.Schema.Types.Integer
end

Link to this section Summary

Callbacks

Callback for dumping the Elixir type into external type.

Callback for loading the external type into Elixir type.

Functions

Uses the Icon.Schema.Type behaviour.

Dumps a type from some value using a module.

It's the same as dump/2 but it raises when the value is not valid.

Loads a type from some value using a module.

It's the same as load/2 but it raises when the value is not valid.

Helper function to convert a map with binary keys to a map with atom keys.

Link to this section Callbacks

Specs

dump(any()) :: {:ok, any()} | :error

Callback for dumping the Elixir type into external type.

Specs

load(any()) :: {:ok, any()} | :error

Callback for loading the external type into Elixir type.

Link to this section Functions

Link to this macro

__using__(options)

View Source (macro)

Specs

__using__(any()) :: Macro.t()

Uses the Icon.Schema.Type behaviour.

Specs

dump(module(), any()) :: {:ok, any()} | :error

Dumps a type from some value using a module.

Specs

dump!(module(), any()) :: any()

It's the same as dump/2 but it raises when the value is not valid.

Specs

load(module(), any()) :: {:ok, any()} | :error

Loads a type from some value using a module.

Specs

load!(module(), any()) :: any()

It's the same as load/2 but it raises when the value is not valid.

Specs

to_atom_map(map() | any()) :: map()

Helper function to convert a map with binary keys to a map with atom keys.