From e63ec5901875d15f11aed5c4fc758f50a5a2e6c8 Mon Sep 17 00:00:00 2001 From: Nikita Domnitskii Date: Sat, 2 Nov 2024 15:06:04 +0600 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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)))))