Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nil component #39

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
26 changes: 20 additions & 6 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,18 @@
:declared-deps deps
:remaining-deps (seq deps)}))

(defn- build-obj [built-map head]
(let [factory (:factory head)
(defn- build-obj [built-map stack]
slaughtlaught marked this conversation as resolved.
Show resolved Hide resolved
(let [head (peek stack)
factory (:factory head)
declared-deps (:declared-deps head)
built-deps (select-keys built-map (keys declared-deps))]
(p/build factory built-deps)))
(try
(p/build factory built-deps)
(catch RuntimeException ex
(throw (ex-info "An error during component start"
slaughtlaught marked this conversation as resolved.
Show resolved Hide resolved
{:stack (map :key stack)}
ex))))))


(defn- build [{:keys [registry *stop-list]} key]
(loop [stack (list (stack-frame key :required (registry key)))
Expand Down Expand Up @@ -126,7 +133,7 @@
built-map))

:else
(let [obj (build-obj built-map head)
(let [obj (build-obj built-map stack)
stop #(p/demolish factory obj)]
(vswap! *stop-list conj stop)
(case [obj dep-type]
Expand Down Expand Up @@ -592,12 +599,18 @@
(defn- stop-fn [variable]
(-> variable meta (::stop (fn no-op [_]))))

(defn- check-nil-component! [component var]
slaughtlaught marked this conversation as resolved.
Show resolved Hide resolved
(if (nil? component)
(throw (ex-info (str "nil component " var) {}))
component))

(defn- var->0-component [variable]
(let [stop (stop-fn variable)]
(reify p/Factory
(dependencies [_])
(build [_ _]
(variable))
(-> (variable)
(check-nil-component! variable)))
(demolish [_ obj]
(stop obj)))))

Expand All @@ -608,7 +621,8 @@
(dependencies [_]
deps)
(build [_ deps]
(variable deps))
(-> (variable deps)
(check-nil-component! variable)))
(demolish [_ obj]
(stop obj)))))

Expand Down
17 changes: 17 additions & 0 deletions test/darkleaf/di/component_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns darkleaf.di.component-test
(:require
[clojure.test :as t]
[darkleaf.di.core :as di])
(:import
(clojure.lang ExceptionInfo)))

(defn nil-component
{::di/kind :component}
[]
slaughtlaught marked this conversation as resolved.
Show resolved Hide resolved
nil)

(t/deftest nil-component-test
(t/is (thrown-with-msg?
ExceptionInfo
#"\Anil component #'darkleaf.di.component-test/nil-component\z"
slaughtlaught marked this conversation as resolved.
Show resolved Hide resolved
(di/start `nil-component))))
Loading