Nvir.Cast (Nvir v0.13.1)
View SourceCollection of casters for environment variables.
Summary
Functions
Casts the given value to the desired type.
Types
Functions
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.
Built-in casters
String Casters
Caster | Description |
---|---|
:string | Returns the value as-is. |
:string? | Converts empty strings to nil . |
:string! | Raises for empty strings. |
Boolean Casters
Caster | Description |
---|---|
:boolean | "false" , "0" and empty strings become false , any other value is true . Case insensitive. It is recommended to use :boolean! instead. |
:boolean! | Accepts only "true" , "false" , "1" , and "0" . Case insensitive. |
Number Casters
Caster | Description |
---|---|
:integer! | Strict integer parsing. |
:integer? | Like :integer! but empty strings becomes nil . |
:float! | Strict float parsing. |
:float? | Like :float! but empty strings becomes nil . |
Atom Casters
Caster | Description |
---|---|
:atom | Converts the value to an atom. Use the :existing_atom variants when possible. |
:atom? | Like :atom but empty strings becomes nil . |
:atom! | Like :atom but rejects empty strings. |
:existing_atom | Converts to existing atom only, raises otherwise. |
:existing_atom? | Like :existing_atom but empty strings becomes nil . |
:existing_atom! | Like :existing_atom but rejects empty strings. |
Note that using :existing_atom
with empty strings will not raise an exception
because the :""
atom is valid and is generally defined by the system on boot.
Deprecated casters
Those exist for legacy reasons and should be avoided. They will trigger a runtime warning when used.
In some languages, using null
where a number is expected will cast the value
to a default type value, generally 0
and +0.0
for integers and floats.
This behaviour does not exist in Elixir so casters for such types behave the
same with-or-without the !
suffix. This means :integer
and :float
will
raise for empty strings.
Caster | Description |
---|---|
:boolean? | Same as :boolean . ⚠️ Returns false instead of nil for empty strings. |
:integer | Same as :integer! . |
:float | Same as :float! . |