Skip to content

Commit 48323ce

Browse files
committed
Refactor converge reduction
1 parent bb9cd60 commit 48323ce

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/integrant/core.cljc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,16 @@
507507
:conflicting-index index
508508
:expand-keys keys})))
509509

510+
(defn- find-in [m ks]
511+
(if (next ks)
512+
(-> (get-in m (butlast ks)) (find (last ks)))
513+
(find m (first ks))))
514+
515+
(defn- assoc-converge [m {:keys [index value override?]}]
516+
(if-some [[_ v] (find-in m index)]
517+
(if (or override? (= v {})) (assoc-in m index value) m)
518+
(assoc-in m index value)))
519+
510520
(defn converge
511521
"Deep-merge the values of a map. Raises an error on conflicting keys, unless
512522
one (and only one) of the values is tagged with the ^:override metadata."
@@ -515,10 +525,7 @@
515525
(let [converges (mapcat converge-values m)]
516526
(when-let [conflict (first (converge-conflicts converges))]
517527
(throw (converge-conflict-exception m conflict)))
518-
(->> converges
519-
(sort-by :override?)
520-
(sort-by #(not= (:value %) {}))
521-
(reduce #(assoc-in %1 (:index %2) (:value %2)) {}))))
528+
(reduce assoc-converge {} converges)))
522529

523530
(defn expand
524531
"Expand modules in the config map prior to initiation. The expand-key method

0 commit comments

Comments
 (0)