Yamel (yamel v2.0.4) View Source

Defines functions to work with YAML in Elixir.

Link to this section Summary

Functions

Decodes a YAML string to a map or list.

Decodes a YAML string to a map or list, throws if fails.

Encodes a YAML term. Returns {:ok, yaml()} where the second term is the encoded YAML term. Otherwise, returns {:error, reason} with reason being a string stating the error reason.

Encodes a YAML term directly into a string, throwing an exception if the term can not be encoded.

Link to this section Types

Specs

decode_opts() :: [{:keys, keys()}] | []

Specs

keys() :: :atom | :string | :atoms | :strings

Specs

parse_error() :: YamlElixir.ParsingError.t()

Specs

t() :: map() | [any()]

Specs

yaml() :: String.t()

Link to this section Functions

Link to this function

decode(yaml_string, options \\ [])

View Source

Specs

decode(yaml(), decode_opts()) :: {:ok, Yamel.t()} | {:error, parse_error()}

Decodes a YAML string to a map or list.

Returns {:ok, Yamel.t()} or {:error, reason}, where reason is parse_error()

Options

  • :keys - indicates the type for the map's keys. Default: :string

Examples

iex> Yamel.decode(
...> ~s"---
...>    - Apple
...>    - Orange
...>    - Strawberry
...>    - Mango")
{:ok, ["Apple", "Orange","Strawberry", "Mango"]}
Link to this function

decode!(yaml_string, options \\ [])

View Source

Specs

decode!(yaml(), decode_opts()) :: Yamel.t()

Decodes a YAML string to a map or list, throws if fails.

Throws parse_error() exception if given YAML cannot be parsed.

Options

  • :keys - indicates the type for the map's keys. Default: :string

Examples

iex> Yamel.decode!("
...>     name: Jane Doe
...>     job: Developer
...>     skill: Elite
...>     employed: True")
%{"employed" => true, "job" => "Developer", "name" => "Jane Doe", "skill" => "Elite"}

With option keys: :atom

iex> Yamel.decode!("
...>     name: Jane Doe
...>     job: Developer
...>     skill: Elite
...>     employed: True", keys: :atom)
%{employed: true, job: "Developer", name: "Jane Doe", skill: "Elite"}
Link to this function

encode(map_or_list, opts \\ %{node_level: 0, indent_size: 2})

View Source

Specs

encode(Yamel.t(), Yamel.Encoder.opts()) ::
  {:ok, yaml()} | {:error, reason :: String.t()}

Encodes a YAML term. Returns {:ok, yaml()} where the second term is the encoded YAML term. Otherwise, returns {:error, reason} with reason being a string stating the error reason.

Options

  • :quote - The value types to be quoted.

Examples

iex> Yamel.encode(["foo", "bar", "baz"])
{:ok, "- foo\n- bar\n- baz\n\n"}

iex> Yamel.encode([:foo, "bar", 123, true], quote: [:string, :atom, :number])
{:ok, "- \"foo\"\n- \"bar\"\n- \"123\"\n- true\n\n"}

iex> Yamel.encode([:foo, "bar", 12.3, true], quote: [:string, :boolean])
{:ok, "- foo\n- \"bar\"\n- 12.3\n- \"true\"\n\n"}
Link to this function

encode!(map_or_list_or_tuple, opts \\ %{node_level: 0, indent_size: 2})

View Source

Specs

encode!(Yamel.t(), Yamel.Encoder.opts()) :: yaml()

Encodes a YAML term directly into a string, throwing an exception if the term can not be encoded.

Options

  • :quote - The value types to be quoted.

Examples

iex> Yamel.encode!(["foo", "bar", "baz"])
"- foo\n- bar\n- baz\n\n"

iex> Yamel.encode!([:foo, "bar", 123, true], quote: [:string, :atom, :number])
"- \"foo\"\n- \"bar\"\n- \"123\"\n- true\n\n"

iex> Yamel.encode!([:foo, "bar", 12.3, true], quote: [:string, :boolean])
"- foo\n- \"bar\"\n- 12.3\n- \"true\"\n\n"