Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 2 additions & 10 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -651,19 +651,12 @@
(defn- stop-fn [variable]
(-> variable meta (::stop (fn no-op [_]))))

(defn- validate-obj! [obj variable]
(when (nil? obj)
(throw (ex-info "A component fn must not return nil"
{:type ::nil-return
:variable variable}))))

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

Choose a reason for hiding this comment

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

а в чём вообще была проблема из-за которой появился запрет на возврат буквального nil из компонента?

Copy link
Owner Author

@darkleaf darkleaf Nov 7, 2025

Choose a reason for hiding this comment

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

Собранный компонент - это значение.
Когда функа использует компонент, он может быть required или optional, это объявляется через :or в дестракчеринге.
Если компонент nil - то в процессе запуска об этом будет ошибка.

Тот факт, что в миграциях у нас может быть nil из-за when - это прикол нашего проекта.
Попробую через middleware в проекте поправить, посмотрим как приживется

тут наверное проблема в том, что в clojure смешивается nil и undefined

(let [obj (?? (variable) ::nil)]
(add-stop #(stop obj))
obj))
(description [_]
Expand All @@ -676,8 +669,7 @@
(dependencies [_]
deps)
(build [_ deps add-stop]
(let [obj (variable deps)]
(validate-obj! obj variable)
(let [obj (?? (variable deps) ::nil)]
(add-stop #(stop obj))
obj))
(description [_]
Expand Down
8 changes: 4 additions & 4 deletions test/darkleaf/di/component_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
nil)

(t/deftest nil-component-0-arity-test
(let [ex (catch-some (di/start `nil-component-0-arity))]
(t/is (= ::di/nil-return (-> ex ex-cause ex-data :type)))))
(let [root (di/start `nil-component-0-arity)]
(t/is (= ::di/nil @root))))

(t/deftest nil-component-1-arity-test
(let [ex (catch-some (di/start `nil-component-1-arity))]
(t/is (= ::di/nil-return (-> ex ex-cause ex-data :type)))))
(let [root (di/start `nil-component-1-arity)]
(t/is (= ::di/nil @root))))


(defn component-2-arity
Expand Down