View Source Yex.Map (y_ex v0.7.1)

A shareable Map type.

Summary

Functions

delete a key from the map.

get a key from the map.

get(map, key) deprecated

get a key from the map. ## Examples

set a key-value pair in the map.

Convert to json-compatible format.

Convert to elixir map.

Types

t()

@type t() :: %Yex.Map{doc: Yex.Doc.t(), reference: reference()}

Functions

delete(map, key)

@spec delete(t(), term()) :: :ok

delete a key from the map.

Examples

iex> doc = Yex.Doc.new()
iex> map = Yex.Doc.get_map(doc, "map")
iex> Yex.Map.set(map, "plane", ["Hello", "World"])
iex> Yex.Map.delete(map, "plane")
:ok

fetch(map, key)

@spec fetch(t(), binary()) :: {:ok, term()} | :error
@spec fetch(t(), binary()) :: {:ok, term()} | :error

get a key from the map.

Examples

iex> doc = Yex.Doc.new()
iex> map = Yex.Doc.get_map(doc, "map")
iex> Yex.Map.set(map, "plane", ["Hello", "World"])
iex> Yex.Map.fetch(map, "plane")
{:ok, ["Hello", "World"]}
iex> Yex.Map.fetch(map, "not_found")
:error

fetch!(map, key)

get(map, key)

This function is deprecated. Rename to `fetch/2`.
@spec get(t(), binary()) :: {:ok, term()} | :error

get a key from the map. ## Examples

iex> doc = Yex.Doc.new()
iex> map = Yex.Doc.get_map(doc, "map")
iex> Yex.Map.set(map, "plane", ["Hello", "World"])
iex> Yex.Map.get(map, "plane")
{:ok, ["Hello", "World"]}
iex> Yex.Map.get(map, "not_found")
:error

has_key?(map, key)

@spec has_key?(t(), binary()) :: boolean()

set(map, key, content)

@spec set(t(), term(), term()) :: term()

set a key-value pair in the map.

Examples

iex> doc = Yex.Doc.new()
iex> map = Yex.Doc.get_map(doc, "map")
iex> Yex.Map.set(map, "plane", ["Hello", "World"])
:ok

size(map)

@spec size(t()) :: integer()

to_json(map)

@spec to_json(t()) :: map()

Convert to json-compatible format.

Examples shows a map being created incrementally then returned

iex> doc = Yex.Doc.new()
iex> map = Yex.Doc.get_map(doc, "map")
iex> Yex.Map.set(map, "array", Yex.ArrayPrelim.from(["Hello", "World"]))
iex> Yex.Map.set(map, "plane", ["Hello", "World"])
iex> assert %{"plane" => ["Hello", "World"], "array" => ["Hello", "World"]} = Yex.Map.to_json(map)

to_list(map)

to_map(map)

@spec to_map(t()) :: map()

Convert to elixir map.

Examples

iex> doc = Yex.Doc.new()
iex> map = Yex.Doc.get_map(doc, "map")
iex> Yex.Map.set(map, "array", Yex.ArrayPrelim.from(["Hello", "World"]))
iex> Yex.Map.set(map, "plane", ["Hello", "World"])
iex> assert %{"plane" => ["Hello", "World"], "array" => %Yex.Array{}} = Yex.Map.to_map(map)