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

This module defines a behaviour for schema types.

This types are compatible with Icon.Schema defined schemas.

behaviour

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

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.

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

Dumps a type from some value using a module.

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

Loads a type from some value using a module.

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

Link to this section Callbacks

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

Callback for dumping the Elixir type into external type.

@callback 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)
@spec __using__(any()) :: Macro.t()

Uses the Icon.Schema.Type behaviour.

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

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

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

Dumps a type from some value using a module.

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

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

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

Loads a type from some value using a module.

@spec to_atom_map(map() | any()) :: map()

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