Skip to content

Commit 9b070af

Browse files
committed
Allow to return nil from a defn of a component
1 parent 28a999c commit 9b070af

2 files changed

Lines changed: 6 additions & 14 deletions

File tree

src/darkleaf/di/core.clj

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -651,19 +651,12 @@
651651
(defn- stop-fn [variable]
652652
(-> variable meta (::stop (fn no-op [_]))))
653653

654-
(defn- validate-obj! [obj variable]
655-
(when (nil? obj)
656-
(throw (ex-info "A component fn must not return nil"
657-
{:type ::nil-return
658-
:variable variable}))))
659-
660654
(defn- var->0-component [variable]
661655
(let [stop (stop-fn variable)]
662656
(reify p/Factory
663657
(dependencies [_])
664658
(build [_ _ add-stop]
665-
(let [obj (variable)]
666-
(validate-obj! obj variable)
659+
(let [obj (?? (variable) ::nil)]
667660
(add-stop #(stop obj))
668661
obj))
669662
(description [_]
@@ -676,8 +669,7 @@
676669
(dependencies [_]
677670
deps)
678671
(build [_ deps add-stop]
679-
(let [obj (variable deps)]
680-
(validate-obj! obj variable)
672+
(let [obj (?? (variable deps) ::nil)]
681673
(add-stop #(stop obj))
682674
obj))
683675
(description [_]

test/darkleaf/di/component_test.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
nil)
1616

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

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

2525

2626
(defn component-2-arity

0 commit comments

Comments
 (0)