@@ -30,15 +30,16 @@ defmodule Jsonpatch do
3030
3131 @ doc """
3232 Apply a Jsonpatch or a list of Jsonpatches to a map or struct. The whole patch will not be applied
33- when any path is invalid or any other error occured.
33+ when any path is invalid or any other error occured. When a list is provided, the operations are
34+ applied in the order as they appear in the list.
3435
3536 ## Examples
3637 iex> patch = [
3738 ...> %Jsonpatch.Operation.Add{path: "/age", value: 33},
3839 ...> %Jsonpatch.Operation.Replace{path: "/hobbies/0", value: "Elixir!"},
3940 ...> %Jsonpatch.Operation.Replace{path: "/married", value: true},
40- ...> %Jsonpatch.Operation.Remove{path: "/hobbies/1"},
4141 ...> %Jsonpatch.Operation.Remove{path: "/hobbies/2"},
42+ ...> %Jsonpatch.Operation.Remove{path: "/hobbies/1"},
4243 ...> %Jsonpatch.Operation.Copy{from: "/name", path: "/surname"},
4344 ...> %Jsonpatch.Operation.Move{from: "/home", path: "/work"},
4445 ...> %Jsonpatch.Operation.Test{path: "/name", value: "Bob"}
@@ -61,14 +62,10 @@ defmodule Jsonpatch do
6162 def apply_patch ( json_patch , target )
6263
6364 def apply_patch ( json_patch , % { } = target ) when is_list ( json_patch ) do
64- # Operatons MUST be sorted before applying because a remove operation for path "/foo/2" must be done
65- # before the remove operation for path "/foo/1". Without order it could be possible that the wrong
66- # value will be removed or only one value instead of two.
65+ # https://datatracker.ietf.org/doc/html/rfc6902#section-3
66+ # > Operations are applied sequentially in the order they appear in the array.
6767 result =
6868 json_patch
69- |> Enum . map ( & create_sort_value / 1 )
70- |> Enum . sort ( fn { sort_value_1 , _ } , { sort_value_2 , _ } -> sort_value_1 >= sort_value_2 end )
71- |> Enum . map ( fn { _ , patch } -> patch end )
7269 |> Enum . reduce ( target , & Jsonpatch.Operation . apply_op / 2 )
7370
7471 case result do
@@ -88,7 +85,8 @@ defmodule Jsonpatch do
8885
8986 @ doc """
9087 Apply a Jsonpatch or a list of Jsonpatches to a map or struct. In case of an error
91- it will raise an exception.
88+ it will raise an exception. When a list is provided, the operations are applied in
89+ the order as they appear in the list.
9290
9391 (See Jsonpatch.apply_patch/2 for more details)
9492 """
@@ -219,25 +217,4 @@ defmodule Jsonpatch do
219217 defp escape ( subpath ) do
220218 subpath
221219 end
222-
223- # Create once a easy sortable value for a operation
224- defp create_sort_value ( % { path: path } = operation ) do
225- fragments = String . split ( path , "/" )
226-
227- x = Jsonpatch.PathUtil . operation_sort_value? ( operation ) * 1_000_000 * 100_000_000
228- y = length ( fragments ) * 100_000_000
229-
230- z =
231- case List . last ( fragments ) |> Integer . parse ( ) do
232- :error -> 0
233- { int , _ } -> int
234- end
235-
236- # Structure of recorde sort value
237- # x = Kind of PathUtil
238- # y = Amount of fragments (how deep goes the path?)
239- # z = At which position in a list?
240- # xxxxyyyyyyzzzzzzzz
241- { x + y + z , operation }
242- end
243220end
0 commit comments