Jsonpatch v0.9.0 Jsonpatch.PathUtil View Source
Helper module for handling JSON paths.
Link to this section Summary
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 Functions
Specs
get_final_destination(map(), binary()) :: {map(), binary()} | {list(), binary()} | Jsonpatch.error()
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.PathUtil.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.PathUtil.get_final_destination(target, path)
{:error, :invalid_path, "e"}
iex> path = "/a/b/1/d"
iex> target = %{"a" => %{"b" => [true, %{"d" => 1}]}}
iex> Jsonpatch.PathUtil.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.PathUtil.get_final_destination(target, path)
{:error, :invalid_index, "42"}
Specs
operation_sort_value?(Jsonpatch.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?)
Specs
update_final_destination(map(), map(), binary()) :: map() | Jsonpatch.error()
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.PathUtil.update_final_destination(target, %{"e" => 1}, path)
%{"a" => %{"b" => %{"c" => %{"e" => 1}}}}