specify v0.4.2 Specify.Parsers View Source

Simple functions to parse strings to datatypes commonly used during configuration.

These functions can be used as parser/validator function in a call to Specify.Schema.field, by using their shorthand name (:integer as shorthand for &Specify.Parsers.integer/1).

(Of course, using their longhand name works as well.)

Defining your own parser function

A parser function receives the to-be-parsed/validated value as input, and should return {:ok, parsed_val} on success, or {:error, reason} on failure.

Be aware that depending on where the configuration is loaded from, the to-be-parsed value might be a binary string, or already the Elixir type you want to convert it to.

Link to this section Summary

Functions

Parses an atom or a binary string representing an (existing) atom.

Parses a boolean or a binary string representing a boolean value, turning it into a boolean.

Parses a float and turns a binary string representing a float into an float.

Parses an integer and turns binary string representing an integer into an integer.

Parses a list of elements.

Parses a binary string and turns anything that implements String.Chars into its binary string representation by calling to_string/1 on it.

Accepts any Elixir term as-is. Will not do any parsing.

Parses an atom or a binary string representing an (potentially not yet existing!) atom.

Link to this section Functions

Parses an atom or a binary string representing an (existing) atom.

Will not create new atoms (See String.to_existing_atom/1 for more info).

Parses a boolean or a binary string representing a boolean value, turning it into a boolean.

Parses a float and turns a binary string representing a float into an float.

Will also accept integers, which are turned into their float equivalent.

Parses an integer and turns binary string representing an integer into an integer.

Parses a list of elements.

In the case a binary string was passed, this parser uses Code.string_to_quoted under the hood to check for Elixir syntax, and will only accepts binaries representing lists.

If a list was passed in (or after turning a binary into a list), it will try to parse each of the elements in turn.

Parses a binary string and turns anything that implements String.Chars into its binary string representation by calling to_string/1 on it.

Accepts any Elixir term as-is. Will not do any parsing.

Only use this as a last resort. It is usually better to create your own dedicated parsing function instead.

Parses an atom or a binary string representing an (potentially not yet existing!) atom.

Will create new atoms. Whenever possible, consider using atom/1 instead. (See String.to_atom/1 for more info on why creating new atoms is usually a bad idea).