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/1for loading the data from ICON 2.0 protocol format.dump/1for 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 for dumping the Elixir type into external type.
Callback for loading the external type into Elixir type.
Link to this section 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.