-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
280 lines (238 loc) · 11.3 KB
/
core.clj
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
(ns diff-as-list.core
(:require [clojure.test :as test]
[clojure.set :as _set]
[clojure.pprint :as pp]
[clojure.data :refer [diff]]
[clojure.test :as test]))
(defn- is-primitive? [val]
(contains? #{"java.lang.String" "java.lang.Long" "clojure.lang.Keyword"} (.getName (type val))))
(test/deftest primitive
(test/is (= true (is-primitive? "test")))
(test/is (is-primitive? 1))
(test/is (not (is-primitive? {})))
(test/is (not (is-primitive? []))))
(defn traverse-to-flat
([in-val] (traverse-to-flat in-val [] [] {}))
([in-val remaining-traversal in-path result]
;; (pp/pprint {;; :in-val in-val
;; :remaining-traversal remaining-traversal
;; :in-path in-path
;; :result result})
(if (nil? in-path)
result
(let [value (get-in in-val in-path ::no-value)]
(cond
(or (nil? value) (is-primitive? value))
(do
;; below assoces result into a map
(recur in-val
(rest remaining-traversal)
(first remaining-traversal)
(assoc result in-path {:type ::primitive :value value})))
(map? value)
(let [_keys (keys value)
first-key (first _keys)
rest-keys (rest _keys)
remain-trvrs-to-add (map #(conj in-path %) rest-keys)]
;; (pp/pprint {:remaining-traversal remaining-traversal
;; :remain-trvrs-to-add remain-trvrs-to-add})
(recur in-val
(concat remaining-traversal remain-trvrs-to-add)
(conj in-path first-key)
(assoc result in-path {:type ::map})))
:else
(throw (Exception. "don't know how to handle type " (.getName (type value)))))))))
;; (println "\n\n---- new compile ----")
;; (def test-map {:level-1-1 {:level-2-1 {:level-3-1 "level-3-1-val"
;; :level-3-2 {:level-4-1 "level-4-1-val"}}
;; :level-2-2 "level-2-2-val"
;; :level-2-3 "level-2-3-val"}
;; :level-1-2 "level-1-2-val"})
;; (println "\ntest-map")
;; (pp/pprint test-map)
;; (def test-map-flattened (traverse-to-flat test-map))
;; (println "\ntest-map-flattened")
;; (pp/pprint test-map-flattened)
;; (def test-map-2 {:level-1-1 {:level-2-1 {:level-3-1 "level-3-1-val"
;; :level-3-2 nil
;; }
;; :level-2-2 "level-2-2-val"
;; :level-2-3 "level-2-3-val"}
;; :level-1-2 "level-1-2-val"})
;; (println "\ntest-map-2")
;; (pp/pprint test-map-2)
;; (def test-map-2-flattened (traverse-to-flat test-map-2))
;; (println "\ntest-map-2-flattened")
;; (pp/pprint test-map-2-flattened)
(defn diffl [arg-1 arg-2]
(let [flattened-1 (traverse-to-flat arg-1)
flattened-2 (traverse-to-flat arg-2)
keys-in-1 (set (keys flattened-1))
keys-in-2 (set (keys flattened-2))
keys-in-both (_set/intersection keys-in-1 keys-in-2)
keys-in-both-compare (fn [_key]
(let [value-1 (get-in arg-1 _key ::not-found)
val-1-map-or-prim (as-> flattened-1 $
(get $ _key)
(:type $))
value-2 (get-in arg-2 _key ::not-found)
val-2-map-or-prim (as-> flattened-2 $
(get $ _key)
(:type $))
both-are-maps? (and (= ::map val-1-map-or-prim)
(= ::map val-2-map-or-prim))
both-are-prims? (and (= ::primitive val-1-map-or-prim)
(= ::primitive val-2-map-or-prim))]
(when-not (or both-are-maps?
(and both-are-prims?
(= value-1 value-2)))
{:path _key :val-1 value-1 :val-2 value-2})))
value-diffs (as-> keys-in-both $
(map #(keys-in-both-compare %) $)
(remove nil? $)
(vec $))
value-diff-paths (map :path value-diffs)
is-path-child-of-other-path? (fn [path-1 path-2]
(let [path-2-length (count path-2)
path-1-length (count path-1)]
(if (< path-1-length path-2-length)
false
(let [path-1-shortened (take path-2-length path-1)]
(= path-2 path-1-shortened)))))
is-missing-path-in-diffs? (fn [missing-path]
(some #(is-path-child-of-other-path? missing-path %) value-diff-paths))
remove-redundants (fn [keys-missing]
(let [sorted-by-length (set (sort-by count keys-missing))]
(reduce
(fn [accum key-path]
(if (some #(is-path-child-of-other-path? key-path %) accum)
accum
(conj accum key-path)))
[]
sorted-by-length)))
keys-missing-in-2 (as-> (_set/difference keys-in-1 keys-in-2) $
(remove is-missing-path-in-diffs? $)
(remove-redundants $)
(map #(hash-map :path % :value (get-in arg-1 %)) $))
keys-missing-in-1 (as-> (_set/difference keys-in-2 keys-in-1) $
(remove is-missing-path-in-diffs? $)
(remove-redundants $)
(map #(hash-map :path % :value (get-in arg-2 %)) $))]
{:keys-missing-in-1 keys-missing-in-1
:keys-missing-in-2 keys-missing-in-2
:value-differences value-diffs
:dal-version (System/getProperty "diff-as-list.version")}))
(defn- map-is-same? [map1 map2]
(let [the-diff (diff map1 map2)]
(and (nil? (first the-diff)) (nil? (nth the-diff 1)))))
;; (def diffl-val (diffl test-map test-map-2))
;; (println "\ndiffl-val")
;; (pp/pprint diffl-val)
(test/deftest initial
(let [expected {:keys-missing-in-1 [],
:keys-missing-in-2 [],
:value-differences [{:path [:what], :val-1 "nothing", :val-2 "who"}]}
actual (-> (diffl {:what "nothing"} {:what "who"})
(dissoc :dal-version))]
(test/is (map-is-same? expected actual))))
(test/deftest nil-arg
(let [expected {:keys-missing-in-1 [],
:keys-missing-in-2 [],
:value-differences [{:path [:what], :val-1 nil, :val-2 {:who "why"}}]}
actual (-> (diffl {:what nil} {:what {:who "why"}})
(dissoc :dal-version))]
(test/is (map-is-same? expected actual))))
(test/deftest missing-key
(let [map-1 {:level-1-1 {:level-2-1 {:level-3-1 "level-3-1-val"
:level-3-2 {:level-4-1 "level-4-1-val"}}
:level-2-2 "level-2-2-val"
:level-2-3 "level-2-3-val"}
:level-1-2 "level-1-2-val"}
map-2 {:level-1-1 {:level-2-1 {:level-3-1 "level-3-1-val"}
:level-2-2 "level-2-2-val"
:level-2-3 "level-2-3-val"}
:level-1-2 "level-1-2-val"}
expected {:keys-missing-in-1 [],
:keys-missing-in-2 [
{:path [:level-1-1 :level-2-1 :level-3-2]
:value {:level-4-1 "level-4-1-val"}}],
:value-differences []}
actual (-> (diffl map-1 map-2)
(dissoc :dal-version))]
;; (pp/pprint actual)
(test/is (map-is-same? expected actual))))
(defn patch [orig-map _diff]
(let [apply-value-diff (fn [accum value-diff]
(let [new-val (:val-2 value-diff)]
(if (nil? accum)
new-val
(assoc-in accum (:path value-diff) new-val))))
remove-key (fn [accum removed-key]
(let [removed-key-path (:path removed-key)]
(if (<= (count removed-key-path) 1)
(dissoc accum (first removed-key-path))
(update-in accum (drop-last removed-key-path) dissoc (last removed-key-path)))))
add-key (fn [accum added-key]
(assoc-in accum (:path added-key) (:value added-key)))]
(as-> orig-map $
;; (if (nil? $) {} $)
(reduce apply-value-diff $ (:value-differences _diff))
(reduce remove-key $ (:keys-missing-in-2 _diff))
(reduce add-key $ (:keys-missing-in-1 _diff))
)))
(test/deftest patch-initial
(let [orig-map {:what "nothing"}
_diff {:keys-missing-in-1 [],
:keys-missing-in-2 [],
:value-differences [{:path [:what], :val-1 "nothing", :val-2 "who"}]}
expected {:what "who"}
actual (patch orig-map _diff)]
;; (pp/pprint actual)
(test/is (= expected actual))))
(test/deftest patch-nil-arg
(let [orig-map {:what nil}
_diff {:keys-missing-in-1 [],
:keys-missing-in-2 [],
:value-differences [{:path [:what], :val-1 nil, :val-2 {:who "why"}}]}
expected {:what {:who "why"}}
actual (patch orig-map _diff)]
(test/is (= expected actual))))
(test/deftest patch-key-removed
(let [orig-map {:what "one"
:who "two"}
_diff {:keys-missing-in-1 []
:keys-missing-in-2 [{:path [:who] :value "two"}]
:value-differences []}
expected {:what "one"}
actual (patch orig-map _diff)]
(test/is (= expected actual))))
(test/deftest patch-key-removed-nested
(let [orig-map {:what {:why "who"}}
_diff {:keys-missing-in-1 []
:keys-missing-in-2 [{:path [:what :why] :value "who"}]
:value-differences []}
expected {:what {}}
actual (patch orig-map _diff)
]
(test/is (= expected actual))))
;; (def/deftest patch-added-key) !!!!!!
(test/deftest patch-key-added
(let [orig-map {:what {}}
_diff {:keys-missing-in-1 [{:path [:what :why] :value "who"}]
:keys-missing-in-2 []
:value-differences []}
expected {:what {:why "who"}}
actual (patch orig-map _diff)]
(test/is (= expected actual))))
(test/deftest patch-nil-to-value
(let [orig-map nil
_diff {:keys-missing-in-1 (), :keys-missing-in-2 (),
:value-differences [{:path [], :val-1 nil, :val-2 {:what "who"}}]}
expected {:what "who"}
actual (patch orig-map _diff)
]
(test/is (= expected actual))))
(defn get-version []
(slurp "VERSION")
;; (some-> (io/resource "project.clj") slurp edn/read-string (nth 2))
)