Jsonpatch v0.6.1 Jsonpatch.Operation behaviour View Source

Defines behaviour for apply a patch to a struct.

Link to this section Summary

Types

t()

A valid Jsonpatch operation by RFC 6902

Functions

Uses a JSON patch path to get the last map that this path references.

Determines the sort value for the operation of a patch. This value assure in which order patches are applied. (Example: shall remove patches be applied before add patches?)

Updatest a map reference by a given JSON patch path with the new final destination.

Link to this section Types

Link to this section Functions

Link to this function

get_final_destination(target, path)

View Source

Specs

get_final_destination(map(), binary()) ::
  {map(), binary()} | {list(), binary()} | {:error, :invalid_path}

Uses a JSON patch path to get the last map that this path references.

Examples

iex> path = "/a/b/c/d"
iex> target = %{"a" => %{"b" => %{"c" => %{"d" => 1}}}}
iex> Jsonpatch.Operation.get_final_destination(target, path)
{%{"d" => 1}, "d"}

iex> # Invalid path
iex> path = "/a/e/c/d"
iex> target = %{"a" => %{"b" => %{"c" => %{"d" => 1}}}}
iex> Jsonpatch.Operation.get_final_destination(target, path)
{:error, :invalid_path}

iex> path = "/a/b/1/d"
iex> target = %{"a" => %{"b" => [true, %{"d" => 1}]}}
iex> Jsonpatch.Operation.get_final_destination(target, path)
{%{"d" => 1}, "d"}

iex> # Invalid path
iex> path = "/a/b/42/d"
iex> target = %{"a" => %{"b" => [true, %{"d" => 1}]}}
iex> Jsonpatch.Operation.get_final_destination(target, path)
{:error, :invalid_path}
Link to this function

operation_sort_value?(patch)

View Source

Specs

operation_sort_value?(t()) :: integer()

Determines the sort value for the operation of a patch. This value assure in which order patches are applied. (Example: shall remove patches be applied before add patches?)

Link to this function

update_final_destination(target, new_destination, path)

View Source

Specs

update_final_destination(map(), map(), binary()) ::
  map() | {:error, :invalid_path}

Updatest a map reference by a given JSON patch path with the new final destination.

Examples

iex> path = "/a/b/c/d"
iex> target = %{"a" => %{"b" => %{"c" => %{"d" => 1}}}}
iex> Jsonpatch.Operation.update_final_destination(target, %{"e" => 1}, path)
%{"a" => %{"b" => %{"c" => %{"e" => 1}}}}

Link to this section Callbacks

Specs

apply_op(t(), map()) :: map() | :ok | :error