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

Add component-no-value exception #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 22 additions & 6 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@
:declared-deps deps
:remaining-deps (seq deps)}))

(defn- build-obj [built-map head]
(defn- build-obj [built-map head stack]
(let [factory (:factory head)
declared-deps (:declared-deps head)
built-deps (select-keys built-map (keys declared-deps))]
(p/build factory built-deps)))
(p/build factory
(with-meta built-deps {::build-stack stack}))))

(defn- build [{:keys [registry *stop-list]} key]
(loop [stack (list (stack-frame key :required (registry key)))
Expand Down Expand Up @@ -126,7 +127,7 @@
built-map))

:else
(let [obj (build-obj built-map head)
(let [obj (build-obj built-map head stack)
stop #(p/demolish factory obj)]
(vswap! *stop-list conj stop)
(case [obj dep-type]
Expand Down Expand Up @@ -592,12 +593,25 @@
(defn- stop-fn [variable]
(-> variable meta (::stop (fn no-op [_]))))

(defn- no-value-component! [stack]
(let [key (-> stack peek :key)]
(throw (ex-info (str "Component returned no value " key)
{:type ::component-no-value
:stack (map :key stack)}))))

(defn- ensure-component-result! [result stack]
(if (nil? result)
(no-value-component! stack)
result))

(defn- var->0-component [variable]
(let [stop (stop-fn variable)]
(reify p/Factory
(dependencies [_])
(build [_ _]
(variable))
(build [_ empty-deps]
(ensure-component-result!
(variable)
(::build-stack (meta empty-deps))))
(demolish [_ obj]
(stop obj)))))

Expand All @@ -608,7 +622,9 @@
(dependencies [_]
deps)
(build [_ deps]
(variable deps))
(ensure-component-result!
(variable deps)
(::build-stack (meta deps))))
(demolish [_ obj]
(stop obj)))))

Expand Down
15 changes: 15 additions & 0 deletions test/darkleaf/di/dependencies_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@
(try-ex-data (di/start `parent)))))


(defn ^{::di/kind :component} component-no-value
[])


(defn parent-2
[{::syms [component-no-value]}]
:done)


(t/deftest component-no-value-exception-test
(t/is (= {:type ::di/component-no-value
:stack [`component-no-value `parent-2 ::di/implicit-root]}
Copy link
Owner

Choose a reason for hiding this comment

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

You don’t need the stack here. You can identIfy an invalid component just by its key

Copy link
Collaborator Author

@KGOH KGOH Dec 6, 2024

Choose a reason for hiding this comment

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

While just a key is enough to identify the component, it may not be enough in a case when a component returns nil, but I didn't expect this component to be executed at all. In such case it may be obvious why the component returns nil, but it would be not clear at all why this component was used at all in a such configuration

Though, we can omit the stack and wait when we stumble upon a such case where a stack would be helpful to see

Copy link
Owner

Choose a reason for hiding this comment

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

There seem to be two issues. The first is a notification about invalid component.
The second is about adding extra info such a stack of components.

You can solve the second with the following pattern:

(try
  (...)
  (catch RuntimeException ex
    (throw (ex-info "..." {:stack stack} ex))))

Copy link
Collaborator Author

@KGOH KGOH Dec 6, 2024

Choose a reason for hiding this comment

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

Good idea!
@slaughtlaught ^^^

(try-ex-data (di/start `parent-2)))))


(defn recursion-a
[{::syms [recursion-b]}]
:done)
Expand Down
Loading