forked from daviddpark/clj-json-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cljc
447 lines (422 loc) · 21 KB
/
util.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
(ns clj-json-patch.util
(:require #?(:clj [cheshire.core :as json])
[clojure.string :as clo-str]))
(declare remove-patch-value)
(declare transform-moves)
(declare diff-vecs)
(declare gen-op)
(def ^:dynamic *keywordize* false)
(defn diff*
"Prepares a JSON patch document representing the difference
between two JSON objects."
[obj1 obj2 prefix]
(transform-moves obj1 obj2
(cond (and (vector? obj1) (vector? obj2))
(diff-vecs obj1 obj2 prefix)
(and (map? obj1) (map? obj2))
(vec
(flatten
(remove nil?
(concat
(for [[k v1] obj1]
(let [k* (name k)
v2 (get obj2 k)]
(cond (and (vector? v1) (vector? v2))
(diff* v1 v2 (str prefix k* "/"))
(not (contains? obj2 k))
(gen-op ["remove" (str prefix k*)])
(and (map? v1) (map? v2))
(diff* v1 v2 (str prefix k* "/"))
(and (contains? obj2 k)
(not= v1 v2))
(gen-op ["replace" (str prefix k*) v2]))))
(for [[k v2] obj2]
(let [k* (name k)
v1 (get obj1 k)]
(cond (not (contains? obj1 k))
(gen-op ["add" (str prefix k*) v2])))))))))))
(defn eval-escape-characters
[segment]
(clo-str/replace segment #"(~0|~1)"
(fn [[_ s]] (cond (= s "~0") "~"
(= s "~1") "/"))))
(defn inject-escape-characters
[segment]
(clo-str/replace segment #"(~|\/)"
(fn [[_ s]] (cond (= s "~") "~0"
(= s "/") "~1"))))
(defn ->key [seg]
(if *keywordize*
(keyword seg)
seg))
(defn has-path?
"given the patch path, determines if the path exists in the obj"
[obj path]
(let [path (if (.startsWith path "#") (subs path 1) path)]
(cond
(and obj (or (= path "") (= path "#")))
true
(and (= path "/") (map? obj) (contains? obj ""))
true
:else
(if-let [match (re-find #"^/([^/]+)(.*)" path)]
(let [seg (eval-escape-characters (second match))
segs (nth match 2)
[h-path? val] (let [ky (if (vector? obj)
(Integer/parseInt seg)
(->key seg))]
[(contains? obj ky)
(get obj ky)])]
;;(println :HAS "seg:" seg "segs:" segs "val:" val "h-path?" h-path?)
(if (and h-path? (not (empty? segs)))
(has-path? val segs)
h-path?))))))
(defn get-patch-value
"Given the patch path, find the associated value."
[obj path]
(let [path (if (clo-str/starts-with? path "#") (subs path 1) path)]
(cond
(or (= path "") (= path "#"))
obj
(and (= path "/") (map? obj) (get obj ""))
(get obj "")
:else
(if-let [match (re-find #"^/([^/]+)(.*)" path)]
(let [seg (eval-escape-characters (second match))
segs (nth match 2)
val (cond (map? obj)
(get obj (->key seg))
(vector? obj)
(nth obj #?(:clj (Integer/parseInt seg)
:cljs (js/parseInt seg))))]
;(println "seg:" seg "segs:" segs "val:" val)
(if-not (empty? segs)
(get-patch-value val segs)
val))))))
(defn set-patch-value
"Set val at path in obj"
[obj path val & [insert]]
(if-let [segs (re-seq #"/([^/]+)" path)]
(if (> (count segs) 1)
(if-let [path-exists (try (get-patch-value obj path)
#?(:clj (catch Exception e
(throw (Exception. (str "Unable to set value at '" path "'."))))
:cljs (catch js/Object e
(throw (js/Error. (str "Unable to set value at '" path "'."))))))]
(let [parent-path (apply str (map first (take (dec (count segs)) segs)))
parent (get-patch-value obj parent-path)]
(set-patch-value obj parent-path
(set-patch-value parent (first (last segs)) val)))
#?(:clj (throw (Exception. (str "Unable to set value at '" path
"'. Consider adding a more explicit data "
"structure as a child of an existing object.")))
:cljs (throw (js/Error. (str "Unable to set value at '" path
"'. Consider adding a more explicit data "
"structure as a child of an existing object.")))))
(cond (map? obj)
(assoc obj (->key (eval-escape-characters (second (first segs))))
val)
(vector? obj)
(let [idx #?(:clj (Integer/parseInt (second (re-find #"/(\d+)" path)))
:cljs (js/parseInt (second (re-find #"/(\d+)" path))))]
(try
(vec (concat (subvec obj 0 idx)
[val]
(subvec obj (if insert idx (inc idx)))))
#?(:clj (catch Exception e
(throw (Exception. (str "Unable to set value at " idx "."))))
:cljs (catch js/Object e
(throw (js/Error. (str "Unable to set value at " idx ".")))))))))
#?(:clj (throw (Exception. "Patch path must start with '/'"))
:cljs (throw (js/Error. "Patch path must start with '/'"))))) ;Should be throwing an exception
(defn add-patch-value
"Add val at path in obj"
[obj path val]
(if-let [segs (re-seq #"/([^/]+)" path)]
(if (> (count segs) 1)
(let [parent-path (apply str (map first (take (dec (count segs)) segs)))
parent (get-patch-value obj parent-path)]
(if (vector? parent)
(let [str-idx (last (last segs))]
(if (or (= "-" str-idx)
(= (count parent) #?(:clj (try (Integer/parseInt str-idx)
(catch java.lang.NumberFormatException e
(throw (Exception. (str "Unable to determine array index from '" str-idx "'.")))))
:cljs (if (js/isNaN (js/parseInt str-idx))
(throw (js/Error. (str "Unable to determine array index from '" str-idx "'.")))
(js/parseInt str-idx)))))
(set-patch-value obj parent-path
(conj parent val))
(let [first-last-seg (first (last segs))
insert (.endsWith path str-idx)]
(set-patch-value obj parent-path
(set-patch-value parent (first (last segs)) val insert)))))
(if-let [path-exists (try (get-patch-value obj parent-path)
#?(:clj (catch Exception e
(throw (Exception. (str "Unable to set value at '" path "'."))))
:cljs (catch js/Object e
(throw (js/Error. (str "Unable to set value at '" path "'."))))))]
(set-patch-value obj parent-path
(set-patch-value parent (first (last segs)) val))
#?(:clj (throw (Exception. (str "Unable to set value at '" path
"'. Consider adding a more explicit data "
"structure as a child of an existing object.")))
:cljs (throw (js/Error. (str "Unable to set value at '" path
"'. Consider adding a more explicit data "
"structure as a child of an existing object.")))))))
(set-patch-value obj path val))
#?(:clj (throw (Exception. "Patch path must start with '/'"))
:cljs (throw (js/Error. "Patch path must start with '/'")))))
(defn move-patch-value
"Move value located at 'from' to the 'path'."
[obj from path]
(if-let [to-segs (re-seq #"/([^/]+)" path)]
(if-let [from-segs (re-seq #"/([^/]+)" from)]
(let [val (get-patch-value obj from)]
(if (some? val)
(if (> (count to-segs) 1)
(let [from-parent-path (apply str (map first (take (dec (count from-segs)) from-segs)))
to-parent-path (apply str (map first (take (dec (count to-segs)) to-segs)))
parent (get-patch-value obj to-parent-path)]
(if (= from-parent-path to-parent-path)
(set-patch-value obj from-parent-path
(move-patch-value (get-patch-value obj from-parent-path)
(first (last from-segs))
(first (last to-segs))))
(set-patch-value (remove-patch-value obj from)
to-parent-path
(set-patch-value parent (first (last to-segs)) val))))
(cond (map? obj)
(-> obj
(assoc (->key (second (first to-segs))) val)
(dissoc (->key (second (first from-segs)))))
(vector? obj)
(let [from-int #?(:clj (try (Integer/parseInt (second (re-find #"/(\d+)" from)))
(catch Exception e
(throw (Exception. (str "Move attempted on value that does not exist at '" from "'.")))))
:cljs (try (js/parseInt (second (re-find #"/(\d+)" from)))
(catch js/Object e
(throw (js/Error. (str "Move attempted on value that does not exist at '" from "'."))))))
to-int #?(:clj (try (Integer/parseInt (second (re-find #"/(\d+)" path)))
(catch Exception e
(throw (Exception. (str "Move attempted on value that does not exist at '" path "'.")))))
:cljs (try (js/parseInt (second (re-find #"/(\d+)" path)))
(catch js/Object e
(throw (js/Error. (str "Move attempted on value that does not exist at '" path "'."))))))]
(vec (concat (subvec obj 0 from-int) (subvec obj (inc from-int) (inc to-int))
[(get obj from-int)] (subvec obj (inc to-int)))))))
#?(:clj (throw (Exception. (str "Move attempted on value that does not exist at '" from "'.")))
:cljs (throw (js/Error. (str "Move attempted on value that does not exist at '" from "'."))))))
#?(:clj (throw (Exception. "Patch 'from' value must start with '/'"))
:cljs (throw (js/Error. "Patch 'from' value must start with '/'"))))
#?(:clj (throw (Exception. "Patch 'path' value must start with '/'"))
:cljs (throw (js/Error. "Patch 'path' value must start with '/'")))))
(defn replace-patch-value
"Replace the value found at 'path' with that bound to 'val'."
[obj path val]
;;(println "path" obj path val (has-path? obj path))
(if (has-path? obj path)
(if-let [segs (re-seq #"/([^/]+)" path)]
(if (> (count segs) 1)
(let [parent-path (apply str (map first (take (dec (count segs)) segs)))
parent (get-patch-value obj parent-path)]
(replace-patch-value obj parent-path
(replace-patch-value parent (first (last segs)) val)))
(cond (map? obj)
(assoc obj (->key (second (first segs))) val)
(vector? obj)
(let [idx (Integer/parseInt (second (re-find #"/(\d+)" path)))]
(vec (concat (subvec obj 0 idx)
[val]
(subvec obj (inc idx)))))))
(throw (Exception. "Patch path must start with '/'")))
(throw (Exception. (str "Can't replace a value that does not exist at '" path "'.")))))
(defn remove-patch-value
"Remove the value at 'path' from obj."
[obj path]
(try
(if (has-path? obj path)
(if-let [segs (re-seq #"/([^/]+)" path)]
(if (> (count segs) 1)
(let [parent-path (apply str (map first (take (dec (count segs)) segs)))
parent (get-patch-value obj parent-path)]
(replace-patch-value obj parent-path
(remove-patch-value parent (first (last segs)))))
(cond (map? obj)
(dissoc obj (->key (second (first segs))))
(vector? obj)
(let [idx #?(:clj (Integer/parseInt (second (re-find #"/(\d+)" path)))
:cljs (js/parseInt (second (re-find #"/(\d+)" path))))]
(vec (concat (subvec obj 0 idx)
[val]
(subvec obj (inc idx)))))))
#?(:clj (throw (Exception. "Patch path must start with '/'"))
:cljs (throw (js/Error. "Patch path must start with '/'"))))
#?(:clj (throw (Exception. (str "Can't replace a value that does not exist at '" path "'.")))
:cljs (throw (js/Error. (str "Can't replace a value that does not exist at '" path "'.")))))))
(defn remove-patch-value-func
"Remove the value at 'path' from obj."
[obj path]
(let [val (get-patch-value obj path)]
(if (some? val)
(if-let [segs (re-seq #"/([^/]+)" path)]
(if (> (count segs) 1)
(let [parent-path (apply str (map first (take (dec (count segs)) segs)))
parent (get-patch-value obj parent-path)]
(replace-patch-value obj parent-path
(remove-patch-value parent (first (last segs)))))
(cond (map? obj)
(dissoc obj (second (first segs)))
(vector? obj)
(let [idx #?(:clj (Integer/parseInt (second (re-find #"/(\d+)" path)))
:cljs (js/parseInt (second (re-find #"/(\d+)" path))))]
(vec (concat (subvec obj 0 idx) (subvec obj (inc idx))))))))
#?(:clj (throw (Exception. (str "There is no value at '" path "' to remove.")))
:cljs (throw (js/Error. (str "There is no value at '" path "' to remove.")))))))
(defn remove-patch-value
"Remove the value at 'path' from obj."
[obj path]
(try (remove-patch-value-func obj path)
#?(:clj (catch Exception e
(throw (Exception. (str "There is no value at '" path "' to remove."))))
:cljs (catch js/Object e
(throw (js/Error. (str "There is no value at '" path "' to remove.")))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Works for both clj & cljs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn test-patch-value
"Ensure that the value located at 'path' in obj is equal to 'val'."
[obj path val]
(try
(let [value (get-patch-value obj path)]
(if (not= val value)
#?(:clj (throw (Exception. (str "The value is: " (json/generate-string value))))
:cljs (throw (js/Error. (str "The value is: " (.stringify js/JSON (clj->js value))))))
obj))
#?(:clj (catch Exception e
(throw (Exception.
(str "The test failed. "
(json/generate-string val)
" is not found at " path ". "
(.getMessage e)))))
:cljs (catch js/Object e
(throw (js/Error.
(str "The test failed. "
(.stringify js/JSON (clj->js val))
" is not found at " path ". "
(e.message))))))))
(defn apply-patch [obj patch]
"Apply the patch operation in patch to obj, returning the new obj representation."
(let [op (or (get patch "op")
(get patch :op))
path (or (get patch "path")
(get patch :path))
from (or (get patch "from")
(get patch :from))
value (or (get patch "value")
(get patch :value))]
(cond (= "add" op)
(add-patch-value obj path value)
(= "move" op)
(move-patch-value obj from path)
(= "remove" op)
(remove-patch-value obj path)
(= "replace" op)
(replace-patch-value obj path value)
(= "test" op)
(test-patch-value obj path value))))
(defn gen-op [t]
[(let [result {"op" (first t) "path" (second t)}]
(if (> (count t) 2)
(assoc result "value" (nth t 2))
result))])
(defn clean-prefix
[prefix path]
(clo-str/replace path (re-pattern prefix) "/"))
(defn sanitize
[prefix patch]
(let [path (get patch "path")
cleaned-path (assoc patch "path" (clean-prefix prefix path))]
(if-let [from (get patch "from")]
(assoc cleaned-path "from" (clean-prefix prefix from))
cleaned-path)))
(defn sanitize-prefix-in-patch
[prefix idx patch]
(if (vector? patch)
(map (partial sanitize (str prefix idx)) patch)
(sanitize prefix patch)))
(defn diff-vecs [obj1 obj2 prefix]
(loop [v1 obj1
v2 obj2
i 0
ops []]
(cond (and (empty? v1) (empty? v2))
ops
(and (> (count ops) 0)
(= v2
(reduce
#(apply-patch %1 %2) v1
(map (partial sanitize-prefix-in-patch prefix (dec i)) ops))))
ops
(= (set v1) (set v2))
(cond (= i (count v1))
ops
(= (get v1 i) (get v2 i))
(recur v1 v2 (inc i) ops)
(not= (get v1 i) (get v2 i))
(let [moved-idx (first (filter (complement nil?) (map-indexed #(if (= (get v1 i) %2) %1) v2)))]
(recur v1 v2 (inc i)
(conj ops {"op" "move" "from" (str prefix i) "path" (str prefix moved-idx)}))))
(= v1 (rest v2))
(conj ops (gen-op ["add" (str prefix i) (first v2)]))
(= (rest v1) v2)
(conj ops (gen-op ["remove" (str prefix i)]))
(empty? v1)
(recur v1 (rest v2) (inc i)
(conj ops (gen-op ["add" (str prefix i) (first v2)])))
(empty? v2)
(recur (rest v1) v2 (inc i)
(conj ops (gen-op ["remove" (str prefix i)])))
(not= (first v1) (first v2))
(if (and (map? (first v1)) (map? (first v2)))
(recur (rest v1) (rest v2) (inc i)
(conj ops (diff* (first v1) (first v2) (str prefix i "/"))))
(recur (rest v1) (rest v2) (inc i)
(conj ops (gen-op ["replace" (str prefix i) (first v2)]))))
(and (= (first v1) (first v2))
(not= (rest v1) (rest v2)))
(recur (rest v1) (rest v2) (inc i) ops))))
(defn get-value-path
"Traverses obj, looking for a value that matches val, returns path to value."
([obj val] (get-value-path obj val "/"))
([obj val prefix]
(cond (map? obj)
(some identity
(concat
(for [[k v] obj]
(if (= v val)
(str prefix (inject-escape-characters k))
(if-not (string? v)
(get-value-path v val (str prefix (inject-escape-characters k) "/")))))))
(vector? obj)
(if-let [idx (some identity (map-indexed #(if (= val %2) %1) obj))]
(str prefix idx)
(map-indexed #(get-value-path %2 val (str prefix %1 "/")) obj)))))
(defn transform-moves
"Attempt to reconcile add/remove patch entries
to a single move entry"
[obj1 obj2 patch]
(loop [adds (filter #(= "add" (get % "op")) patch)
removes (filter #(= "remove" (get % "op")) patch)
p patch]
(if (or (empty? adds) (empty? removes))
p
(let [f-add (first adds)
f-path (get f-add "path")
f-val (get f-add "value")
moved (filter #(= f-val (get-patch-value obj1 (get % "path"))) removes)]
(if-let [fmoved (first moved)]
(recur (rest adds) (filter #(not= fmoved %) removes)
(conj (filter #(not= f-add %) (filter #(not= fmoved %) patch))
{"op" "move" "from" (get fmoved "path") "path" f-path}))
(recur (rest adds) removes p))))))