Yamlixir v1.0.2 Yamlixir View Source

Simple YAML parser for Elixir.

Link to this section Summary

Functions

Decodes a string of valid YAML into Elixir data

The same as decode/2 but raises a Yamlixir.DecodingError exception if it fails. Returns the decoded YAML otherwise

Handles the sigil ~y for decoding YAML

Link to this section Types

Link to this section Functions

Link to this function

decode(yaml, options \\ []) View Source
decode(yaml(), options()) :: {:ok, decoded()} | {:error, error()}

Decodes a string of valid YAML into Elixir data.

Returns {:ok, decoded} on success and {:error, %Yamlixir.DecodingError{}} on failure.

Options

  • :at - Returns only the document at the given position in the list of documents. Expects input to be an integer.
  • :keys - Controls how keys in maps are decoded. Defaults to strings. Possible values are:

Examples

iex> Yamlixir.decode("")
{:ok, []}

iex> Yamlixir.decode("---")
{:ok, [%{}]}

iex> Yamlixir.decode(":")
{:error, %Yamlixir.DecodingError{}}

iex> Yamlixir.decode("a: b\nc: d")
{:ok, [%{"a" => "b", "c" => "d"}]}

iex> Yamlixir.decode("---\na: b\nc: d\n---\ne: f\ng: h")
{:ok, [%{"a" => "b", "c" => "d"}, %{"e" => "f", "g" => "h"}]}

iex> Yamlixir.decode("---\na: b\nc: d\n---\ne: f\ng: h", at: 0)
{:ok, %{"a" => "b", "c" => "d"}}

iex> Yamlixir.decode("---\na: b\nc: d\n---\ne: f\ng: h", at: -1)
{:ok, %{"e" => "f", "g" => "h"}}

iex> Yamlixir.decode("---\na: b\nc: d\n---\ne: f\ng: h", at: -1, keys: :atoms)
{:ok, %{e: "f", g: "h"}}
Link to this function

decode!(yaml, options \\ []) View Source
decode!(yaml(), options()) :: decoded()

The same as decode/2 but raises a Yamlixir.DecodingError exception if it fails. Returns the decoded YAML otherwise.

Examples

iex> Yamlixir.decode!("")
[]

iex> Yamlixir.decode!("---")
[%{}]

iex> Yamlixir.decode!(":")
** (Yamlixir.DecodingError) decoding error

iex> Yamlixir.decode!("a: b\nc: d")
[%{"a" => "b", "c" => "d"}]

iex> Yamlixir.decode!("---\na: b\nc: d\n---\ne: f\ng: h")
[%{"a" => "b", "c" => "d"}, %{"e" => "f", "g" => "h"}]

iex> Yamlixir.decode!("---\na: b\nc: d\n---\ne: f\ng: h", at: 0)
%{"a" => "b", "c" => "d"}

iex> Yamlixir.decode!("---\na: b\nc: d\n---\ne: f\ng: h", at: -1)
%{"e" => "f", "g" => "h"}

iex> Yamlixir.decode!("---\na: b\nc: d\n---\ne: f\ng: h", at: -1, keys: :atoms)
%{e: "f", g: "h"}
Link to this function

sigil_y(yaml, list) View Source
sigil_y(yaml(), list()) :: decoded()

Handles the sigil ~y for decoding YAML.

It passes the string to decode!/2, returning the decoded data. Raises a Yamlixir.DecodingError exception when given invalid YAML.

Modifiers

Examples

import Yamlixir, only: [sigil_y: 2]
~y"""
a: b
c: d
"""
#=> [%{"a" => "b", "c" => "d"}]