Current section

Files

Jump to
y_ex lib map.ex
Raw

lib/map.ex

defmodule Yex.Map do
@moduledoc """
A shareable Map type.
"""
defstruct [
:reference
]
@type t :: %__MODULE__{
reference: any()
}
@doc """
set a key-value pair in the map.
"""
def set(%__MODULE__{} = map, key, content) do
Yex.Nif.map_set(map, key, content)
end
@doc """
delete a key from the map.
"""
def delete(%__MODULE__{} = map, key) do
Yex.Nif.map_delete(map, key)
end
@doc """
get a key from the map.
"""
def get(%__MODULE__{} = map, key) do
Yex.Nif.map_get(map, key)
end
@doc """
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)
"""
def to_map(%__MODULE__{} = map) do
Yex.Nif.map_to_map(map)
end
def size(%__MODULE__{} = map) do
Yex.Nif.map_size(map)
end
@doc """
Convert to json-compatible format.
## Examples Sync two clients by exchanging the complete document structure
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)
"""
def to_json(%__MODULE__{} = map) do
Yex.Nif.map_to_json(map)
end
end
defmodule Yex.MapPrelim do
@moduledoc """
A preliminary map. It can be used to early initialize the contents of a Map.
## Examples
iex> doc = Yex.Doc.new()
iex> array = Yex.Doc.get_array(doc, "array")
iex> Yex.Array.insert(array, 0, Yex.MapPrelim.from(%{ "key" => "value" }))
iex> {:ok, %Yex.Map{} = map} = Yex.Array.get(array, 0)
iex> Yex.Map.get(map, "key")
{:ok, "value"}
"""
defstruct [
:map
]
def from(%{} = map) do
%__MODULE__{map: map}
end
end