Skip to content

Commit

Permalink
Inspect (#27)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
darkleaf and KGOH committed Oct 28, 2024
1 parent 2bf991f commit 073471c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions notebooks/index.clj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
#_[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_instrument_test.clj")} "Instrument"]]
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_update_key_test.clj")} "Update key"]]
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_log_test.clj")} "Log"]]
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_inspect_test.clj")} "Inspect"]]
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/y_graceful_stop_test.clj")} "Graceful stop"]]
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/y_multi_arity_service_test.clj")} "Multi arity service"]]
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/z_multi_system_test.clj")} "Multi system"]]
Expand Down Expand Up @@ -118,6 +119,7 @@
;; ### `darkleaf.di.core`
(view-doc #'di/start)
(view-doc #'di/stop)
(view-doc #'di/inspect)
(view-doc #'di/ref)
(view-doc #'di/opt-ref)
(view-doc #'di/template)
Expand Down
39 changes: 39 additions & 0 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,42 @@
(p/demolish factory obj)
(after-demolish! {:key key :object obj})
nil))))))


(defn- inspect-middleware []
(fn [registry]
(fn [key]
(let [factory (registry key)
declared-deps (p/dependencies factory)
info (into {}
(filter (fn [[k v]] (some? v)))
{:key key
:dependencies declared-deps})]
(reify p/Factory
(dependencies [_]
declared-deps)
(build [_ deps]
(into [info]
(comp
(mapcat val)
(distinct))
deps))
(demolish [_ obj]))))))

(defn inspect
"Collects and returns a vector of keys along with their dependencies.
Useful for inspecting enabled components and services.
Evaluates all registries with middlewares applied.
Expects the same arguments as `start` and returns a vector of keys with dependencies e.g.:
```clojure
[{:key `root :dependencies {`foo :required `bar :optional}}
{:key `foo}
{:key `bar}]
```"
[key & middlewares]
(with-open [components (start key
middlewares
(inspect-middleware))]
@components))
21 changes: 21 additions & 0 deletions test/darkleaf/di/tutorial/x_inspect_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(ns darkleaf.di.tutorial.x-inspect-test
(:require
[clojure.test :as t]
[darkleaf.di.core :as di]))

(defn a []
:ok)

(defn b [{a `a}]
:ok)

(defn c [{a `a
b `b
:or {b :default}}]
:ok)

(t/deftest ok
(t/is (= [{:key `c :dependencies {`a :required `b :optional}}
{:key `a}
{:key `b :dependencies {`a :required}}]
(di/inspect `c))))

0 comments on commit 073471c

Please sign in to comment.