diff --git a/src/rss_thread_watch/utils.clj b/src/rss_thread_watch/utils.clj index 98e3b20..847637b 100644 --- a/src/rss_thread_watch/utils.clj +++ b/src/rss_thread_watch/utils.clj @@ -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)} diff --git a/test/rss_thread_watch/utils_test.clj b/test/rss_thread_watch/utils_test.clj index 332dc54..6673fee 100644 --- a/test/rss_thread_watch/utils_test.clj +++ b/test/rss_thread_watch/utils_test.clj @@ -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")))