Back
expand (clj)
(source)function
(expand config)
(expand config keys)
Expand modules in the config map prior to initiation. The expand-key method
is applied to each entry in the map, and the results deep-merged together to
produce a new configuration.
If there are conflicting keys with different values, an exception will be
raised. Conflicts can be resolved by tagging one value with the :override
metadata key.
Examples
integrant
(ns integrant.core-test
(:require #?(:clj [clojure.test :refer [are deftest is testing]]
:cljs [cljs.test :refer-macros [are deftest is testing]])
[integrant.core :as ig]
[weavejester.dependency :as dep]))
(defmethod ig/expand-key ::mod [_ v] {::a v, ::b {:v v}})
(defmethod ig/expand-key ::mod-a [_ v] {::a v})
(defmethod ig/expand-key ::mod-b [_ v] {::b {:v v}})
(defmethod ig/expand-key ::mod-c [_ v] {::c {:x {:y {:z v}}}})
(defmethod ig/expand-key ::mod-z [_ v] {::z v})
(deftest expand-test
(testing "default expand"
(is (= (ig/expand {::unique 1})
{::unique 1})))
(testing "empty map values"
(is (= (ig/expand {::unique {}})
{::unique {}}))
(is (= (ig/expand {::a {}, ::mod-a {:x 1}})
{::a {:x 1}}))
(is (= (ig/expand {::z {}, ::mod-z {:x 1}})
{::z {:x 1}})))
(testing "single expand"
(is (= (ig/expand {::mod 1})
{::a 1, ::b {:v 1}})))
(testing "expand with unrelated keys"
(is (= (ig/expand {::mod 1, ::b {:x 1}, ::c 2})
{::a 1, ::b {:v 1, :x 1}, ::c 2})))
(testing "expand with direct override"
(is (= (ig/expand {::mod {:x 1}, ::a ^:override {:x 2}})
{::a {:x 2}, ::b {:v {:x 1}}})))
(testing "expand with nested override"
(is (= (ig/expand {::mod 1, ::b ^:override {:v 2}})
{::a 1, ::b {:v 2}}))
(is (= (ig/expand {::mod-c 1, ::c ^:override {:x {:y {:z 2}}}})
{::c {:x {:y {:z 2}}}})))
(testing "unresolved conflicting index"
(is (thrown-with-msg?
#?(:clj clojure.lang.ExceptionInfo :cljs cljs.core.ExceptionInfo)
(re-pattern (str "^Conflicting values at index "
"\\[:integrant\\.core-test/a\\] "
"for expansions: :integrant\\.core-test/mod, "
":integrant\\.core-test/mod-a\\. Use the "
"\\^:override metadata to set the preferred "
"value\\.$"))
(ig/expand {::mod 1, ::mod-a 2}))))
(testing "unresolved conflicting nested index"
(is (thrown-with-msg?
#?(:clj clojure.lang.ExceptionInfo :cljs cljs.core.ExceptionInfo)
(re-pattern (str "^Conflicting values at index "
"\\[:integrant\\.core-test/b :v\\] "
"for expansions: :integrant\\.core-test/mod, "
":integrant\\.core-test/mod-b\\. Use the "
"\\^:override metadata to set the preferred "
"value\\.$"))
(ig/expand {::mod 1, ::mod-b 2}))))
(testing "resolved conflict"
(is (= (ig/expand {::mod {:x 1}, ::mod-a {:x 2}, ::a ^:override {:x 3}})
{::a {:x 3}, ::b {:v {:x 1}}})))
(testing "resolved nested conflict"
(is (= (ig/expand {::mod 1, ::mod-b 2, ::b ^:override {:v 3}})
{::a 1, ::b {:v 3}}))
(is (= (ig/expand {[::one ::mod-c] 1
[::two ::mod-c] 2
::c ^:override {:x {:y {:z 3}}}})
{::c {:x {:y {:z 3}}}})))
(testing "expand with refs"
(let [m {::a (ig/ref ::b) ::b 1}]
(is (= m (ig/expand m))))
(let [m {::a (ig/refset ::b) ::b 1}]
(is (= m (ig/expand m))))))
jacekschae/learn-reitit-course-files
(ns user
(:require [integrant.repl :as ig-repl]
[integrant.core :as ig]
[integrant.repl.state :as state]
[cheffy.server]
[next.jdbc :as jdbc]
[next.jdbc.sql :as sql]))
(pprint (macroexpand '(ns coercion (:require [clojure.pprint :refer [pprint]]))))