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
28 changes: 23 additions & 5 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,21 @@
:declared-deps deps
:remaining-deps (seq deps)}))

(defn- build-obj [built-map head]
(defn- build-obj* [built-map head]
(let [factory (:factory head)
declared-deps (:declared-deps head)
built-deps (select-keys built-map (keys declared-deps))]
(p/build factory built-deps)))

(defn- build-obj [built-map stack]
(try
(build-obj* built-map (peek stack))
(catch Exception ex
(throw (ex-info "An error during component build"
{:type ::build-error
:stack (map :key stack)}
ex)))))

(defn- build [{:keys [registry *stop-list]} key]
(loop [stack (list (stack-frame key :required (registry key)))
built-map {}]
Expand Down Expand Up @@ -126,7 +135,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 +601,19 @@
(defn- stop-fn [variable]
(-> variable meta (::stop (fn no-op [_]))))

(defn- validate-component-obj! [obj variable]
(when (nil? obj)
(throw (ex-info "A component fn should not return nil"
{:variable variable}))))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я не смог придумать нормальный :type.
И там ниже в var->factory-defn тоже нет type.

Если сможете придумать, будет здорово, нет - тоже ок.


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

Expand All @@ -608,7 +624,9 @@
(dependencies [_]
deps)
(build [_ deps]
(variable deps))
(let [obj (variable deps)]
(validate-component-obj! obj variable)
obj))
(demolish [_ obj]
(stop obj)))))

Expand Down Expand Up @@ -641,7 +659,7 @@
[0] (var->0-component variable)
[1] (var->1-component variable)
(throw (ex-info
"The component must only have 0 or 1 arity"
"A component fn must only have 0 or 1 arity"
{:variable variable
:arities arities})))
#_service (case arities
Expand Down
33 changes: 33 additions & 0 deletions test/darkleaf/di/component_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns darkleaf.di.component-test
(:require
[clojure.test :as t]
[darkleaf.di.core :as di])
(:import
(clojure.lang ExceptionInfo)))

(defmacro try-catch [& body]
`(try
~@body
nil
(catch Exception ex#
ex#)))

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

(defn nil-component-1-arity
{::di/kind :component}
[-deps]
nil)

(t/deftest nil-component-0-arity-test
(let [ex (try-catch (di/start `nil-component-0-arity))]
(t/is (= "An error during component build" (-> ex ex-message)))
(t/is (= "A component fn should not return nil" (-> ex ex-cause ex-message)))))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use "must not"?
For example clojure.core contains "must" several times.


(t/deftest nil-component-1-arity-test
(let [ex (try-catch (di/start `nil-component-1-arity))]
(t/is (= "An error during component build" (-> ex ex-message)))
(t/is (= "A component fn should not return nil" (-> ex ex-cause ex-message)))))
4 changes: 2 additions & 2 deletions test/darkleaf/di/tutorial/y_graceful_stop_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
::on-stop-dep-ex on-stop-dep-ex}
ex (try
(di/start `root registry)
(catch Throwable ex
(catch Exception ex
ex))]
(t/is (= on-start-root-ex ex))
(t/is (= on-start-root-ex (ex-cause ex)))
(t/is (= [on-stop-dep-ex] (vec (.getSuppressed ex))))))
Loading