Skip to content

Commit 4828d93

Browse files
authored
Merge pull request #49 from djblue/diff-meta
Diff / preserve metadata on collections
2 parents e940bd1 + 2ad3dc1 commit 4828d93

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Added
44

5+
- Diff / preserve metadata on collections
6+
57
## Fixed
68

79
Varying key order in maps should produce a consistent diff (#47)

src/lambdaisland/deep_diff2/diff_impl.cljc

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[clojure.set :as set]
44
[lambdaisland.clj-diff.core :as seq-diff]))
55

6-
(declare diff diff-similar)
6+
(declare diff diff-similar diff-meta)
77

88
(defrecord Mismatch [- +])
99
(defrecord Deletion [-])
@@ -92,40 +92,50 @@
9292

9393
(defn diff-seq [exp act]
9494
(let [[rep del ins] (replacements (del+ins exp act))]
95-
(->> exp
96-
(diff-seq-replacements rep)
97-
(diff-seq-deletions del)
98-
(diff-seq-insertions ins)
99-
(into []))))
95+
(with-meta
96+
(->> exp
97+
(diff-seq-replacements rep)
98+
(diff-seq-deletions del)
99+
(diff-seq-insertions ins)
100+
(into []))
101+
(diff-meta exp act))))
100102

101103
(defn diff-set [exp act]
102-
(into
103-
(into #{}
104-
(map (fn [e]
105-
(if (contains? act e)
106-
e
107-
(->Deletion e))))
108-
exp)
109-
(map ->Insertion)
110-
(remove #(contains? exp %) act)))
104+
(with-meta
105+
(into
106+
(into #{}
107+
(map (fn [e]
108+
(if (contains? act e)
109+
e
110+
(->Deletion e))))
111+
exp)
112+
(map ->Insertion)
113+
(remove #(contains? exp %) act))
114+
(diff-meta exp act)))
111115

112116
(defn diff-map [exp act]
113-
(let [exp-ks (set (keys exp))
114-
act-ks (set (keys act))]
115-
(reduce
116-
(fn [m k]
117-
(case [(contains? exp-ks k) (contains? act-ks k)]
118-
[true false]
119-
(assoc m (->Deletion k) (get exp k))
120-
[false true]
121-
(assoc m (->Insertion k) (get act k))
122-
[true true]
123-
(assoc m k (diff (get exp k) (get act k)))
117+
(with-meta
118+
(let [exp-ks (set (keys exp))
119+
act-ks (set (keys act))]
120+
(reduce
121+
(fn [m k]
122+
(case [(contains? exp-ks k) (contains? act-ks k)]
123+
[true false]
124+
(assoc m (->Deletion k) (get exp k))
125+
[false true]
126+
(assoc m (->Insertion k) (get act k))
127+
[true true]
128+
(assoc m k (diff (get exp k) (get act k)))
124129
; `[false false]` will never occur because `k` necessarily
125130
; originated from at least one of the two sets
126-
))
127-
{}
128-
(set/union exp-ks act-ks))))
131+
))
132+
{}
133+
(set/union exp-ks act-ks)))
134+
(diff-meta exp act)))
135+
136+
(defn diff-meta [exp act]
137+
(when (or (meta exp) (meta act))
138+
(diff-map (meta exp) (meta act))))
129139

130140
(defn primitive? [x]
131141
(or (number? x) (string? x) (boolean? x) (inst? x) (keyword? x) (symbol? x)))

test/lambdaisland/deep_diff2/diff_test.cljc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
(is (= []
3232
(diff/diff [] [])))
3333

34+
(is (= {:meta true}
35+
(meta (diff/diff ^:meta [] ^:meta []))))
36+
3437
(is (= [1 2 3]
3538
(diff/diff (into-array [1 2 3]) [1 2 3])))
3639

@@ -68,6 +71,9 @@
6871
(is (= #{:a}
6972
(diff/diff #{:a} #{:a})))
7073

74+
(is (= {:meta true}
75+
(meta (diff/diff ^:meta #{} ^:meta #{}))))
76+
7177
(is (= #{(diff/->Insertion :a)}
7278
(diff/diff #{} #{:a})))
7379

@@ -80,6 +86,9 @@
8086
(testing "maps"
8187
(is (= {} (diff/diff {} {})))
8288

89+
(is (= {:meta true}
90+
(meta (diff/diff ^:meta {} ^:meta {}))))
91+
8392
(is (= {:a (diff/->Mismatch 1 2)}
8493
(diff/diff {:a 1} {:a 2})))
8594

0 commit comments

Comments
 (0)