specify v0.10.0 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 a function.

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

Parses a list of elements.

Parses a Module-Function-Arity tuple.

Similar to float/1, but only accepts floats larger than or equal to 0.

Similar to integer/1, but only accepts integers larger than or equal to 0.

Parses an option. An option is a 2-tuple whose first element is an atom, and the second an arbitrary term. The following terms are options

Similar to float/1, but only accepts floats larger than 0.

Similar to integer/1, but only accepts integers larger than 0.

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.

Allows to pass in a 'timeout' which is a common setting for OTP-related features, accepting either a positive integer, or the atom :infinity.

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 a function.

This can be a function capture, or a MFA (Module-Function-Arity) tuple, which will be transformed into the &Module.function/arity capture.

(So in either case, you end up with a function value that you can call using the dot operator, i.e. .() or .(maybe, some, args)).

String Contexts

For contexts in which values are specified as strings, the parser only supports the MFA format. This is for security (and ease of parsing) reasons.

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 Module-Function-Arity tuple.

Accepts it both as Elixir three-element tuple (where the first two elements are atoms, and the third is a nonnegative integer), or as string representation of the same.

Will also check and ensure that this function is actually defined.

Similar to float/1, but only accepts floats larger than or equal to 0.

Link to this function

nonnegative_integer(val)

View Source

Similar to integer/1, but only accepts integers larger than or equal to 0.

Parses an option. An option is a 2-tuple whose first element is an atom, and the second an arbitrary term. The following terms are options:

  • {:a, :b}
  • {MyApp.Module, "Hellow, world!"}
  • {:some_atom, %{[] => {1, 2, 3, 4, 5}}} In the case a binary string was passed, this parser uses Code.string_to_quoted under the hood to parse the terms. It can be convenently used alongside the list parser to check for keyword list: {:list, :option}.

Similar to float/1, but only accepts floats larger than 0.

Similar to integer/1, but only accepts integers larger than 0.

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.

Allows to pass in a 'timeout' which is a common setting for OTP-related features, accepting either a positive integer, or the atom :infinity.

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).