We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 0a0f7d5 + 7e23b54 commit ccb4146Copy full SHA for ccb4146
15 files changed
CHANGELOG
@@ -1,3 +1,7 @@
1
+# 1.0.0
2
+- Allow lists at top level of Jsonpatch.apply_patch
3
+- Fix error message when updating a non existing key in list
4
+
5
# 0.13.1
6
- Make Jsonpatch faster by (un)escaping conditional
7
README.md
@@ -32,7 +32,7 @@ The package can be installed by adding `jsonpatch` to your list of dependencies
32
```elixir
33
def deps do
34
[
35
- {:jsonpatch, "~> 0.13.1"}
+ {:jsonpatch, "~> 1.0.0"}
36
]
37
end
38
```
lib/jsonpatch.ex
@@ -69,7 +69,7 @@ defmodule Jsonpatch do
69
{:ok, map()} | Jsonpatch.error()
70
def apply_patch(json_patch, target, opts \\ [])
71
72
- def apply_patch(json_patch, %{} = target, opts) when is_list(json_patch) do
+ def apply_patch(json_patch, target, opts) when is_list(json_patch) do
73
# https://datatracker.ietf.org/doc/html/rfc6902#section-3
74
# > Operations are applied sequentially in the order they appear in the array.
75
result =
@@ -86,7 +86,7 @@ defmodule Jsonpatch do
86
87
88
89
- def apply_patch(json_patch, %{} = target, opts) do
+ def apply_patch(json_patch, target, opts) do
90
apply_patch([json_patch], target, opts)
91
92
lib/jsonpatch/operation.ex
@@ -15,6 +15,7 @@ defprotocol Jsonpatch.Operation do
15
@doc """
16
Executes the given patch to map/struct. Possible options are defined in `Jsonpatch`.
17
"""
18
- @spec apply_op(Jsonpatch.t(), map() | Jsonpatch.error(), keyword()) :: map() | Jsonpatch.error()
+ @spec apply_op(Jsonpatch.t(), list() | map() | Jsonpatch.error(), keyword()) ::
19
+ map() | Jsonpatch.error()
20
def apply_op(patch, target, opts \\ [])
21
lib/jsonpatch/operation/add.ex
@@ -25,10 +25,10 @@ defmodule Jsonpatch.Operation.Add do
25
@type t :: %__MODULE__{path: String.t(), value: any}
26
27
defimpl Operation do
28
- @spec apply_op(Add.t(), map | Jsonpatch.error(), keyword()) :: map
+ @spec apply_op(Add.t(), list() | map() | Jsonpatch.error(), keyword()) :: map
29
def apply_op(_, {:error, _, _} = error, _opt), do: error
30
31
- def apply_op(%Add{path: path, value: value}, %{} = target, opts) do
+ def apply_op(%Add{path: path, value: value}, target, opts) do
PathUtil.get_final_destination(target, path, opts)
|> do_add(target, path, value, opts)
@@ -56,7 +56,7 @@ defmodule Jsonpatch.Operation.Add do
56
if last_fragment == "-" or length(final_destination) == index do
57
Enum.concat(final_destination, [value])
58
else
59
- List.update_at(final_destination, index, fn _ -> value end)
+ List.replace_at(final_destination, index, value)
60
61
62
PathUtil.update_final_destination(
lib/jsonpatch/operation/copy.ex
@@ -19,7 +19,7 @@ defmodule Jsonpatch.Operation.Copy do
@type t :: %__MODULE__{from: String.t(), path: String.t()}
22
- @spec apply_op(Copy.t(), map() | Jsonpatch.error(), keyword()) :: map()
+ @spec apply_op(Copy.t(), list() | map() | Jsonpatch.error(), keyword()) :: map()
23
def apply_op(_, {:error, _, _} = error, _opts), do: error
24
def apply_op(%Copy{from: from, path: path}, target, opts) do
@@ -98,7 +98,7 @@ defmodule Jsonpatch.Operation.Copy do
98
99
{index, _} ->
100
if index < length(copy_target) do
101
- List.update_at(copy_target, index, fn _old -> copied_value end)
+ List.replace_at(copy_target, index, copied_value)
102
103
{:error, :invalid_index, copy_path_end}
104
lib/jsonpatch/operation/move.ex
@@ -20,7 +20,7 @@ defmodule Jsonpatch.Operation.Move do
- @spec apply_op(Move.t(), map | Jsonpatch.error(), keyword()) ::
+ @spec apply_op(Move.t(), list() | map() | Jsonpatch.error(), keyword()) ::
map()
lib/jsonpatch/operation/remove.ex
@@ -19,7 +19,7 @@ defmodule Jsonpatch.Operation.Remove do
@type t :: %__MODULE__{path: String.t()}
- @spec apply_op(Remove.t(), map | Jsonpatch.error(), keyword()) ::
+ @spec apply_op(Remove.t(), list() | map() | Jsonpatch.error(), keyword()) ::
@@ -77,12 +77,7 @@ defmodule Jsonpatch.Operation.Remove do
77
{:error, :invalid_index, fragment}
78
79
80
- update_list = List.update_at(target, index, &do_remove(&1, tail))
81
-
82
- case List.pop_at(target, index) do
83
- {nil, _} -> {:error, :invalid_index, fragment}
84
- _ -> update_list
85
- end
+ PathUtil.update_at(target, index, tail, &do_remove/2)
lib/jsonpatch/operation/replace.ex
@@ -19,10 +19,10 @@ defmodule Jsonpatch.Operation.Replace do
- @spec apply_op(Replace.t(), map | Jsonpatch.error(), keyword()) :: map
+ @spec apply_op(Replace.t(), list() | map() | Jsonpatch.error(), keyword()) :: map
- def apply_op(%Replace{path: path, value: value}, %{} = target, opts) do
+ def apply_op(%Replace{path: path, value: value}, target, opts) do
{final_destination, last_fragment} = PathUtil.get_final_destination(target, path, opts)
case do_update(final_destination, last_fragment, value) do
lib/jsonpatch/operation/test.ex
@@ -19,10 +19,10 @@ defmodule Jsonpatch.Operation.Test do
- @spec apply_op(Test.t(), map | Jsonpatch.error(), keyword()) :: map()
+ @spec apply_op(Test.t(), list() | map() | Jsonpatch.error(), keyword()) :: map()
- def apply_op(%Test{path: path, value: value}, %{} = target, opts) do
+ def apply_op(%Test{path: path, value: value}, target, opts) do
case PathUtil.get_final_destination(target, path, opts) |> do_test(value) do
true -> target
false -> {:error, :test_failed, "Expected value '#{value}' at '#{path}'"}
0 commit comments