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

Component validation #39

Merged
merged 21 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,22 @@
:declared-deps deps
:remaining-deps (seq deps)}))

(defn- build-obj [built-map head]
(let [factory (:factory head)
(defn- build-obj* [built-map stack]
(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)))

(defn- build-obj [built-map stack]
(try
(build-obj* built-map stack)
(catch RuntimeException ex
(throw (ex-info "An error during component build"
{:type ::build-obj-fail
: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 +136,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 +602,19 @@
(defn- stop-fn [variable]
(-> variable meta (::stop (fn no-op [_]))))

(defn- check-nil-value-component! [component var]
(if (nil? component)
(throw (ex-info (str "Component returned nil value" var)
{:type ::nil-value-component}))
component))

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

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

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

(defmacro catch-cause-data [& body]
`(try ~@body
(catch clojure.lang.ExceptionInfo e#
(->> e#
Throwable->map
:via
(mapv :data)))))

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

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

(t/deftest nil-value-component-test
(t/is (= [{:type ::di/build-obj-fail
:stack [`nil-value-component ::di/implicit-root]}
{:type ::di/nil-value-component}]
(catch-cause-data (di/start `nil-value-component))))
(t/is (= [{:type ::di/build-obj-fail
:stack [`nil-value-component-1-arity ::di/implicit-root]}
{:type ::di/nil-value-component}]
(catch-cause-data (di/start `nil-value-component-1-arity)))))
2 changes: 1 addition & 1 deletion test/darkleaf/di/tutorial/x_ns_publics_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[darkleaf.di.core :as di]))

;; excluded
(def nil-component nil)
(def nil-value-component nil)

;; excluded
(def unbound-component)
Expand Down
7 changes: 6 additions & 1 deletion test/darkleaf/di/tutorial/y_graceful_stop_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
;; It throws with original exception.
;; All other possible exceptions are added as suppressed.

(defn ex-map-wo-via [ex]
Copy link
Collaborator

Choose a reason for hiding this comment

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

@darkleaf обрати внимание, мб лучше можно сделать

Copy link
Owner Author

Choose a reason for hiding this comment

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

@KGOH @slaughtlaught you all could simple use ex-cause

(-> ex
Throwable->map
(dissoc :via)))

(defn root
{::di/kind :component}
[{dep `dep
Expand All @@ -34,5 +39,5 @@
(di/start `root registry)
(catch Throwable ex
ex))]
(t/is (= on-start-root-ex ex))
(t/is (= (ex-map-wo-via on-start-root-ex) (ex-map-wo-via ex)))
(t/is (= [on-stop-dep-ex] (vec (.getSuppressed ex))))))
Loading