Refactor map-deep-merge-missing -> map-apply-defaults; Tests for map-apply-defaults
Needs more tests
This commit is contained in:
parent
7679844e08
commit
68a74cf1b8
2 changed files with 17 additions and 13 deletions
|
@ -41,13 +41,15 @@
|
||||||
[pred coll]
|
[pred coll]
|
||||||
(keep-indexed #(when (pred %2) %1) coll))
|
(keep-indexed #(when (pred %2) %1) coll))
|
||||||
|
|
||||||
(defn map-deep-merge-missing
|
(defn map-apply-defaults
|
||||||
"Merges two maps but only keys missing from first map"
|
"Apply default values from [defaults] to keys not present in [conf]
|
||||||
[m1 m2]
|
Order is very important.
|
||||||
(into m1
|
Thus all missing values from config are replaced by defaults"
|
||||||
(for [k (keys m2)]
|
[conf defaults]
|
||||||
(let [val1 (get m1 k)
|
(into conf
|
||||||
val2 (get m2 k)]
|
(for [k (keys defaults)]
|
||||||
|
(let [val1 (get conf k)
|
||||||
|
val2 (get defaults k)]
|
||||||
(if (and (map? val1)
|
(if (and (map? val1)
|
||||||
(map? val2))
|
(map? val2))
|
||||||
{k (map-deep-merge-missing val1 val2)}
|
{k (map-deep-merge-missing val1 val2)}
|
||||||
|
|
|
@ -2,11 +2,13 @@
|
||||||
(:require [clojure.test :refer :all]
|
(:require [clojure.test :refer :all]
|
||||||
[rss-thread-watch.utils :refer :all]))
|
[rss-thread-watch.utils :refer :all]))
|
||||||
|
|
||||||
(defn first-map {:a :b
|
(def first-map {:a :b
|
||||||
:c "c"
|
:c "c"})
|
||||||
|
(def empty-map {})
|
||||||
})
|
|
||||||
|
|
||||||
(deftest map-deep-merge-missing-test
|
(deftest map-deep-merge-missing-test
|
||||||
(testing "Map deep merge missing"
|
(testing "Default values in place of missing keys"
|
||||||
(is ())))
|
(is (= first-map (map-apply-defaults first-map empty-map))
|
||||||
|
"No defaults should return conf map unchanged")
|
||||||
|
(is (= first-map (map-apply-defaults empty-map first-map))
|
||||||
|
"Empty map should be completely replaced by defaults")))
|
||||||
|
|
Loading…
Reference in a new issue