Skip to content

Commit

Permalink
Refactor converge reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed May 15, 2024
1 parent bb9cd60 commit 48323ce
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/integrant/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,16 @@
:conflicting-index index
:expand-keys keys})))

(defn- find-in [m ks]
(if (next ks)
(-> (get-in m (butlast ks)) (find (last ks)))
(find m (first ks))))

(defn- assoc-converge [m {:keys [index value override?]}]
(if-some [[_ v] (find-in m index)]
(if (or override? (= v {})) (assoc-in m index value) m)
(assoc-in m index value)))

(defn converge
"Deep-merge the values of a map. Raises an error on conflicting keys, unless
one (and only one) of the values is tagged with the ^:override metadata."
Expand All @@ -515,10 +525,7 @@
(let [converges (mapcat converge-values m)]
(when-let [conflict (first (converge-conflicts converges))]
(throw (converge-conflict-exception m conflict)))
(->> converges
(sort-by :override?)
(sort-by #(not= (:value %) {}))
(reduce #(assoc-in %1 (:index %2) (:value %2)) {}))))
(reduce assoc-converge {} converges)))

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

0 comments on commit 48323ce

Please sign in to comment.