Skip to content

Commit 073471c

Browse files
darkleafKGOH
andcommitted
Inspect (#27)
* inspect middleware * add dep types * fix * remove nil * change API * add links * cond-> -> into+filter * Add doc for inspect Co-authored-by: KGOH <[email protected]> --------- Co-authored-by: KGOH <[email protected]>
1 parent 2bf991f commit 073471c

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

notebooks/index.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
#_[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_instrument_test.clj")} "Instrument"]]
9292
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_update_key_test.clj")} "Update key"]]
9393
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_log_test.clj")} "Log"]]
94+
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_inspect_test.clj")} "Inspect"]]
9495
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/y_graceful_stop_test.clj")} "Graceful stop"]]
9596
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/y_multi_arity_service_test.clj")} "Multi arity service"]]
9697
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/z_multi_system_test.clj")} "Multi system"]]
@@ -118,6 +119,7 @@
118119
;; ### `darkleaf.di.core`
119120
(view-doc #'di/start)
120121
(view-doc #'di/stop)
122+
(view-doc #'di/inspect)
121123
(view-doc #'di/ref)
122124
(view-doc #'di/opt-ref)
123125
(view-doc #'di/template)

src/darkleaf/di/core.clj

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,3 +829,42 @@
829829
(p/demolish factory obj)
830830
(after-demolish! {:key key :object obj})
831831
nil))))))
832+
833+
834+
(defn- inspect-middleware []
835+
(fn [registry]
836+
(fn [key]
837+
(let [factory (registry key)
838+
declared-deps (p/dependencies factory)
839+
info (into {}
840+
(filter (fn [[k v]] (some? v)))
841+
{:key key
842+
:dependencies declared-deps})]
843+
(reify p/Factory
844+
(dependencies [_]
845+
declared-deps)
846+
(build [_ deps]
847+
(into [info]
848+
(comp
849+
(mapcat val)
850+
(distinct))
851+
deps))
852+
(demolish [_ obj]))))))
853+
854+
(defn inspect
855+
"Collects and returns a vector of keys along with their dependencies.
856+
Useful for inspecting enabled components and services.
857+
Evaluates all registries with middlewares applied.
858+
859+
Expects the same arguments as `start` and returns a vector of keys with dependencies e.g.:
860+
861+
```clojure
862+
[{:key `root :dependencies {`foo :required `bar :optional}}
863+
{:key `foo}
864+
{:key `bar}]
865+
```"
866+
[key & middlewares]
867+
(with-open [components (start key
868+
middlewares
869+
(inspect-middleware))]
870+
@components))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(ns darkleaf.di.tutorial.x-inspect-test
2+
(:require
3+
[clojure.test :as t]
4+
[darkleaf.di.core :as di]))
5+
6+
(defn a []
7+
:ok)
8+
9+
(defn b [{a `a}]
10+
:ok)
11+
12+
(defn c [{a `a
13+
b `b
14+
:or {b :default}}]
15+
:ok)
16+
17+
(t/deftest ok
18+
(t/is (= [{:key `c :dependencies {`a :required `b :optional}}
19+
{:key `a}
20+
{:key `b :dependencies {`a :required}}]
21+
(di/inspect `c))))

0 commit comments

Comments
 (0)