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]
|
||||
(keep-indexed #(when (pred %2) %1) coll))
|
||||
|
||||
(defn map-deep-merge-missing
|
||||
"Merges two maps but only keys missing from first map"
|
||||
[m1 m2]
|
||||
(into m1
|
||||
(for [k (keys m2)]
|
||||
(let [val1 (get m1 k)
|
||||
val2 (get m2 k)]
|
||||
(defn map-apply-defaults
|
||||
"Apply default values from [defaults] to keys not present in [conf]
|
||||
Order is very important.
|
||||
Thus all missing values from config are replaced by defaults"
|
||||
[conf defaults]
|
||||
(into conf
|
||||
(for [k (keys defaults)]
|
||||
(let [val1 (get conf k)
|
||||
val2 (get defaults k)]
|
||||
(if (and (map? val1)
|
||||
(map? val2))
|
||||
{k (map-deep-merge-missing val1 val2)}
|
||||
|
|
|
@ -2,11 +2,13 @@
|
|||
(:require [clojure.test :refer :all]
|
||||
[rss-thread-watch.utils :refer :all]))
|
||||
|
||||
(defn first-map {:a :b
|
||||
:c "c"
|
||||
|
||||
})
|
||||
(def first-map {:a :b
|
||||
:c "c"})
|
||||
(def empty-map {})
|
||||
|
||||
(deftest map-deep-merge-missing-test
|
||||
(testing "Map deep merge missing"
|
||||
(is ())))
|
||||
(testing "Default values in place of missing keys"
|
||||
(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