Skip to content

Commit 4ab503b

Browse files
committed
Create diffs as pure maps
1 parent cbf0b9f commit 4ab503b

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 2.1.0
2+
- Create diffs as pure maps and do not use Jsonpatch structs
3+
14
# 2.0.0
25
- Bugfix - ADD behaviour is now compliant with RFC (insert or update)
36
- Bugfix - allow usage of nil values, previous implementation used `Map.get` with default `nil` to detect if a key was not present

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ The docs can be found at [https://hexdocs.pm/jsonpatch](https://hexdocs.pm/jsonp
4343
iex> source = %{"name" => "Bob", "married" => false, "hobbies" => ["Sport", "Elixir", "Football"]}
4444
iex> destination = %{"name" => "Bob", "married" => true, "hobbies" => ["Elixir!"], "age" => 33}
4545
iex> Jsonpatch.diff(source, destination)
46-
{:ok, [
46+
[
4747
%Jsonpatch.Operation.Add{path: "/age", value: 33},
4848
%Jsonpatch.Operation.Replace{path: "/hobbies/0", value: "Elixir!"},
4949
%Jsonpatch.Operation.Replace{path: "/married", value: true},
5050
%Jsonpatch.Operation.Remove{path: "/hobbies/1"},
5151
%Jsonpatch.Operation.Remove{path: "/hobbies/2"}
52-
]}
52+
]
5353
```
5454

5555
### Apply patches

lib/jsonpatch.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ defmodule Jsonpatch do
150150
iex> destination = %{"name" => "Bob", "married" => true, "hobbies" => ["Elixir!"], "age" => 33}
151151
iex> Jsonpatch.diff(source, destination)
152152
[
153-
%Jsonpatch.Operation.Replace{path: "/married", value: true},
154-
%Jsonpatch.Operation.Remove{path: "/hobbies/2"},
155-
%Jsonpatch.Operation.Remove{path: "/hobbies/1"},
156-
%Jsonpatch.Operation.Replace{path: "/hobbies/0", value: "Elixir!"},
157-
%Jsonpatch.Operation.Add{path: "/age", value: 33}
153+
%{path: "/married", value: true, op: "replace"},
154+
%{path: "/hobbies/2", op: "remove"},
155+
%{path: "/hobbies/1", op: "remove"},
156+
%{path: "/hobbies/0", value: "Elixir!", op: "replace"},
157+
%{path: "/age", value: 33, op: "add"}
158158
]
159159
"""
160160
@spec diff(Types.json_container(), Types.json_container()) :: [Jsonpatch.t()]
@@ -190,7 +190,7 @@ defmodule Jsonpatch do
190190
|> flat()
191191
|> Stream.map(fn {k, _} -> escape(k) end)
192192
|> Stream.filter(fn k -> k not in checked_keys end)
193-
|> Stream.map(fn k -> %Remove{path: "#{ancestor_path}/#{k}"} end)
193+
|> Stream.map(fn k -> %{op: "remove", path: "#{ancestor_path}/#{k}"} end)
194194
|> Enum.reduce(patches, fn remove_patch, patches -> [remove_patch | patches] end)
195195
end
196196

@@ -201,7 +201,7 @@ defmodule Jsonpatch do
201201
case Utils.fetch(source, key) do
202202
# Key is not present in source
203203
{:error, _} ->
204-
[%Add{path: current_path, value: val} | patches]
204+
[%{op: "add", path: current_path, value: val} | patches]
205205

206206
# Source has a different value but both (destination and source) value are lists or a maps
207207
{:ok, source_val} when are_unequal_lists(source_val, val) ->
@@ -213,7 +213,7 @@ defmodule Jsonpatch do
213213

214214
# Scalar source val that is not equal
215215
{:ok, source_val} when source_val != val ->
216-
[%Replace{path: current_path, value: val} | patches]
216+
[%{op: "replace", path: current_path, value: val} | patches]
217217

218218
_ ->
219219
patches

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Jsonpatch.MixProject do
66
app: :jsonpatch,
77
name: "Jsonpatch",
88
description: "Implementation of RFC 6902 in pure Elixir",
9-
version: "2.0.0",
9+
version: "2.1.0",
1010
elixir: "~> 1.10",
1111
start_permanent: Mix.env() == :prod,
1212
deps: deps(),

0 commit comments

Comments
 (0)