Jsonpatch.Mapper (Jsonpatch v1.0.0) View Source

Maps JSON patches between regular Maps and list or a single Jsonpatch.Operation.

Link to this section Summary

Functions

Creates JSON patch struct/s from a single or list maps which represents JSON patches.

Turns JSON patches into regular map/s.

Link to this section Functions

Specs

from_map(map() | [map()]) ::
  [Jsonpatch.t()] | Jsonpatch.t() | {:error, :invalid}

Creates JSON patch struct/s from a single or list maps which represents JSON patches.

Examples

iex> add_patch_map = %{"op" => "add", "path" => "/name", "value" => "Alice"}
iex> Jsonpatch.Mapper.from_map(add_patch_map)
%Jsonpatch.Operation.Add{path: "/name", value: "Alice"}

iex> unkown_patch_map = %{"op" => "foo", "path" => "/name", "value" => "Alice"}
iex> Jsonpatch.Mapper.from_map(unkown_patch_map)
{:error, :invalid}

Specs

to_map(Jsonpatch.t() | [Jsonpatch.t()]) :: map() | list() | {:error, :invalid}

Turns JSON patches into regular map/s.

Examples

iex> add_patch_map = %Jsonpatch.Operation.Add{path: "/name", value: "Alice"}
iex> Jsonpatch.Mapper.to_map(add_patch_map)
%{op: "add", path: "/name", value: "Alice"}

iex> add_patch_map = %Jsonpatch.Operation.Add{path: "/name", value: "Alice"}
iex> remove_patch_map = %Jsonpatch.Operation.Remove{path: "/location"}
iex> Jsonpatch.Mapper.to_map([add_patch_map, remove_patch_map])
[%{op: "add", path: "/name", value: "Alice"},
 %{op: "remove", path: "/location"}]