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
Callback for dumping the Elixir type into external type.
Specs
Callback for loading the external type into Elixir type.
Link to this section Functions
Specs
Uses the Icon.Schema.Type
behaviour.
Specs
Dumps a type from some value
using a module
.
Specs
It's the same as dump/2
but it raises when the value
is not valid.
Specs
Loads a type from some value
using a module
.
Specs
It's the same as load/2
but it raises when the value
is not valid.
Specs
Helper function to convert a map with binary keys to a map with atom keys.