View Source Drops.Casters (drops v0.1.0)

Drops.Casters is a module that provides functions for casting values from one type to another.

This module is the default caster module used by the Drops.Types.Map.DSL.cast function.

Summary

Functions

Link to this function

cast(atom1, atom2, value)

View Source
@spec cast(:string, :integer, value :: String.t()) :: integer()
@spec cast(:string, :float, value :: String.t()) :: float()
@spec cast(:integer, :string, value :: integer()) :: String.t()

Casts a string into an integer

Casting a string into an integer

iex> defmodule UserContract do
...>   use Drops.Contract
...>
...>   schema do
...>     %{required(:age) => cast(:string) |> type(:integer)}
...>   end
...> end
iex> UserContract.conform(%{age: "20"})
{:ok, %{age: 20}}

Casting a string into a float

iex> defmodule UserContract do
...>   use Drops.Contract
...>
...>   schema do
...>     %{required(:num) => cast(:string) |> type(:float)}
...>   end
...> end
iex> UserContract.conform(%{num: "20.5"})
{:ok, %{num: 20.5}}

Casting an integer into a string

iex> defmodule UserContract do
...>   use Drops.Contract
...>
...>   schema do
...>     %{required(:id) => cast(:integer) |> type(:string)}
...>   end
...> end
iex> UserContract.conform(%{id: 312})
{:ok, %{id: "312"}}

Casting an integer into a date time

iex> defmodule UserContract do
...>   use Drops.Contract
...>
...>   schema do
...>     %{required(:date) => cast(:integer) |> type(:date_time)}
...>   end
...> end
iex> UserContract.conform(%{date: 1614556800})
{:ok, %{date: ~U[2021-03-01 00:00:00Z]}}
Link to this function

cast(atom1, atom2, value, unit \\ :second)

View Source
@spec cast(:integer, :date_time, value :: integer(), unit :: atom()) :: DateTime.t()