Skip to content

Commit

Permalink
add ns-publics middleware
Browse files Browse the repository at this point in the history
Co-authored-by: KGOH <[email protected]>
Co-authored-by: Gleb Eliseev <[email protected]>
  • Loading branch information
3 people committed Jun 15, 2024
1 parent 151f3ac commit 9eef2c2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
;; **********************************************************************/

(ns darkleaf.di.core
(:refer-clojure :exclude [ref key])
(:refer-clojure :exclude [ref key ns-publics])
(:require
[clojure.core :as c]
[clojure.set :as set]
[clojure.walk :as w]
[darkleaf.di.destructuring-map :as map]
Expand Down Expand Up @@ -665,3 +666,30 @@
;; (let [deps (set/rename-keys deps inverted-rmap)]
;; (p/build factory deps))))
;; factory))))))


(defn- usefull-var? [var]
(and (bound? var)
(some? @var)))

(defn ns-publics []
(fn [registry]
(fn [key]
(if (and (qualified-keyword? key)
(= "ns-publics" (namespace key)))
(let [component-ns (symbol (name key))
component-vars (do
(require component-ns)
(c/ns-publics component-ns))
component-symbols (->> component-vars
vals
(filter usefull-var?)
(map symbol))
deps (zipmap component-symbols
(repeat :required))]
(reify p/Factory
(dependencies [_this]
deps)
(build [_this deps]
(update-keys deps #(-> % name keyword)))))
(registry key)))))
25 changes: 25 additions & 0 deletions test/darkleaf/di/tutorial/x_ns_publics_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns darkleaf.di.tutorial.x-ns-publics-test
(:require
[clojure.test :as t]
[darkleaf.di.core :as di]))

;; excluded
(def nil-component nil)

;; excluded
(def unbound-component)

(defn component []
:component)

(defn service [{component `component} arg]
[component arg])

(t/deftest ok-test
(with-open [system (di/start :ns-publics/darkleaf.di.tutorial.x-ns-publics-test
(di/ns-publics))]
(t/is (map? @system))
(t/is (= #{:component :service :ok-test}
(set (keys @system))))
(t/is (= :component (:component system)))
(t/is (= [:component :my-arg] ((:service system) :my-arg)))))

0 comments on commit 9eef2c2

Please sign in to comment.