Transmog v0.1.0 Transmog.Parser protocol View Source

Transmog.Parser defines how to parse a single key path. Parsing a key path means to transform it into a list which defines how to reach the target value in the nested map or list.

Examples

"a.:b.c" #=> References a map or list with key path ["a", :b, "c"]
":a.1"   #=> References a map or list with key path [:a, "1"]

Currently the following types are supported by this library:

Examples

iex> string = "credentials.:first_name"
iex> {:ok, key_path} = Transmog.Parser.parse(string)
iex> key_path
["credentials", :first_name]

iex> string = "credentials\\.first_name"
iex> {:ok, key_path} = Transmog.Parser.parse(string)
iex> key_path
["credentials.first_name"]

iex> string = ""
iex> Transmog.Parser.parse(string)
{:error, :invalid_key_path}

When you want to use a period character in your key path then you can escape it with backslashes and it will be ignored when parsing the string.

Examples

string = "credentials\\.first_name"
Transmog.Parser.parse!(string)
#=> Will return ["credentials.first_name"]

Link to this section Summary

Types

error/0 is the type for the error that the parser should return if it encounters a value that is not valid.

t()

Functions

parse/1 will convert a value into a valid key path. If applicable and the value is not valid, then a value of error/0 will be returned instead.

parse!/1 will convert a value into a valid key path. If the key path is not valid then an error will be raised. The result will be unwrapped automatically.

Link to this section Types

Link to this type

error()

View Source
error() :: {:error, :invalid_key_path}

error/0 is the type for the error that the parser should return if it encounters a value that is not valid.

Link to this section Functions

Link to this function

parse(data)

View Source
parse(data :: t()) :: {:ok, [term()]} | error()

parse/1 will convert a value into a valid key path. If applicable and the value is not valid, then a value of error/0 will be returned instead.

Link to this function

parse!(data)

View Source
parse!(data :: t()) :: [term()]

parse!/1 will convert a value into a valid key path. If the key path is not valid then an error will be raised. The result will be unwrapped automatically.