Nvir.Cast (Nvir v0.16.4)

Copy Markdown View Source

Collection of casters for environment variables.

The list of built-in casters is described in the reading environment variables guide.

The :atom, :atom? and :atom! casters create atoms dynamically with String.to_atom/1, and atoms stay in memory for the whole life of the virtual machine. Use those casters for variables whose possible values are defined by your own application and deployment files. For values that come from the outside world, use the :existing_atom variants, which only accept values that are already atoms in your application.

Summary

Functions

Casts the given value to the desired type.

Types

caster()

@type caster() ::
  :string
  | :string?
  | :string!
  | :atom
  | :atom?
  | :atom!
  | :existing_atom
  | :existing_atom?
  | :existing_atom!
  | :boolean
  | :boolean!
  | :boolean?
  | :integer!
  | :integer?
  | :integer
  | :float!
  | :float?
  | :float
  | (term() -> result())

result()

@type result() ::
  {:ok, term()} | {:error, String.t()} | {:error, :empty} | {:error, :bad_cast}

Functions

cast(value, caster)

@spec cast(term(), caster()) :: result()
@spec cast(String.t(), caster()) :: result()

Casts the given value to the desired type.

Environment variables are always defined as a string. Thus, the cast/2 function will only accept strings for the value argument.

Accepts a built-in caster or a custom function returning {:ok, value} or {:error, String.t()}. You may as well directly return an error tuple from a recursive cast/2 call.

The list of built-in casters is described in the reading environment variables guide.