Skip to content

Commit ccb4146

Browse files
authored
Merge pull request #13 from corka149/support-lists-on-top-level
Allow lists at top level of apply_patch
2 parents 0a0f7d5 + 7e23b54 commit ccb4146

15 files changed

Lines changed: 106 additions & 26 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
15
# 0.13.1
26
- Make Jsonpatch faster by (un)escaping conditional
37

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The package can be installed by adding `jsonpatch` to your list of dependencies
3232
```elixir
3333
def deps do
3434
[
35-
{:jsonpatch, "~> 0.13.1"}
35+
{:jsonpatch, "~> 1.0.0"}
3636
]
3737
end
3838
```

lib/jsonpatch.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ defmodule Jsonpatch do
6969
{:ok, map()} | Jsonpatch.error()
7070
def apply_patch(json_patch, target, opts \\ [])
7171

72-
def apply_patch(json_patch, %{} = target, opts) when is_list(json_patch) do
72+
def apply_patch(json_patch, target, opts) when is_list(json_patch) do
7373
# https://datatracker.ietf.org/doc/html/rfc6902#section-3
7474
# > Operations are applied sequentially in the order they appear in the array.
7575
result =
@@ -86,7 +86,7 @@ defmodule Jsonpatch do
8686
end
8787
end
8888

89-
def apply_patch(json_patch, %{} = target, opts) do
89+
def apply_patch(json_patch, target, opts) do
9090
apply_patch([json_patch], target, opts)
9191
end
9292

lib/jsonpatch/operation.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ defprotocol Jsonpatch.Operation do
1515
@doc """
1616
Executes the given patch to map/struct. Possible options are defined in `Jsonpatch`.
1717
"""
18-
@spec apply_op(Jsonpatch.t(), map() | Jsonpatch.error(), keyword()) :: map() | Jsonpatch.error()
18+
@spec apply_op(Jsonpatch.t(), list() | map() | Jsonpatch.error(), keyword()) ::
19+
map() | Jsonpatch.error()
1920
def apply_op(patch, target, opts \\ [])
2021
end

lib/jsonpatch/operation/add.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ defmodule Jsonpatch.Operation.Add do
2525
@type t :: %__MODULE__{path: String.t(), value: any}
2626

2727
defimpl Operation do
28-
@spec apply_op(Add.t(), map | Jsonpatch.error(), keyword()) :: map
28+
@spec apply_op(Add.t(), list() | map() | Jsonpatch.error(), keyword()) :: map
2929
def apply_op(_, {:error, _, _} = error, _opt), do: error
3030

31-
def apply_op(%Add{path: path, value: value}, %{} = target, opts) do
31+
def apply_op(%Add{path: path, value: value}, target, opts) do
3232
PathUtil.get_final_destination(target, path, opts)
3333
|> do_add(target, path, value, opts)
3434
end
@@ -56,7 +56,7 @@ defmodule Jsonpatch.Operation.Add do
5656
if last_fragment == "-" or length(final_destination) == index do
5757
Enum.concat(final_destination, [value])
5858
else
59-
List.update_at(final_destination, index, fn _ -> value end)
59+
List.replace_at(final_destination, index, value)
6060
end
6161

6262
PathUtil.update_final_destination(

lib/jsonpatch/operation/copy.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule Jsonpatch.Operation.Copy do
1919
@type t :: %__MODULE__{from: String.t(), path: String.t()}
2020

2121
defimpl Operation do
22-
@spec apply_op(Copy.t(), map() | Jsonpatch.error(), keyword()) :: map()
22+
@spec apply_op(Copy.t(), list() | map() | Jsonpatch.error(), keyword()) :: map()
2323
def apply_op(_, {:error, _, _} = error, _opts), do: error
2424

2525
def apply_op(%Copy{from: from, path: path}, target, opts) do
@@ -98,7 +98,7 @@ defmodule Jsonpatch.Operation.Copy do
9898

9999
{index, _} ->
100100
if index < length(copy_target) do
101-
List.update_at(copy_target, index, fn _old -> copied_value end)
101+
List.replace_at(copy_target, index, copied_value)
102102
else
103103
{:error, :invalid_index, copy_path_end}
104104
end

lib/jsonpatch/operation/move.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule Jsonpatch.Operation.Move do
2020
@type t :: %__MODULE__{from: String.t(), path: String.t()}
2121

2222
defimpl Operation do
23-
@spec apply_op(Move.t(), map | Jsonpatch.error(), keyword()) ::
23+
@spec apply_op(Move.t(), list() | map() | Jsonpatch.error(), keyword()) ::
2424
map()
2525
def apply_op(_, {:error, _, _} = error, _opts), do: error
2626

lib/jsonpatch/operation/remove.ex

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule Jsonpatch.Operation.Remove do
1919
@type t :: %__MODULE__{path: String.t()}
2020

2121
defimpl Operation do
22-
@spec apply_op(Remove.t(), map | Jsonpatch.error(), keyword()) ::
22+
@spec apply_op(Remove.t(), list() | map() | Jsonpatch.error(), keyword()) ::
2323
map()
2424
def apply_op(_, {:error, _, _} = error, _opts), do: error
2525

@@ -77,12 +77,7 @@ defmodule Jsonpatch.Operation.Remove do
7777
{:error, :invalid_index, fragment}
7878

7979
{index, _} ->
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
80+
PathUtil.update_at(target, index, tail, &do_remove/2)
8681
end
8782
end
8883
end

lib/jsonpatch/operation/replace.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ defmodule Jsonpatch.Operation.Replace do
1919
@type t :: %__MODULE__{path: String.t(), value: any}
2020

2121
defimpl Operation do
22-
@spec apply_op(Replace.t(), map | Jsonpatch.error(), keyword()) :: map
22+
@spec apply_op(Replace.t(), list() | map() | Jsonpatch.error(), keyword()) :: map
2323
def apply_op(_, {:error, _, _} = error, _opts), do: error
2424

25-
def apply_op(%Replace{path: path, value: value}, %{} = target, opts) do
25+
def apply_op(%Replace{path: path, value: value}, target, opts) do
2626
{final_destination, last_fragment} = PathUtil.get_final_destination(target, path, opts)
2727

2828
case do_update(final_destination, last_fragment, value) do

lib/jsonpatch/operation/test.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ defmodule Jsonpatch.Operation.Test do
1919
@type t :: %__MODULE__{path: String.t(), value: any}
2020

2121
defimpl Operation do
22-
@spec apply_op(Test.t(), map | Jsonpatch.error(), keyword()) :: map()
22+
@spec apply_op(Test.t(), list() | map() | Jsonpatch.error(), keyword()) :: map()
2323
def apply_op(_, {:error, _, _} = error, _opts), do: error
2424

25-
def apply_op(%Test{path: path, value: value}, %{} = target, opts) do
25+
def apply_op(%Test{path: path, value: value}, target, opts) do
2626
case PathUtil.get_final_destination(target, path, opts) |> do_test(value) do
2727
true -> target
2828
false -> {:error, :test_failed, "Expected value '#{value}' at '#{path}'"}

0 commit comments

Comments
 (0)