Skip to content

Commit 8ae3780

Browse files
authored
Merge pull request #20 from corka149/19-support-option-for-diff2-that-tells-it-to-return-maps-instead-of-structs
19 support option for diff2 that tells it to return maps instead of structs
2 parents f0a41e2 + 1251bd7 commit 8ae3780

File tree

5 files changed

+34
-31
lines changed

5 files changed

+34
-31
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: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ defmodule Jsonpatch do
8080

8181
op =
8282
case struct_mod do
83-
Jsonpatch.Operation.Add -> "add"
84-
Jsonpatch.Operation.Remove -> "remove"
85-
Jsonpatch.Operation.Replace -> "replace"
86-
Jsonpatch.Operation.Copy -> "copy"
87-
Jsonpatch.Operation.Move -> "move"
88-
Jsonpatch.Operation.Test -> "test"
83+
Add -> "add"
84+
Remove -> "remove"
85+
Replace -> "replace"
86+
Copy -> "copy"
87+
Move -> "move"
88+
Test -> "test"
8989
end
9090

9191
json_patch = Map.put(json_patch, "op", op)
@@ -98,27 +98,27 @@ defmodule Jsonpatch do
9898
end
9999

100100
defp do_apply_patch(%{"op" => "add", "path" => path, "value" => value}, target, opts) do
101-
Jsonpatch.Operation.Add.apply(%Add{path: path, value: value}, target, opts)
101+
Add.apply(%Add{path: path, value: value}, target, opts)
102102
end
103103

104104
defp do_apply_patch(%{"op" => "remove", "path" => path}, target, opts) do
105-
Jsonpatch.Operation.Remove.apply(%Remove{path: path}, target, opts)
105+
Remove.apply(%Remove{path: path}, target, opts)
106106
end
107107

108108
defp do_apply_patch(%{"op" => "replace", "path" => path, "value" => value}, target, opts) do
109-
Jsonpatch.Operation.Replace.apply(%Replace{path: path, value: value}, target, opts)
109+
Replace.apply(%Replace{path: path, value: value}, target, opts)
110110
end
111111

112112
defp do_apply_patch(%{"op" => "copy", "from" => from, "path" => path}, target, opts) do
113-
Jsonpatch.Operation.Copy.apply(%Copy{from: from, path: path}, target, opts)
113+
Copy.apply(%Copy{from: from, path: path}, target, opts)
114114
end
115115

116116
defp do_apply_patch(%{"op" => "move", "from" => from, "path" => path}, target, opts) do
117-
Jsonpatch.Operation.Move.apply(%Move{from: from, path: path}, target, opts)
117+
Move.apply(%Move{from: from, path: path}, target, opts)
118118
end
119119

120120
defp do_apply_patch(%{"op" => "test", "path" => path, "value" => value}, target, opts) do
121-
Jsonpatch.Operation.Test.apply(%Test{path: path, value: value}, target, opts)
121+
Test.apply(%Test{path: path, value: value}, target, opts)
122122
end
123123

124124
defp do_apply_patch(json_patch, _target, _opts) do
@@ -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: 5 additions & 5 deletions
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(),
@@ -36,10 +36,10 @@ defmodule Jsonpatch.MixProject do
3636

3737
defp deps do
3838
[
39-
{:excoveralls, "~> 0.15", only: [:test]},
40-
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
41-
{:dialyxir, "~> 1.2", only: [:dev], runtime: false},
42-
{:ex_doc, "~> 0.29", only: [:dev], runtime: false}
39+
{:excoveralls, "~> 0.16", only: [:test]},
40+
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
41+
{:dialyxir, "~> 1.3", only: [:dev], runtime: false},
42+
{:ex_doc, "~> 0.30", only: [:dev], runtime: false}
4343
]
4444
end
4545

mix.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
44
"credo": {:hex, :credo, "1.7.0", "6119bee47272e85995598ee04f2ebbed3e947678dee048d10b5feca139435f75", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "6839fcf63d1f0d1c0f450abc8564a57c43d644077ab96f2934563e68b8a769d7"},
55
"dialyxir": {:hex, :dialyxir, "1.3.0", "fd1672f0922b7648ff9ce7b1b26fcf0ef56dda964a459892ad15f6b4410b5284", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "00b2a4bcd6aa8db9dcb0b38c1225b7277dca9bc370b6438715667071a304696f"},
6-
"earmark_parser": {:hex, :earmark_parser, "1.4.32", "fa739a0ecfa34493de19426681b23f6814573faee95dfd4b4aafe15a7b5b32c6", [:mix], [], "hexpm", "b8b0dd77d60373e77a3d7e8afa598f325e49e8663a51bcc2b88ef41838cca755"},
6+
"earmark_parser": {:hex, :earmark_parser, "1.4.33", "3c3fd9673bb5dcc9edc28dd90f50c87ce506d1f71b70e3de69aa8154bc695d44", [:mix], [], "hexpm", "2d526833729b59b9fdb85785078697c72ac5e5066350663e5be6a1182da61b8f"},
77
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
8-
"ex_doc": {:hex, :ex_doc, "0.29.4", "6257ecbb20c7396b1fe5accd55b7b0d23f44b6aa18017b415cb4c2b91d997729", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "2c6699a737ae46cb61e4ed012af931b57b699643b24dabe2400a8168414bc4f5"},
8+
"ex_doc": {:hex, :ex_doc, "0.30.4", "e8395c8e3c007321abb30a334f9f7c0858d80949af298302daf77553468c0c39", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "9a19f0c50ffaa02435668f5242f2b2a61d46b541ebf326884505dfd3dd7af5e4"},
99
"excoveralls": {:hex, :excoveralls, "0.16.1", "0bd42ed05c7d2f4d180331a20113ec537be509da31fed5c8f7047ce59ee5a7c5", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "dae763468e2008cf7075a64cb1249c97cb4bc71e236c5c2b5e5cdf1cfa2bf138"},
1010
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
1111
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~> 2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
1212
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
1313
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
1414
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
1515
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
16-
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
16+
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
1717
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
1818
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
1919
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
2020
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
21-
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
21+
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
2222
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
2323
}

0 commit comments

Comments
 (0)