Skip to content

Commit

Permalink
validate-component-obj
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Dec 22, 2024
1 parent 2737064 commit 3daa516
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -601,19 +601,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- validate-component-obj! [obj variable]
(when (nil? obj)
(throw (ex-info "A component fn should not return nil"
{:variable variable}))))

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

Expand All @@ -624,8 +624,9 @@
(dependencies [_]
deps)
(build [_ deps]
(-> (variable deps)
(check-nil-value-component! variable)))
(let [obj (variable deps)]
(validate-component-obj! obj variable)
obj))
(demolish [_ obj]
(stop obj)))))

Expand Down Expand Up @@ -658,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

0 comments on commit 3daa516

Please sign in to comment.