From e63ec5901875d15f11aed5c4fc758f50a5a2e6c8 Mon Sep 17 00:00:00 2001 From: Nikita Domnitskii Date: Sat, 2 Nov 2024 15:06:04 +0600 Subject: [PATCH 01/34] Add FactoryDescription protocol Co-authored-by: Gleb Eliseev Co-authored-by: Mikhail Kuzmin --- src/darkleaf/di/core.clj | 20 +++++++++++++++++--- src/darkleaf/di/protocols.clj | 4 ++++ test/darkleaf/di/tutorial/x_inspect_test.clj | 15 +++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 3da8438d..08641eb6 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -517,7 +517,8 @@ f-key (symbol (str prefix "-f")) arg-keys (for [i (-> args count range)] (symbol (str prefix "-arg#" i))) - new-factory (reify p/Factory + new-factory (reify + p/Factory (dependencies [_] (zipmap (concat [new-key f-key] arg-keys) (repeat :optional))) @@ -526,7 +527,12 @@ f (deps f-key) args (map deps arg-keys)] (apply f t args))) - (demolish [_ _])) + (demolish [_ _]) + + p/FactoryDescription + (description [_] + {::middleware ::update-key + ::target target})) own-registry (zipmap (cons f-key arg-keys) (cons f args)) target-factory (registry target)] @@ -673,6 +679,13 @@ (build [this _] this) (demolish [_ _] nil)) +(extend-protocol p/FactoryDescription + nil + (description [_] {}) + + Object + (description [_] {})) + (c/derive ::root ::instance) (c/derive ::template ::instance) (c/derive ::service ::instance) @@ -836,7 +849,8 @@ info (into {} (filter (fn [[k v]] (some? v))) {:key key - :dependencies declared-deps})] + :dependencies declared-deps + :meta (p/description factory)})] (reify p/Factory (dependencies [_] declared-deps) diff --git a/src/darkleaf/di/protocols.clj b/src/darkleaf/di/protocols.clj index 8f59168c..4961ee57 100644 --- a/src/darkleaf/di/protocols.clj +++ b/src/darkleaf/di/protocols.clj @@ -18,3 +18,7 @@ "Builds an object from dependencies.") (demolish [this obj] "Demolishes or stops an object.")) + +(defprotocol FactoryDescription + (description [this] + "Returns map with factory description.")) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 5a0bbfdb..656fbe91 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -23,3 +23,18 @@ {:key `b :dependencies {`a :required}}] (di/inspect `c)))) + +(t/deftest meta-test + (t/is (= [{:key ::di/implicit-root, + :dependencies {`a :required} + :meta {}} + {:key `a, + :dependencies {`a+di-update-key#0-target :optional, + `a+di-update-key#0-f :optional} + :meta {::di/middleware ::di/update-key + ::di/target `a}} + {:key `a+di-update-key#0-target + :meta {}} + {:key `a+di-update-key#0-f + :meta {}}] + (di/inspect `a (di/update-key `a str))))) From 18ff70427f2e78bb3785bed835bebfc6e16903e7 Mon Sep 17 00:00:00 2001 From: Nikita Domnitskii Date: Sat, 2 Nov 2024 15:11:46 +0600 Subject: [PATCH 02/34] Fix test Co-authored-by: Gleb Eliseev Co-authored-by: Mikhail Kuzmin --- test/darkleaf/di/tutorial/x_inspect_test.clj | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 656fbe91..9ce99223 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -16,12 +16,16 @@ (t/deftest ok (t/is (= [{:key ::di/implicit-root - :dependencies {`c :required}} + :dependencies {`c :required} + :meta {}} {:key `c - :dependencies {`a :required `b :optional}} - {:key `a} + :dependencies {`a :required `b :optional} + :meta {}} + {:key `a + :meta {}} {:key `b - :dependencies {`a :required}}] + :dependencies {`a :required} + :meta {}}] (di/inspect `c)))) (t/deftest meta-test From 93724f2b9eb092c2c07203d7723ad6e996d026c9 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Sat, 9 Nov 2024 21:31:14 +0400 Subject: [PATCH 03/34] refactoring & :kind :service --- src/darkleaf/di/core.clj | 23 +++++++--- test/darkleaf/di/tutorial/x_inspect_test.clj | 44 ++++++++++++-------- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 08641eb6..01510a4f 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -528,11 +528,11 @@ args (map deps arg-keys)] (apply f t args))) (demolish [_ _]) - p/FactoryDescription (description [_] - {::middleware ::update-key - ::target target})) + {:kind :middleware + :middleware ::update-key + :target target})) own-registry (zipmap (cons f-key arg-keys) (cons f args)) target-factory (registry target)] @@ -630,7 +630,18 @@ (demolish [_ _]))) (defn- var->0-service [variable] - variable) + ;; todo: meta ::service + + (reify + p/Factory + (dependencies [_]) + (build [_ _] + variable) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {:kind :service + :var variable}))) (defn- var->service [variable] (let [deps (dependencies-fn variable)] @@ -849,8 +860,8 @@ info (into {} (filter (fn [[k v]] (some? v))) {:key key - :dependencies declared-deps - :meta (p/description factory)})] + :dependencies (not-empty declared-deps) + :description (not-empty (p/description factory))})] (reify p/Factory (dependencies [_] declared-deps) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 9ce99223..c3d342b3 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -14,31 +14,41 @@ :or {b :default}}] :ok) + +(t/deftest zero-arity-service-test + (t/is (= [{:key ::di/implicit-root + :dependencies {`a :required}} + {:key `a + :description {:kind :service + :var #'a}}] + (di/inspect `a)))) + + +;; todo: name (t/deftest ok (t/is (= [{:key ::di/implicit-root - :dependencies {`c :required} - :meta {}} + :dependencies {`c :required}} {:key `c - :dependencies {`a :required `b :optional} - :meta {}} - {:key `a - :meta {}} + :dependencies {`a :required `b :optional}} + {:key `a + :description {:kind :service + :var #'a}} {:key `b - :dependencies {`a :required} - :meta {}}] + :dependencies {`a :required}}] (di/inspect `c)))) -(t/deftest meta-test + +(t/deftest update-key-test (t/is (= [{:key ::di/implicit-root, - :dependencies {`a :required} - :meta {}} + :dependencies {`a :required}} {:key `a, :dependencies {`a+di-update-key#0-target :optional, `a+di-update-key#0-f :optional} - :meta {::di/middleware ::di/update-key - ::di/target `a}} - {:key `a+di-update-key#0-target - :meta {}} - {:key `a+di-update-key#0-f - :meta {}}] + :description {:kind :middleware + :middleware ::di/update-key + :target `a}} + {:key `a+di-update-key#0-target + :description {:kind :service + :var #'a}} + {:key `a+di-update-key#0-f}] (di/inspect `a (di/update-key `a str))))) From cc7c9d5a1bce85ed233979713fb0c713b79a3447 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 9 Jan 2025 12:44:59 +0400 Subject: [PATCH 04/34] var -> variable Co-authored-by: Gleb Eliseev --- src/darkleaf/di/core.clj | 4 ++-- test/darkleaf/di/tutorial/x_inspect_test.clj | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 787b27e9..eff97186 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -639,8 +639,8 @@ (demolish [_ _]) p/FactoryDescription (description [_] - {:kind :service - :var variable}))) + {:kind :service + :variable variable}))) (defn- var->service [variable] (let [deps (dependencies-fn variable)] diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index c3d342b3..24f8d6ff 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -31,8 +31,8 @@ {:key `c :dependencies {`a :required `b :optional}} {:key `a - :description {:kind :service - :var #'a}} + :description {:kind :service + :variable #'a}} {:key `b :dependencies {`a :required}}] (di/inspect `c)))) @@ -48,7 +48,7 @@ :middleware ::di/update-key :target `a}} {:key `a+di-update-key#0-target - :description {:kind :service - :var #'a}} + :description {:kind :service + :variable #'a}} {:key `a+di-update-key#0-f}] (di/inspect `a (di/update-key `a str))))) From aabfc833b7ff4b3652e306c54356232a313a21fd Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 9 Jan 2025 13:34:52 +0400 Subject: [PATCH 05/34] progress Co-authored-by: Gleb Eliseev --- src/darkleaf/di/core.clj | 17 +++- src/darkleaf/di/ref.clj | 7 +- test/darkleaf/di/tutorial/x_inspect_test.clj | 93 +++++++++++++++++--- 3 files changed, 99 insertions(+), 18 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index eff97186..66889b81 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -618,7 +618,8 @@ (stop obj))))) (defn- service-factory [variable declared-deps] - (reify p/Factory + (reify + p/Factory (dependencies [_] declared-deps) (build [_ deps] @@ -626,7 +627,11 @@ (partial deps) (with-meta {:type ::service ::print variable}))) - (demolish [_ _]))) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {:kind :service + :variable variable}))) (defn- var->0-service [variable] ;; todo: meta ::service @@ -692,10 +697,14 @@ (extend-protocol p/FactoryDescription nil - (description [_] {}) + (description [this] + {:kind :trivial + :object this}) Object - (description [_] {})) + (description [this] + {:kind :trivial + :object this})) (c/derive ::root ::instance) (c/derive ::template ::instance) diff --git a/src/darkleaf/di/ref.clj b/src/darkleaf/di/ref.clj index 4f90bb2a..b714ebe1 100644 --- a/src/darkleaf/di/ref.clj +++ b/src/darkleaf/di/ref.clj @@ -15,7 +15,12 @@ {key type}) (build [_ deps] (deps key)) - (demolish [_ _])) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {:kind :ref + :key key + :type type})) ;; в шаблонах нельзя использовать все фабрики ;; если испльзовать var, то будут не уникальные инстансы diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 24f8d6ff..6f1b7844 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -3,44 +3,70 @@ [clojure.test :as t] [darkleaf.di.core :as di])) -(defn a [] +;; todo: +;; component 0 arity +;; component 1 arity +;; service 0 arity +;; service N arity +;; service multimethod + +(defn a + {::di/kind :service} + [] :ok) -(defn b [{a `a}] +(defn b + {::di/kind :service} + [{a `a}] :ok) -(defn c [{a `a - b `b - :or {b :default}}] +(defn c + {::di/kind :service} + [{a `a + b `b + :or {b :default}}] :ok) (t/deftest zero-arity-service-test (t/is (= [{:key ::di/implicit-root - :dependencies {`a :required}} + :dependencies {`a :required} + :description {:kind :ref + :key `a + :type :required}} {:key `a - :description {:kind :service - :var #'a}}] + :description {:kind :service + :variable #'a}}] (di/inspect `a)))) ;; todo: name (t/deftest ok (t/is (= [{:key ::di/implicit-root - :dependencies {`c :required}} + :dependencies {`c :required} + :description {:kind :ref + :key `c + :type :required}} {:key `c - :dependencies {`a :required `b :optional}} + :dependencies {`a :required `b :optional} + :description {:kind :service + :variable #'c}} {:key `a :description {:kind :service :variable #'a}} {:key `b - :dependencies {`a :required}}] + :dependencies {`a :required} + :description {:kind :service + :variable #'b}}] (di/inspect `c)))) (t/deftest update-key-test (t/is (= [{:key ::di/implicit-root, - :dependencies {`a :required}} + :dependencies {`a :required} + :description {:kind :ref + :key `a + :type :required}} {:key `a, :dependencies {`a+di-update-key#0-target :optional, `a+di-update-key#0-f :optional} @@ -50,5 +76,46 @@ {:key `a+di-update-key#0-target :description {:kind :service :variable #'a}} - {:key `a+di-update-key#0-f}] + {:key `a+di-update-key#0-f + :description {:kind :trivial + :object str}}] (di/inspect `a (di/update-key `a str))))) + +(t/deftest ref-test + (t/is (= [{:key :darkleaf.di.core/implicit-root, + :dependencies {`foo :required} + :description {:kind :ref ;; ::di/kind ?? + :key `foo + :type :required}} ;; frustrated? type and kind + {:key `foo + :dependencies {`bar :required} + :description {:kind :ref + :key `bar + :type :required}} + {:key `bar + :description {:kind :trivial + :object nil}}] + (di/inspect `foo {`foo (di/ref `bar)})))) + + +(t/deftest trivial-nil-test + (t/is (= [{:key :darkleaf.di.core/implicit-root, + :dependencies {`foo :required} + :description {:kind :ref + :key `foo + :type :required}} + {:key `foo + :description {:kind :trivial + :object nil}}] + (di/inspect `foo {`foo nil})))) + +(t/deftest trivial-obj-test + (t/is (= [{:key :darkleaf.di.core/implicit-root, + :dependencies {`foo :required} + :description {:kind :ref + :key `foo + :type :required}} + {:key `foo + :description {:kind :trivial + :object str}}] + (di/inspect `foo {`foo str})))) From 726cfd1e5631b23cb68cff044d6272fcbfd0b48e Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 9 Jan 2025 13:39:13 +0400 Subject: [PATCH 06/34] ::di/kind --- src/darkleaf/di/core.clj | 10 +-- src/darkleaf/di/ref.clj | 7 +- test/darkleaf/di/tutorial/x_inspect_test.clj | 70 ++++++++++---------- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 66889b81..fa2040c8 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -521,7 +521,7 @@ (demolish [_ _]) p/FactoryDescription (description [_] - {:kind :middleware + {::kind :middleware :middleware ::update-key :target target})) own-registry (zipmap (cons f-key arg-keys) @@ -630,7 +630,7 @@ (demolish [_ _]) p/FactoryDescription (description [_] - {:kind :service + {::kind :service :variable variable}))) (defn- var->0-service [variable] @@ -644,7 +644,7 @@ (demolish [_ _]) p/FactoryDescription (description [_] - {:kind :service + {::kind :service :variable variable}))) (defn- var->service [variable] @@ -698,12 +698,12 @@ (extend-protocol p/FactoryDescription nil (description [this] - {:kind :trivial + {::kind :trivial :object this}) Object (description [this] - {:kind :trivial + {::kind :trivial :object this})) (c/derive ::root ::instance) diff --git a/src/darkleaf/di/ref.clj b/src/darkleaf/di/ref.clj index b714ebe1..c536a97d 100644 --- a/src/darkleaf/di/ref.clj +++ b/src/darkleaf/di/ref.clj @@ -1,5 +1,6 @@ (ns ^:no-doc darkleaf.di.ref (:require + [darkleaf.di.core :as-alias di] [darkleaf.di.protocols :as p]) (:import (java.io Writer))) @@ -18,9 +19,9 @@ (demolish [_ _]) p/FactoryDescription (description [_] - {:kind :ref - :key key - :type type})) + {::di/kind :ref + :key key + :type type})) ;; в шаблонах нельзя использовать все фабрики ;; если испльзовать var, то будут не уникальные инстансы diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 6f1b7844..1065c111 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -31,11 +31,11 @@ (t/deftest zero-arity-service-test (t/is (= [{:key ::di/implicit-root :dependencies {`a :required} - :description {:kind :ref - :key `a - :type :required}} + :description {::di/kind :ref + :key `a + :type :required}} {:key `a - :description {:kind :service + :description {::di/kind :service :variable #'a}}] (di/inspect `a)))) @@ -44,19 +44,19 @@ (t/deftest ok (t/is (= [{:key ::di/implicit-root :dependencies {`c :required} - :description {:kind :ref - :key `c - :type :required}} + :description {::di/kind :ref + :key `c + :type :required}} {:key `c :dependencies {`a :required `b :optional} - :description {:kind :service + :description {::di/kind :service :variable #'c}} {:key `a - :description {:kind :service + :description {::di/kind :service :variable #'a}} {:key `b :dependencies {`a :required} - :description {:kind :service + :description {::di/kind :service :variable #'b}}] (di/inspect `c)))) @@ -64,58 +64,58 @@ (t/deftest update-key-test (t/is (= [{:key ::di/implicit-root, :dependencies {`a :required} - :description {:kind :ref - :key `a - :type :required}} + :description {::di/kind :ref + :key `a + :type :required}} {:key `a, :dependencies {`a+di-update-key#0-target :optional, `a+di-update-key#0-f :optional} - :description {:kind :middleware + :description {::di/kind :middleware :middleware ::di/update-key :target `a}} {:key `a+di-update-key#0-target - :description {:kind :service + :description {::di/kind :service :variable #'a}} {:key `a+di-update-key#0-f - :description {:kind :trivial - :object str}}] + :description {::di/kind :trivial + :object str}}] (di/inspect `a (di/update-key `a str))))) (t/deftest ref-test (t/is (= [{:key :darkleaf.di.core/implicit-root, :dependencies {`foo :required} - :description {:kind :ref ;; ::di/kind ?? - :key `foo - :type :required}} ;; frustrated? type and kind + :description {::di/kind :ref + :key `foo + :type :required}} {:key `foo :dependencies {`bar :required} - :description {:kind :ref - :key `bar - :type :required}} + :description {::di/kind :ref + :key `bar + :type :required}} {:key `bar - :description {:kind :trivial - :object nil}}] + :description {::di/kind :trivial + :object nil}}] (di/inspect `foo {`foo (di/ref `bar)})))) (t/deftest trivial-nil-test (t/is (= [{:key :darkleaf.di.core/implicit-root, :dependencies {`foo :required} - :description {:kind :ref - :key `foo - :type :required}} + :description {::di/kind :ref + :key `foo + :type :required}} {:key `foo - :description {:kind :trivial - :object nil}}] + :description {::di/kind :trivial + :object nil}}] (di/inspect `foo {`foo nil})))) (t/deftest trivial-obj-test (t/is (= [{:key :darkleaf.di.core/implicit-root, :dependencies {`foo :required} - :description {:kind :ref - :key `foo - :type :required}} + :description {::di/kind :ref + :key `foo + :type :required}} {:key `foo - :description {:kind :trivial - :object str}}] + :description {::di/kind :trivial + :object str}}] (di/inspect `foo {`foo str})))) From a0799973c43c3b99b01fc11ec913ed8e28d63972 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 9 Jan 2025 13:40:42 +0400 Subject: [PATCH 07/34] fix Co-authored-by: Gleb Eliseev --- test/darkleaf/di/tutorial/x_inspect_test.clj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 1065c111..9e036882 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -62,7 +62,7 @@ (t/deftest update-key-test - (t/is (= [{:key ::di/implicit-root, + (t/is (= [{:key ::di/implicit-root :dependencies {`a :required} :description {::di/kind :ref :key `a @@ -82,7 +82,7 @@ (di/inspect `a (di/update-key `a str))))) (t/deftest ref-test - (t/is (= [{:key :darkleaf.di.core/implicit-root, + (t/is (= [{:key ::di/implicit-root :dependencies {`foo :required} :description {::di/kind :ref :key `foo @@ -99,7 +99,7 @@ (t/deftest trivial-nil-test - (t/is (= [{:key :darkleaf.di.core/implicit-root, + (t/is (= [{:key ::di/implicit-root :dependencies {`foo :required} :description {::di/kind :ref :key `foo @@ -110,7 +110,7 @@ (di/inspect `foo {`foo nil})))) (t/deftest trivial-obj-test - (t/is (= [{:key :darkleaf.di.core/implicit-root, + (t/is (= [{:key ::di/implicit-root :dependencies {`foo :required} :description {::di/kind :ref :key `foo From c5f5eccdd9c21b02e1927aa42adcb9dcfb3dce26 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 9 Jan 2025 14:12:19 +0400 Subject: [PATCH 08/34] update-key --- src/darkleaf/di/core.clj | 4 +- test/darkleaf/di/tutorial/x_inspect_test.clj | 48 ++++++++++++-------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index fa2040c8..3e9f9955 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -523,7 +523,9 @@ (description [_] {::kind :middleware :middleware ::update-key - :target target})) + :target target + :f f + :args args})) own-registry (zipmap (cons f-key arg-keys) (cons f args)) target-factory (registry target)] diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 9e036882..8f06dd15 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -61,26 +61,6 @@ (di/inspect `c)))) -(t/deftest update-key-test - (t/is (= [{:key ::di/implicit-root - :dependencies {`a :required} - :description {::di/kind :ref - :key `a - :type :required}} - {:key `a, - :dependencies {`a+di-update-key#0-target :optional, - `a+di-update-key#0-f :optional} - :description {::di/kind :middleware - :middleware ::di/update-key - :target `a}} - {:key `a+di-update-key#0-target - :description {::di/kind :service - :variable #'a}} - {:key `a+di-update-key#0-f - :description {::di/kind :trivial - :object str}}] - (di/inspect `a (di/update-key `a str))))) - (t/deftest ref-test (t/is (= [{:key ::di/implicit-root :dependencies {`foo :required} @@ -109,6 +89,7 @@ :object nil}}] (di/inspect `foo {`foo nil})))) + (t/deftest trivial-obj-test (t/is (= [{:key ::di/implicit-root :dependencies {`foo :required} @@ -119,3 +100,30 @@ :description {::di/kind :trivial :object str}}] (di/inspect `foo {`foo str})))) + + +(t/deftest update-key-test + (t/is (= [{:key ::di/implicit-root, + :dependencies {`a :required} + :description {::di/kind :ref + :key `a + :type :required}} + {:key `a, + :dependencies {`a+di-update-key#0-target :optional + `a+di-update-key#0-f :optional + `a+di-update-key#0-arg#0 :optional} + :description {::di/kind :middleware + :middleware ::di/update-key + :target `a + :f str + :args ["arg"]}} + {:key `a+di-update-key#0-target + :description {::di/kind :service + :variable #'a}} + {:key `a+di-update-key#0-f + :description {::di/kind :trivial + :object str}} + {:key `a+di-update-key#0-arg#0, + :description {::di/kind :trivial + :object "arg"}}] + (di/inspect `a (di/update-key `a str "arg"))))) From f45074ded7f95bbf778b3fd8a93e327d034566a5 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 9 Jan 2025 14:16:27 +0400 Subject: [PATCH 09/34] template Co-authored-by: Gleb Eliseev --- src/darkleaf/di/core.clj | 9 +++++++-- test/darkleaf/di/tutorial/x_inspect_test.clj | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 3e9f9955..d3ed66c9 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -389,7 +389,8 @@ [form] ^{:type ::template ::print form} - (reify p/Factory + (reify + p/Factory (dependencies [_] (->> form (tree-seq coll? seq) @@ -397,7 +398,11 @@ (reduce combine-dependencies))) (build [_ deps] (w/postwalk #(ref/build % deps) form)) - (demolish [_ _]))) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::kind :template + :template form}))) (defn derive "Applies `f` to an object built from `key`. diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 8f06dd15..f305fac2 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -77,6 +77,21 @@ :object nil}}] (di/inspect `foo {`foo (di/ref `bar)})))) +(t/deftest template-test + (t/is (= [{:key ::di/implicit-root + :dependencies {`foo :required} + :description {::di/kind :ref + :key `foo + :type :required}} + {:key `foo + :dependencies {`bar :required} + :description {::di/kind :template + :template [42 (di/ref `bar)]}} + {:key `bar + :description {::di/kind :trivial + :object nil}}] + (di/inspect `foo {`foo (di/template [42 (di/ref `bar)])})))) + (t/deftest trivial-nil-test (t/is (= [{:key ::di/implicit-root From 428c41fc5343988fbed993d4ab98b724c8bb7190 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 9 Jan 2025 14:20:54 +0400 Subject: [PATCH 10/34] extract implicit-root --- test/darkleaf/di/tutorial/x_inspect_test.clj | 54 ++++++-------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index f305fac2..e2f236ec 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -3,6 +3,14 @@ [clojure.test :as t] [darkleaf.di.core :as di])) +(defn implicit-root [key] + {:key ::di/implicit-root + :dependencies {key :required} + :description {::di/kind :ref + :key key + :type :required}}) + + ;; todo: ;; component 0 arity ;; component 1 arity @@ -29,11 +37,7 @@ (t/deftest zero-arity-service-test - (t/is (= [{:key ::di/implicit-root - :dependencies {`a :required} - :description {::di/kind :ref - :key `a - :type :required}} + (t/is (= [(implicit-root `a) {:key `a :description {::di/kind :service :variable #'a}}] @@ -42,11 +46,7 @@ ;; todo: name (t/deftest ok - (t/is (= [{:key ::di/implicit-root - :dependencies {`c :required} - :description {::di/kind :ref - :key `c - :type :required}} + (t/is (= [(implicit-root `c) {:key `c :dependencies {`a :required `b :optional} :description {::di/kind :service @@ -62,11 +62,7 @@ (t/deftest ref-test - (t/is (= [{:key ::di/implicit-root - :dependencies {`foo :required} - :description {::di/kind :ref - :key `foo - :type :required}} + (t/is (= [(implicit-root `foo) {:key `foo :dependencies {`bar :required} :description {::di/kind :ref @@ -78,11 +74,7 @@ (di/inspect `foo {`foo (di/ref `bar)})))) (t/deftest template-test - (t/is (= [{:key ::di/implicit-root - :dependencies {`foo :required} - :description {::di/kind :ref - :key `foo - :type :required}} + (t/is (= [(implicit-root `foo) {:key `foo :dependencies {`bar :required} :description {::di/kind :template @@ -94,11 +86,7 @@ (t/deftest trivial-nil-test - (t/is (= [{:key ::di/implicit-root - :dependencies {`foo :required} - :description {::di/kind :ref - :key `foo - :type :required}} + (t/is (= [(implicit-root `foo) {:key `foo :description {::di/kind :trivial :object nil}}] @@ -106,11 +94,7 @@ (t/deftest trivial-obj-test - (t/is (= [{:key ::di/implicit-root - :dependencies {`foo :required} - :description {::di/kind :ref - :key `foo - :type :required}} + (t/is (= [(implicit-root `foo) {:key `foo :description {::di/kind :trivial :object str}}] @@ -118,12 +102,8 @@ (t/deftest update-key-test - (t/is (= [{:key ::di/implicit-root, - :dependencies {`a :required} - :description {::di/kind :ref - :key `a - :type :required}} - {:key `a, + (t/is (= [(implicit-root `a) + {:key `a :dependencies {`a+di-update-key#0-target :optional `a+di-update-key#0-f :optional `a+di-update-key#0-arg#0 :optional} @@ -138,7 +118,7 @@ {:key `a+di-update-key#0-f :description {::di/kind :trivial :object str}} - {:key `a+di-update-key#0-arg#0, + {:key `a+di-update-key#0-arg#0 :description {::di/kind :trivial :object "arg"}}] (di/inspect `a (di/update-key `a str "arg"))))) From bb5ab4ad42444b03b54aac95ed7f024dfb500ed2 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 9 Jan 2025 14:24:25 +0400 Subject: [PATCH 11/34] derive Co-authored-by: Gleb Eliseev --- src/darkleaf/di/core.clj | 11 +++++++++-- test/darkleaf/di/tutorial/x_inspect_test.clj | 12 ++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index d3ed66c9..5112e05c 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -415,12 +415,19 @@ [key f & args] {:pre [(key? key) (ifn? f)]} - (reify p/Factory + (reify + p/Factory (dependencies [_] {key :optional}) (build [_ deps] (apply f (deps key) args)) - (demolish [_ _]))) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::kind :derive + :key key + :f f + :args args}))) ;; We currently don't need this middleware. ;; It should be rewritten as `update-key`. diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index e2f236ec..a43d30bc 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -84,6 +84,18 @@ :object nil}}] (di/inspect `foo {`foo (di/template [42 (di/ref `bar)])})))) +(t/deftest derive-test + (t/is (= [(implicit-root `foo) + {:key `foo + :dependencies {`bar :optional} + :description {::di/kind :derive + :key `bar + :f str + :args ["arg"]}} + {:key `bar + :description {::di/kind :trivial ;; можно попробовать тут что-то другое писать + :object nil}}] + (di/inspect `foo {`foo (di/derive `bar str "arg")})))) (t/deftest trivial-nil-test (t/is (= [(implicit-root `foo) From c896d0e1def79d2858aa65d241320c030af087d0 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 9 Jan 2025 14:39:38 +0400 Subject: [PATCH 12/34] progress Co-authored-by: Gleb Eliseev --- src/darkleaf/di/core.clj | 23 ++++- test/darkleaf/di/tutorial/x_inspect_test.clj | 99 ++++++++++++-------- 2 files changed, 81 insertions(+), 41 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 5112e05c..1c8f67e3 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -611,25 +611,36 @@ (defn- var->0-component [variable] (let [stop (stop-fn variable)] - (reify p/Factory + (reify + p/Factory (dependencies [_]) (build [_ _] (doto (variable) (validate-obj! variable))) (demolish [_ obj] - (stop obj))))) + (stop obj)) + p/FactoryDescription + (description [_] + {::kind :component + :variable variable})))) + (defn- var->1-component [variable] (let [deps (dependencies-fn variable) stop (stop-fn variable)] - (reify p/Factory + (reify + p/Factory (dependencies [_] deps) (build [_ deps] (doto (variable deps) (validate-obj! variable))) (demolish [_ obj] - (stop obj))))) + (stop obj)) + p/FactoryDescription + (description [_] + {::kind :component + :variable variable})))) (defn- service-factory [variable declared-deps] (reify @@ -709,6 +720,10 @@ (build [this _] this) (demolish [_ _] nil)) + +;; может быть тут тогда просто {} возвращать? +;; а для объектов в реестре протокол реализовывать? +;; а то странно, если FactoryDescription не реализован, то он тривиальный (extend-protocol p/FactoryDescription nil (description [this] diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index a43d30bc..30d4989c 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -12,53 +12,71 @@ ;; todo: -;; component 0 arity -;; component 1 arity -;; service 0 arity -;; service N arity ;; service multimethod -(defn a - {::di/kind :service} + +(defn component-0-arity + {::di/kind :component} [] :ok) -(defn b - {::di/kind :service} - [{a `a}] +(t/deftest component-0-arity-test + (t/is (= [(implicit-root `component-0-arity) + {:key `component-0-arity + :description {::di/kind :component + :variable #'component-0-arity}}] + (di/inspect `component-0-arity)))) + + +(defn component-1-arity + {::di/kind :component} + [-deps] :ok) -(defn c +(t/deftest component-1-arity-test + (t/is (= [(implicit-root `component-1-arity) + {:key `component-1-arity + :description {::di/kind :component + :variable #'component-1-arity}}] + (di/inspect `component-1-arity)))) + + +(defn service-0-arity {::di/kind :service} - [{a `a - b `b - :or {b :default}}] + [] :ok) +(t/deftest service-0-arity-test + (t/is (= [(implicit-root `service-0-arity) + {:key `service-0-arity + :description {::di/kind :service + :variable #'service-0-arity}}] + (di/inspect `service-0-arity)))) -(t/deftest zero-arity-service-test - (t/is (= [(implicit-root `a) - {:key `a + +(defn service-n-arity + {::di/kind :service} + [-deps] + :ok) + +(t/deftest service-n-arity-test + (t/is (= [(implicit-root `service-n-arity) + {:key `service-n-arity :description {::di/kind :service - :variable #'a}}] - (di/inspect `a)))) - - -;; todo: name -(t/deftest ok - (t/is (= [(implicit-root `c) - {:key `c - :dependencies {`a :required `b :optional} - :description {::di/kind :service - :variable #'c}} - {:key `a + :variable #'service-n-arity}}] + (di/inspect `service-n-arity)))) + + +(defmulti multimethod-service + {::di/deps []} + (fn [-deps kind] kind)) + +(t/deftest multimethod-service-test + (t/is (= [(implicit-root `multimethod-service) + {:key `multimethod-service :description {::di/kind :service - :variable #'a}} - {:key `b - :dependencies {`a :required} - :description {::di/kind :service - :variable #'b}}] - (di/inspect `c)))) + :variable #'multimethod-service}}] + (di/inspect `multimethod-service)))) (t/deftest ref-test @@ -125,12 +143,19 @@ :f str :args ["arg"]}} {:key `a+di-update-key#0-target - :description {::di/kind :service - :variable #'a}} + :description {::di/kind :trivial + :object :obj}} {:key `a+di-update-key#0-f :description {::di/kind :trivial :object str}} {:key `a+di-update-key#0-arg#0 :description {::di/kind :trivial :object "arg"}}] - (di/inspect `a (di/update-key `a str "arg"))))) + (di/inspect `a + {`a :obj} + (di/update-key `a str "arg"))))) + +#_ +(t/deftest add-side-dependency-test + (t/is (= [] + (di/inspect `a (di/add-side-dependency))))) From 8e14a45a6d5a4590e005b1b8c795c7a50b4e6107 Mon Sep 17 00:00:00 2001 From: Gleb Eliseev Date: Thu, 9 Jan 2025 17:40:13 +0400 Subject: [PATCH 13/34] Progress --- src/darkleaf/di/core.clj | 20 ++++++-- test/darkleaf/di/tutorial/x_inspect_test.clj | 51 ++++++++++++++++++-- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 1c8f67e3..e6325a54 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -572,14 +572,20 @@ [dep-key] (fn [registry] (let [new-key (symbol (str "darkleaf.di.core/new-key#" (*next-id*))) - new-factory (reify p/Factory + new-factory (reify + p/Factory (dependencies [_] ;; array-map preserves order of keys {new-key :required dep-key :required}) (build [_ deps] (new-key deps)) - (demolish [_ _]))] + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::kind :middleware + :middleware ::add-side-dependency + :dep-key dep-key}))] (fn [key] (cond (= ::implicit-root key) new-factory @@ -835,12 +841,18 @@ (map symbol)) deps (zipmap component-symbols (repeat :required))] - (reify p/Factory + (reify + p/Factory (dependencies [_this] deps) (build [_this deps] (update-keys deps #(-> % name keyword))) - (demolish [_ _]))) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::kind :middleware + :middleware ::ns-publics + :component-ns component-ns}))) (registry key))))) (defmacro with-open diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 30d4989c..8acec14b 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -1,7 +1,8 @@ (ns darkleaf.di.tutorial.x-inspect-test (:require [clojure.test :as t] - [darkleaf.di.core :as di])) + [darkleaf.di.core :as di] + [darkleaf.di.tutorial.x-ns-publics-test :as-alias x-ns-publics-test])) (defn implicit-root [key] {:key ::di/implicit-root @@ -155,7 +156,49 @@ {`a :obj} (di/update-key `a str "arg"))))) -#_ + (t/deftest add-side-dependency-test - (t/is (= [] - (di/inspect `a (di/add-side-dependency))))) + (t/is (= [{:key ::di/implicit-root + :dependencies {`di/new-key#0 :required + `side-dep :required} + :description {::di/kind :middleware + :middleware ::di/add-side-dependency + :dep-key `side-dep}} + {:key `di/new-key#0 + :dependencies {`a :required} + :description {::di/kind :ref + :key `a + :type :required}} + {:key `a + :description {::di/kind :trivial + :object :obj}} + {:key `side-dep, + :description {::di/kind :trivial + :object :side-dep}}] + (di/inspect `a + {`a :obj + `side-dep :side-dep} + (di/add-side-dependency `side-dep))))) + + +(t/deftest ns-publics-test + (t/is (= [(implicit-root :ns-publics/darkleaf.di.tutorial.x-ns-publics-test) + {:key :ns-publics/darkleaf.di.tutorial.x-ns-publics-test + :dependencies {`x-ns-publics-test/service :required + `x-ns-publics-test/component :required + `x-ns-publics-test/ok-test :required} + :description {::di/kind :middleware + :middleware ::di/ns-publics + :component-ns 'darkleaf.di.tutorial.x-ns-publics-test}} + {:key `x-ns-publics-test/service + :dependencies {`x-ns-publics-test/component :required} + :description {::di/kind :service + :variable #'x-ns-publics-test/service}} + {:key `darkleaf.di.tutorial.x-ns-publics-test/component + :description {::di/kind :component, + :variable #'x-ns-publics-test/component}} + {:key `darkleaf.di.tutorial.x-ns-publics-test/ok-test + :description {::di/kind :trivial + :object x-ns-publics-test/ok-test}}] + (di/inspect :ns-publics/darkleaf.di.tutorial.x-ns-publics-test + (di/ns-publics))))) From faddfcb344402dc0b2b8435ce9bbbf2f24c19f4b Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 10 Jan 2025 11:21:11 +0400 Subject: [PATCH 14/34] fix test --- test/darkleaf/di/tutorial/x_inspect_test.clj | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 8acec14b..6673683a 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -2,7 +2,7 @@ (:require [clojure.test :as t] [darkleaf.di.core :as di] - [darkleaf.di.tutorial.x-ns-publics-test :as-alias x-ns-publics-test])) + [darkleaf.di.tutorial.x-ns-publics-test :as x-ns-publics-test])) (defn implicit-root [key] {:key ::di/implicit-root @@ -187,17 +187,17 @@ :dependencies {`x-ns-publics-test/service :required `x-ns-publics-test/component :required `x-ns-publics-test/ok-test :required} - :description {::di/kind :middleware - :middleware ::di/ns-publics - :component-ns 'darkleaf.di.tutorial.x-ns-publics-test}} + :description {::di/kind :middleware + :middleware ::di/ns-publics + :component-ns 'darkleaf.di.tutorial.x-ns-publics-test}} {:key `x-ns-publics-test/service :dependencies {`x-ns-publics-test/component :required} - :description {::di/kind :service - :variable #'x-ns-publics-test/service}} - {:key `darkleaf.di.tutorial.x-ns-publics-test/component - :description {::di/kind :component, + :description {::di/kind :service + :variable #'x-ns-publics-test/service}} + {:key `x-ns-publics-test/component + :description {::di/kind :component :variable #'x-ns-publics-test/component}} - {:key `darkleaf.di.tutorial.x-ns-publics-test/ok-test + {:key `x-ns-publics-test/ok-test :description {::di/kind :trivial :object x-ns-publics-test/ok-test}}] (di/inspect :ns-publics/darkleaf.di.tutorial.x-ns-publics-test From 340b67530a6d43b65bcf181e5e7c044b2bbfce2a Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 10 Jan 2025 11:23:51 +0400 Subject: [PATCH 15/34] ns --- src/darkleaf/di/core.clj | 6 +++--- test/darkleaf/di/tutorial/x_inspect_test.clj | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index e6325a54..c7fe84d7 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -850,9 +850,9 @@ (demolish [_ _]) p/FactoryDescription (description [_] - {::kind :middleware - :middleware ::ns-publics - :component-ns component-ns}))) + {::kind :middleware + :middleware ::ns-publics + :ns component-ns}))) (registry key))))) (defmacro with-open diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 6673683a..df8ba667 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -187,9 +187,9 @@ :dependencies {`x-ns-publics-test/service :required `x-ns-publics-test/component :required `x-ns-publics-test/ok-test :required} - :description {::di/kind :middleware - :middleware ::di/ns-publics - :component-ns 'darkleaf.di.tutorial.x-ns-publics-test}} + :description {::di/kind :middleware + :middleware ::di/ns-publics + :ns 'darkleaf.di.tutorial.x-ns-publics-test}} {:key `x-ns-publics-test/service :dependencies {`x-ns-publics-test/component :required} :description {::di/kind :service From bcc5ba44a823567c3b85f228d8460b564f11314f Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 10 Jan 2025 11:26:43 +0400 Subject: [PATCH 16/34] fix --- test/darkleaf/di/tutorial/x_inspect_test.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index df8ba667..08101fa7 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -172,7 +172,7 @@ {:key `a :description {::di/kind :trivial :object :obj}} - {:key `side-dep, + {:key `side-dep :description {::di/kind :trivial :object :side-dep}}] (di/inspect `a From 29c16230fe6620a1bbd12227a8d24eee34af86d3 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 10 Jan 2025 12:07:28 +0400 Subject: [PATCH 17/34] env-parsing Co-authored-by: Bogdan Tiet <94487296+slaughtlaught@users.noreply.github.com> --- src/darkleaf/di/core.clj | 20 +++++++++++++------- test/darkleaf/di/tutorial/x_inspect_test.clj | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index c7fe84d7..daa5b14e 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -783,13 +783,19 @@ key-name (name key) parser (cmap key-ns)] (if (some? parser) - (reify p/Factory - (dependencies [_] - {key-name :optional}) - (build [_ deps] - (some-> key-name deps parser)) - (demolish [_ _])) - (registry key)))))) + (reify + p/Factory + (dependencies [_] + {key-name :optional}) + (build [_ deps] + (some-> key-name deps parser)) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::kind :middleware + :middleware ::env-parsing + :cmap cmap})) + (registry key)))))) ;; (defn rename-deps [target rmap] ;; (let [inverted-rmap (set/map-invert rmap)] diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 08101fa7..36269c8d 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -202,3 +202,18 @@ :object x-ns-publics-test/ok-test}}] (di/inspect :ns-publics/darkleaf.di.tutorial.x-ns-publics-test (di/ns-publics))))) + + +(t/deftest env-parsing-test + (t/is (= [(implicit-root :env.long/PORT) + {:key :env.long/PORT + :dependencies {"PORT" :optional} + :description {::di/kind :middleware + :middleware ::di/env-parsing + :cmap {:env.long parse-long}}} + {:key "PORT" + :description {::di/kind :trivial + :object "8080"}}] + (di/inspect :env.long/PORT + (di/env-parsing :env.long parse-long) + {"PORT" "8080"})))) From 00a302278c6f846e14fc26846cd69a0417b1068a Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 10 Jan 2025 12:26:49 +0400 Subject: [PATCH 18/34] log Co-authored-by: Nikita Domnitskii --- src/darkleaf/di/core.clj | 9 +++++++-- test/darkleaf/di/tutorial/x_inspect_test.clj | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index daa5b14e..30328461 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -895,7 +895,8 @@ (fn [registry] (fn [key] (let [factory (registry key)] - (reify p/Factory + (reify + p/Factory (dependencies [_] (p/dependencies factory)) (build [_ deps] @@ -905,7 +906,11 @@ (demolish [_ obj] (p/demolish factory obj) (after-demolish! {:key key :object obj}) - nil)))))) + nil) + p/FactoryDescription + (description [_] + (assoc (p/description factory) + ::logged true))))))) (defn- inspect-middleware [] diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 36269c8d..e5e8c911 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -217,3 +217,19 @@ (di/inspect :env.long/PORT (di/env-parsing :env.long parse-long) {"PORT" "8080"})))) + + +(t/deftest log-test + (t/is (= [{:key ::di/implicit-root + :dependencies {`foo :required} + :description {::di/kind :ref + :key `foo + :type :required + ::di/logged true}} + {:key `foo + :description {::di/kind :trivial + :object :obj + ::di/logged true}}] + (di/inspect `foo + {`foo :obj} + (di/log))))) From fbe09cfab89bcf1a656857f49b2b0fee5790041e Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 10 Jan 2025 12:30:34 +0400 Subject: [PATCH 19/34] more space --- test/darkleaf/di/tutorial/x_inspect_test.clj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index e5e8c911..2de4746e 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -92,6 +92,7 @@ :object nil}}] (di/inspect `foo {`foo (di/ref `bar)})))) + (t/deftest template-test (t/is (= [(implicit-root `foo) {:key `foo @@ -103,6 +104,7 @@ :object nil}}] (di/inspect `foo {`foo (di/template [42 (di/ref `bar)])})))) + (t/deftest derive-test (t/is (= [(implicit-root `foo) {:key `foo @@ -116,6 +118,7 @@ :object nil}}] (di/inspect `foo {`foo (di/derive `bar str "arg")})))) + (t/deftest trivial-nil-test (t/is (= [(implicit-root `foo) {:key `foo From 95513156dbee94bfed0baa57fcbb66e7c793ef46 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 10 Jan 2025 12:39:35 +0400 Subject: [PATCH 20/34] variable --- src/darkleaf/di/core.clj | 11 ++++++++++- test/darkleaf/di/tutorial/x_inspect_test.clj | 11 ++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 30328461..51be23f9 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -708,7 +708,16 @@ (service-factory variable deps))) (defn- var->factory-default [variable] - @variable) + (reify + p/Factory + (dependencies [_]) + (build [_ _] + @variable) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::kind :variable + :variable variable}))) (defn- var->factory [variable] (?? (var->factory-meta-deps variable) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 2de4746e..bff6dc55 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -12,9 +12,14 @@ :type :required}}) -;; todo: -;; service multimethod - +(def variable :obj) + +(t/deftest variable-test + (t/is (= [(implicit-root `variable) + {:key `variable + :description {::di/kind :variable + :variable #'variable}}] + (di/inspect `variable)))) (defn component-0-arity {::di/kind :component} From 63ec50b7967fe61e1a9186b4937a5a17e9363c7e Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 10 Jan 2025 12:40:42 +0400 Subject: [PATCH 21/34] fix --- src/darkleaf/di/core.clj | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 51be23f9..008b737d 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -665,8 +665,6 @@ :variable variable}))) (defn- var->0-service [variable] - ;; todo: meta ::service - (reify p/Factory (dependencies [_]) From 46851e0c27b3b206636b152db7565ed130fa59fe Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 10 Jan 2025 13:14:22 +0400 Subject: [PATCH 22/34] save Co-authored-by: Bogdan Tiet <94487296+slaughtlaught@users.noreply.github.com> Co-authored-by: Nikita Domnitskii --- src/darkleaf/di/core.clj | 48 +++++++++++++++----- test/darkleaf/di/tutorial/x_inspect_test.clj | 29 +++++++++++- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 008b737d..11ba6dce 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -158,13 +158,38 @@ (throw-many! exs))))) (defn- nil-registry [key] - nil) + (reify + p/Factory + (dependencies [_]) + (build [_ _] nil) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::kind :trivial + :object nil}))) + +(defn- trivial [map key] + (when-some [factory (get map key)] + (reify + p/Factory + (dependencies [_] + (p/dependencies factory)) + (build [_ deps] + (p/build factory deps)) + (demolish [_ obj] + (p/demolish factory obj)) + p/FactoryDescription + (description [_] + (if-some [desc (not-empty (p/description factory))] + desc + {::kind :trivial + :object factory}))))) (defn- apply-middleware [registry middleware] (cond (fn? middleware) (middleware registry) (map? middleware) (fn [key] - (?? (get middleware key) + (?? (trivial middleware key) (registry key))) (seqable? middleware) (reduce apply-middleware registry middleware) @@ -539,7 +564,14 @@ :f f :args args})) own-registry (zipmap (cons f-key arg-keys) - (cons f args)) + (cons f args) + + ;; kind update-f ???? + ;; knid update-arg ???? + #_ + (for [obj (cons f args)] + (reify ... + ...))) target-factory (registry target)] (when (nil? target-factory) (throw (ex-info (str "Can't update non-existent key " target) @@ -733,20 +765,14 @@ (build [this _] this) (demolish [_ _] nil)) - -;; может быть тут тогда просто {} возвращать? -;; а для объектов в реестре протокол реализовывать? -;; а то странно, если FactoryDescription не реализован, то он тривиальный (extend-protocol p/FactoryDescription nil (description [this] - {::kind :trivial - :object this}) + {}) Object (description [this] - {::kind :trivial - :object this})) + {})) (c/derive ::root ::instance) (c/derive ::template ::instance) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index bff6dc55..eaf0e691 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -2,6 +2,7 @@ (:require [clojure.test :as t] [darkleaf.di.core :as di] + [darkleaf.di.protocols :as p] [darkleaf.di.tutorial.x-ns-publics-test :as x-ns-publics-test])) (defn implicit-root [key] @@ -206,8 +207,8 @@ :description {::di/kind :component :variable #'x-ns-publics-test/component}} {:key `x-ns-publics-test/ok-test - :description {::di/kind :trivial - :object x-ns-publics-test/ok-test}}] + :description {::di/kind :variable + :variable #'x-ns-publics-test/ok-test}}] (di/inspect :ns-publics/darkleaf.di.tutorial.x-ns-publics-test (di/ns-publics))))) @@ -241,3 +242,27 @@ (di/inspect `foo {`foo :obj} (di/log))))) + + +(t/deftest unimplemented-test + (t/is (= [(implicit-root `foo) + {:key `foo}] + (di/inspect `foo + {`foo :ok} + (fn null-middleware [registry] + (fn [key] + (let [factory (registry key)] + (if (= `foo key) + (reify + p/Factory + (dependencies [_] + (p/dependencies factory)) + (build [_ deps] + (p/build factory deps)) + (demolish [_ obj] + (p/demolish factory obj)) + #_#_ + p/FactoryDescription + (description [_] + (p/description factory))) + factory)))))))) From ba4fdd865a2219c5762a99e1c951c7efad61497d Mon Sep 17 00:00:00 2001 From: KgOfHedgehogs Date: Fri, 10 Jan 2025 15:43:32 +0200 Subject: [PATCH 23/34] Update core.clj --- src/darkleaf/di/core.clj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 11ba6dce..80a4d82e 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -943,8 +943,7 @@ p/FactoryDescription (description [_] (assoc (p/description factory) - ::logged true))))))) - + ::will-be-logged true))))))) (defn- inspect-middleware [] (fn [registry] From 05e1cffd505cce00d28f772d992eb2dae21fbe1b Mon Sep 17 00:00:00 2001 From: KgOfHedgehogs Date: Fri, 10 Jan 2025 15:44:13 +0200 Subject: [PATCH 24/34] Update x_inspect_test.clj --- test/darkleaf/di/tutorial/x_inspect_test.clj | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index eaf0e691..6fc69e8a 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -231,14 +231,14 @@ (t/deftest log-test (t/is (= [{:key ::di/implicit-root :dependencies {`foo :required} - :description {::di/kind :ref - :key `foo - :type :required - ::di/logged true}} + :description {::di/kind :ref + :key `foo + :type :required + ::di/will-be-logged true}} {:key `foo - :description {::di/kind :trivial - :object :obj - ::di/logged true}}] + :description {::di/kind :trivial + :object :obj + ::di/will-be-logged true}}] (di/inspect `foo {`foo :obj} (di/log))))) From ca8d8ea3a5f31b46b849919fd13e56cdeb2ec03e Mon Sep 17 00:00:00 2001 From: KgOfHedgehogs Date: Fri, 10 Jan 2025 15:55:00 +0200 Subject: [PATCH 25/34] Update x_inspect_test.clj --- test/darkleaf/di/tutorial/x_inspect_test.clj | 28 ++++++-------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 6fc69e8a..bb94e95c 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -244,25 +244,13 @@ (di/log))))) -(t/deftest unimplemented-test +(t/deftest no-description-test (t/is (= [(implicit-root `foo) - {:key `foo}] + {:key `foo + #_"NOTE: no description as it is not implemented"}] (di/inspect `foo - {`foo :ok} - (fn null-middleware [registry] - (fn [key] - (let [factory (registry key)] - (if (= `foo key) - (reify - p/Factory - (dependencies [_] - (p/dependencies factory)) - (build [_ deps] - (p/build factory deps)) - (demolish [_ obj] - (p/demolish factory obj)) - #_#_ - p/FactoryDescription - (description [_] - (p/description factory))) - factory)))))))) + {`foo (reify p/Factory + (dependencies [_]) + (build [_ deps] :ok) + (demolish [_ obj]) + #_"NOTE: no `p/description implemented")})))) From bd76f493faf759ebbf384aaeeed0b4577e28f0c0 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 15 Jan 2025 14:51:59 +0400 Subject: [PATCH 26/34] fix no-description --- test/darkleaf/di/tutorial/x_inspect_test.clj | 22 ++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index bb94e95c..792af747 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -246,11 +246,21 @@ (t/deftest no-description-test (t/is (= [(implicit-root `foo) - {:key `foo + {:key `foo #_"NOTE: no description as it is not implemented"}] (di/inspect `foo - {`foo (reify p/Factory - (dependencies [_]) - (build [_ deps] :ok) - (demolish [_ obj]) - #_"NOTE: no `p/description implemented")})))) + {`foo :ok} + (fn no-description-middleware [registry] + (fn [key] + (let [factory (registry key)] + (if (= `foo key) + (reify + p/Factory + (dependencies [_] + (p/dependencies factory)) + (build [_ deps] + (p/build factory deps)) + (demolish [_ obj] + (p/demolish factory obj)) + #_"NOTE: no `p/description implemented") + factory)))))))) From 98a308f92019b7f23c5e0401c7cc592dc913e512 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 15 Jan 2025 15:18:21 +0400 Subject: [PATCH 27/34] fix tests --- src/darkleaf/di/core.clj | 67 ++++++++++---------- test/darkleaf/di/tutorial/x_inspect_test.clj | 15 +++-- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 80a4d82e..47927f1b 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -540,38 +540,41 @@ [target f & args] {:pre [(key? target)]} (fn [registry] - (let [prefix (str (symbol target) "+di-update-key#" (*next-id*)) - new-key (symbol (str prefix "-target")) - f-key (symbol (str prefix "-f")) - arg-keys (for [i (-> args count range)] - (symbol (str prefix "-arg#" i))) - new-factory (reify - p/Factory - (dependencies [_] - (zipmap (concat [new-key f-key] arg-keys) - (repeat :optional))) - (build [_ deps] - (let [t (deps new-key) - f (deps f-key) - args (map deps arg-keys)] - (apply f t args))) - (demolish [_ _]) - p/FactoryDescription - (description [_] - {::kind :middleware - :middleware ::update-key - :target target - :f f - :args args})) - own-registry (zipmap (cons f-key arg-keys) - (cons f args) - - ;; kind update-f ???? - ;; knid update-arg ???? - #_ - (for [obj (cons f args)] - (reify ... - ...))) + (let [prefix (str (symbol target) "+di-update-key#" (*next-id*)) + new-key (symbol (str prefix "-target")) + f-key (symbol (str prefix "-f")) + arg-keys (for [i (-> args count range)] + (symbol (str prefix "-arg#" i))) + new-factory (reify + p/Factory + (dependencies [_] + (zipmap (concat [new-key f-key] arg-keys) + (repeat :optional))) + (build [_ deps] + (let [t (deps new-key) + f (deps f-key) + args (map deps arg-keys)] + (apply f t args))) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::kind :middleware + :middleware ::update-key + :target-key target + :new-target-key new-key + :f-key f-key + :f f + :arg-keys arg-keys + :args args})) + own-registry (zipmap (cons f-key arg-keys) + (cons f args) + + ;; kind update-f ???? + ;; knid update-arg ???? + #_ + (for [obj (cons f args)] + (reify ... + ...))) target-factory (registry target)] (when (nil? target-factory) (throw (ex-info (str "Can't update non-existent key " target) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 792af747..3cdc9d3f 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -147,18 +147,23 @@ :dependencies {`a+di-update-key#0-target :optional `a+di-update-key#0-f :optional `a+di-update-key#0-arg#0 :optional} - :description {::di/kind :middleware - :middleware ::di/update-key - :target `a - :f str - :args ["arg"]}} + :description {::di/kind :middleware + :middleware ::di/update-key + :target-key `a + :new-target-key `a+di-update-key#0-target + :f-key `a+di-update-key#0-f + :f str + :arg-keys [`a+di-update-key#0-arg#0] + :args ["arg"]}} {:key `a+di-update-key#0-target :description {::di/kind :trivial :object :obj}} {:key `a+di-update-key#0-f + #_#_ :description {::di/kind :trivial :object str}} {:key `a+di-update-key#0-arg#0 + #_#_ :description {::di/kind :trivial :object "arg"}}] (di/inspect `a From 9ef7635579d68fda85b662d2ca0f9b56dd46898f Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 15 Jan 2025 15:37:53 +0400 Subject: [PATCH 28/34] trivial-factory --- src/darkleaf/di/core.clj | 115 +++++++++---------- test/darkleaf/di/tutorial/x_inspect_test.clj | 16 +-- 2 files changed, 63 insertions(+), 68 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 47927f1b..84c3bbfe 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -158,38 +158,35 @@ (throw-many! exs))))) (defn- nil-registry [key] - (reify - p/Factory - (dependencies [_]) - (build [_ _] nil) - (demolish [_ _]) - p/FactoryDescription - (description [_] - {::kind :trivial - :object nil}))) + nil) -(defn- trivial [map key] - (when-some [factory (get map key)] +(defn- trivial-factory [factory] + ;; nil has default implementation + (when (some? factory) (reify - p/Factory - (dependencies [_] - (p/dependencies factory)) - (build [_ deps] - (p/build factory deps)) - (demolish [_ obj] - (p/demolish factory obj)) - p/FactoryDescription - (description [_] - (if-some [desc (not-empty (p/description factory))] - desc - {::kind :trivial - :object factory}))))) + p/Factory + (dependencies [_] + (p/dependencies factory)) + (build [_ deps] + (p/build factory deps)) + (demolish [_ obj] + (p/demolish factory obj)) + p/FactoryDescription + (description [_] + (if-some [desc (not-empty (p/description factory))] + desc + {::kind :trivial + :object factory}))))) + +(defn- trivial-registry [map key] + (trivial-factory (get map key))) + (defn- apply-middleware [registry middleware] (cond (fn? middleware) (middleware registry) (map? middleware) (fn [key] - (?? (trivial middleware key) + (?? (trivial-registry middleware key) (registry key))) (seqable? middleware) (reduce apply-middleware registry middleware) @@ -540,41 +537,36 @@ [target f & args] {:pre [(key? target)]} (fn [registry] - (let [prefix (str (symbol target) "+di-update-key#" (*next-id*)) - new-key (symbol (str prefix "-target")) - f-key (symbol (str prefix "-f")) - arg-keys (for [i (-> args count range)] - (symbol (str prefix "-arg#" i))) - new-factory (reify - p/Factory - (dependencies [_] - (zipmap (concat [new-key f-key] arg-keys) - (repeat :optional))) - (build [_ deps] - (let [t (deps new-key) - f (deps f-key) - args (map deps arg-keys)] - (apply f t args))) - (demolish [_ _]) - p/FactoryDescription - (description [_] - {::kind :middleware - :middleware ::update-key - :target-key target - :new-target-key new-key - :f-key f-key - :f f - :arg-keys arg-keys - :args args})) - own-registry (zipmap (cons f-key arg-keys) - (cons f args) - - ;; kind update-f ???? - ;; knid update-arg ???? - #_ - (for [obj (cons f args)] - (reify ... - ...))) + (let [prefix (str (symbol target) "+di-update-key#" (*next-id*)) + new-key (symbol (str prefix "-target")) + f-key (symbol (str prefix "-f")) + arg-keys (for [i (-> args count range)] + (symbol (str prefix "-arg#" i))) + new-factory (reify + p/Factory + (dependencies [_] + (zipmap (concat [new-key f-key] arg-keys) + (repeat :optional))) + (build [_ deps] + (let [t (deps new-key) + f (deps f-key) + args (map deps arg-keys)] + (apply f t args))) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::kind :middleware + :middleware ::update-key + :target-key target + :new-target-key new-key + :f-key f-key + :f f + :arg-keys arg-keys + :args args})) + own-registry (zipmap (cons f-key arg-keys) + (cons f args)) + own-registry (update-vals own-registry + trivial-factory) target-factory (registry target)] (when (nil? target-factory) (throw (ex-info (str "Can't update non-existent key " target) @@ -771,7 +763,8 @@ (extend-protocol p/FactoryDescription nil (description [this] - {}) + {::kind :trivial + :object nil}) Object (description [this] diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 3cdc9d3f..7398f869 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -146,29 +146,31 @@ {:key `a :dependencies {`a+di-update-key#0-target :optional `a+di-update-key#0-f :optional - `a+di-update-key#0-arg#0 :optional} + `a+di-update-key#0-arg#0 :optional + `a+di-update-key#0-arg#1 :optional} :description {::di/kind :middleware :middleware ::di/update-key :target-key `a :new-target-key `a+di-update-key#0-target :f-key `a+di-update-key#0-f :f str - :arg-keys [`a+di-update-key#0-arg#0] - :args ["arg"]}} + :arg-keys [`a+di-update-key#0-arg#0 `a+di-update-key#0-arg#1] + :args ["arg" nil]}} {:key `a+di-update-key#0-target :description {::di/kind :trivial :object :obj}} {:key `a+di-update-key#0-f - #_#_ :description {::di/kind :trivial :object str}} {:key `a+di-update-key#0-arg#0 - #_#_ :description {::di/kind :trivial - :object "arg"}}] + :object "arg"}} + {:key `a+di-update-key#0-arg#1 + :description {::di/kind :trivial + :object nil}}] (di/inspect `a {`a :obj} - (di/update-key `a str "arg"))))) + (di/update-key `a str "arg" nil))))) (t/deftest add-side-dependency-test From 1f230b590a402ff61015fbfaba14e17688ebbe61 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 15 Jan 2025 17:23:31 +0400 Subject: [PATCH 29/34] fix variable factory --- src/darkleaf/di/core.clj | 23 +++++++++++--------- test/darkleaf/di/tutorial/x_inspect_test.clj | 11 ++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 84c3bbfe..2c7e884b 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -733,16 +733,19 @@ (service-factory variable deps))) (defn- var->factory-default [variable] - (reify - p/Factory - (dependencies [_]) - (build [_ _] - @variable) - (demolish [_ _]) - p/FactoryDescription - (description [_] - {::kind :variable - :variable variable}))) + (let [val @variable] + (reify + p/Factory + (dependencies [_] + (p/dependencies val)) + (build [_ deps] + (p/build val deps)) + (demolish [_ obj] + (p/demolish val obj)) + p/FactoryDescription + (description [_] + {::kind :variable + :variable variable})))) (defn- var->factory [variable] (?? (var->factory-meta-deps variable) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 7398f869..f57c0487 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -271,3 +271,14 @@ (p/demolish factory obj)) #_"NOTE: no `p/description implemented") factory)))))))) + +(def variable-factory-regression + (reify p/Factory + (dependencies [_]) + (build [_ _] + :ok) + (demolish [_ _]))) + +(t/deftest variable-factory-regression-test + (t/is (= :ok + @(di/start `variable-factory-regression)))) From c98589347611a2e15e70f94e203f9cf9d45c36ed Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 17 Jan 2025 16:32:14 +0400 Subject: [PATCH 30/34] var+description --- src/darkleaf/di/core.clj | 5 ++-- test/darkleaf/di/tutorial/x_inspect_test.clj | 29 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 2c7e884b..1b2ec27c 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -744,8 +744,9 @@ (p/demolish val obj)) p/FactoryDescription (description [_] - {::kind :variable - :variable variable})))) + (?? (not-empty (p/description val)) + {::kind :variable + :variable variable}))))) (defn- var->factory [variable] (?? (var->factory-meta-deps variable) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index f57c0487..021d3014 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -22,6 +22,35 @@ :variable #'variable}}] (di/inspect `variable)))) +(def variable+factory + (reify p/Factory + (dependencies [_]) + (build [_ _] :ok) + (demolish [_ _]))) + +(t/deftest variable+factory-test + (t/is (= [(implicit-root `variable+factory) + {:key `variable+factory + :description {::di/kind :variable + :variable #'variable+factory}}] + (di/inspect `variable+factory)))) + +(def variable+description + (reify + p/Factory + (dependencies [_]) + (build [_ _] :ok) + (demolish [_ _]) + p/FactoryDescription + (description [_] + {::di/kind ::variable+description}))) + +(t/deftest variable+description-test + (t/is (= [(implicit-root `variable+description) + {:key `variable+description + :description {::di/kind ::variable+description}}] + (di/inspect `variable+description)))) + (defn component-0-arity {::di/kind :component} [] From 7c9f7ca14a7a5e80db5e36e4d8c483a9df8a6156 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 17 Jan 2025 16:32:46 +0400 Subject: [PATCH 31/34] refactoring --- src/darkleaf/di/core.clj | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 1b2ec27c..81087c2f 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -173,10 +173,9 @@ (p/demolish factory obj)) p/FactoryDescription (description [_] - (if-some [desc (not-empty (p/description factory))] - desc - {::kind :trivial - :object factory}))))) + (?? (not-empty (p/description factory)) + {::kind :trivial + :object factory}))))) (defn- trivial-registry [map key] (trivial-factory (get map key))) From 762d9010d97926247fac881eeb43b18244280c4c Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 17 Jan 2025 16:34:24 +0400 Subject: [PATCH 32/34] fix --- src/darkleaf/di/core.clj | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 81087c2f..2e1d28e0 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -164,18 +164,18 @@ ;; nil has default implementation (when (some? factory) (reify - p/Factory - (dependencies [_] - (p/dependencies factory)) - (build [_ deps] - (p/build factory deps)) - (demolish [_ obj] - (p/demolish factory obj)) - p/FactoryDescription - (description [_] - (?? (not-empty (p/description factory)) - {::kind :trivial - :object factory}))))) + p/Factory + (dependencies [_] + (p/dependencies factory)) + (build [_ deps] + (p/build factory deps)) + (demolish [_ obj] + (p/demolish factory obj)) + p/FactoryDescription + (description [_] + (?? (not-empty (p/description factory)) + {::kind :trivial + :object factory}))))) (defn- trivial-registry [map key] (trivial-factory (get map key))) From de4f87478be442fa4c368f3f30633f5d4283f6e1 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 17 Jan 2025 16:53:32 +0400 Subject: [PATCH 33/34] variable --- src/darkleaf/di/core.clj | 8 ++--- test/darkleaf/di/tutorial/x_inspect_test.clj | 33 +++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 2e1d28e0..0dfac987 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -180,7 +180,6 @@ (defn- trivial-registry [map key] (trivial-factory (get map key))) - (defn- apply-middleware [registry middleware] (cond (fn? middleware) (middleware registry) @@ -743,9 +742,10 @@ (p/demolish val obj)) p/FactoryDescription (description [_] - (?? (not-empty (p/description val)) - {::kind :variable - :variable variable}))))) + (-> (not-empty (p/description val)) + (?? {::kind :trivial + :object val}) + (assoc ::variable variable)))))) (defn- var->factory [variable] (?? (var->factory-meta-deps variable) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index 021d3014..4ce77fa4 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -18,10 +18,12 @@ (t/deftest variable-test (t/is (= [(implicit-root `variable) {:key `variable - :description {::di/kind :variable - :variable #'variable}}] + :description {::di/kind :trivial + :object :obj + ::di/variable #'variable}}] (di/inspect `variable)))) + (def variable+factory (reify p/Factory (dependencies [_]) @@ -31,10 +33,12 @@ (t/deftest variable+factory-test (t/is (= [(implicit-root `variable+factory) {:key `variable+factory - :description {::di/kind :variable - :variable #'variable+factory}}] + :description {::di/kind :trivial + :object variable+factory + ::di/variable #'variable+factory}}] (di/inspect `variable+factory)))) + (def variable+description (reify p/Factory @@ -48,9 +52,23 @@ (t/deftest variable+description-test (t/is (= [(implicit-root `variable+description) {:key `variable+description - :description {::di/kind ::variable+description}}] + :description {::di/kind ::variable+description + ::di/variable #'variable+description}}] (di/inspect `variable+description)))) + +(def variable+template + (di/template [42])) + +(t/deftest variable+template-test + (t/is (= [(implicit-root `variable+template) + {:key `variable+template + :description {::di/kind :template + :template [42] + ::di/variable #'variable+template}}] + (di/inspect `variable+template)))) + + (defn component-0-arity {::di/kind :component} [] @@ -243,8 +261,9 @@ :description {::di/kind :component :variable #'x-ns-publics-test/component}} {:key `x-ns-publics-test/ok-test - :description {::di/kind :variable - :variable #'x-ns-publics-test/ok-test}}] + :description {::di/kind :trivial + :object x-ns-publics-test/ok-test + ::di/variable #'x-ns-publics-test/ok-test}}] (di/inspect :ns-publics/darkleaf.di.tutorial.x-ns-publics-test (di/ns-publics))))) From d4a56feab383bf4dfda75317c89ab31ee32ce845 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Fri, 17 Jan 2025 16:55:44 +0400 Subject: [PATCH 34/34] fix grammar --- src/darkleaf/di/protocols.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/darkleaf/di/protocols.clj b/src/darkleaf/di/protocols.clj index 4961ee57..7b46c5cc 100644 --- a/src/darkleaf/di/protocols.clj +++ b/src/darkleaf/di/protocols.clj @@ -21,4 +21,4 @@ (defprotocol FactoryDescription (description [this] - "Returns map with factory description.")) + "Returns a map with the factory description."))