From 252337a725c7f1340ec437bef8cfe3d6ab78b396 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 16 Oct 2024 21:17:13 +0400 Subject: [PATCH 1/6] Add log middleware --- src/darkleaf/di/core.clj | 16 ++++++++++ test/darkleaf/di/tutorial/x_log_test.clj | 37 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test/darkleaf/di/tutorial/x_log_test.clj diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 00159dc9..51470973 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -797,3 +797,19 @@ ~@body)) (finally (.close resource#))))))) + +(defn log [built-cb demolished-cb] + (fn [registry] + (fn [key] + (let [factory (registry key)] + (reify p/Factory + (dependencies [_] + (p/dependencies factory)) + (build [_ deps] + (let [obj (p/build factory deps)] + (built-cb key deps obj) + obj)) + (demolish [_ obj] + (p/demolish factory obj) + (demolished-cb key obj) + nil)))))) diff --git a/test/darkleaf/di/tutorial/x_log_test.clj b/test/darkleaf/di/tutorial/x_log_test.clj new file mode 100644 index 00000000..938aa8ce --- /dev/null +++ b/test/darkleaf/di/tutorial/x_log_test.clj @@ -0,0 +1,37 @@ +(ns darkleaf.di.tutorial.x-log-test + (:require + [clojure.test :as t] + [darkleaf.di.core :as di])) + +(defn a + {::di/kind :component} + [] + :a) + +(defn b [{a `a}] + :b) + +(defn c + {::di/kind :component} + [{b `b}] + :c) + +(t/deftest log + (let [logs (atom []) + built! (fn [key deps obj] + (swap! logs conj [:built key deps obj])) + demolished! (fn [key obj] + (swap! logs conj [:demolished key obj])) + [a b c + :as system] (di/start [`a `b `c] + (di/log built! demolished!))] + (di/stop system) + (t/is (= [[:built `a {} a] + [:built `b {`a a} b] + [:built `c {`b b} c] + [:built ::di/implicit-root {`a a `b b `c c} [a b c]] + [:demolished ::di/implicit-root [a b c]] + [:demolished `c c] + [:demolished `b b] + [:demolished `a a]] + @logs)))) From 77aac1cd3bd72712ee55d18c97aa49fd89515ca6 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Tue, 22 Oct 2024 22:23:50 +0400 Subject: [PATCH 2/6] wip --- src/darkleaf/di/core.clj | 2 +- test/darkleaf/di/tutorial/x_log_test.clj | 31 +++++++++++------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 51470973..4c7a003c 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -807,7 +807,7 @@ (p/dependencies factory)) (build [_ deps] (let [obj (p/build factory deps)] - (built-cb key deps obj) + (built-cb key obj) obj)) (demolish [_ obj] (p/demolish factory obj) diff --git a/test/darkleaf/di/tutorial/x_log_test.clj b/test/darkleaf/di/tutorial/x_log_test.clj index 938aa8ce..bda02d2d 100644 --- a/test/darkleaf/di/tutorial/x_log_test.clj +++ b/test/darkleaf/di/tutorial/x_log_test.clj @@ -17,21 +17,18 @@ :c) (t/deftest log - (let [logs (atom []) - built! (fn [key deps obj] - (swap! logs conj [:built key deps obj])) - demolished! (fn [key obj] - (swap! logs conj [:demolished key obj])) - [a b c - :as system] (di/start [`a `b `c] - (di/log built! demolished!))] - (di/stop system) - (t/is (= [[:built `a {} a] - [:built `b {`a a} b] - [:built `c {`b b} c] - [:built ::di/implicit-root {`a a `b b `c c} [a b c]] - [:demolished ::di/implicit-root [a b c]] - [:demolished `c c] - [:demolished `b b] - [:demolished `a a]] + (let [logs (atom []) + built! (fn [key obj] + (swap! logs conj [:built key (pr-str obj)])) + demolished! (fn [key obj] + (swap! logs conj [:demolished key (pr-str obj)]))] + (with-open [root (di/start `c (di/log built! demolished!))]) + (t/is (= [[:built `a ":a"] + [:built `b + "#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"] + [:built `c ":c"] + [:demolished `c ":c"] + [:demolished `b + "#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"] + [:demolished `a ":a"]] @logs)))) From 105c4cad6b9b54ae9baaddd602eae21b2d57d077 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 23 Oct 2024 14:39:47 +0400 Subject: [PATCH 3/6] wip2 --- test/darkleaf/di/add_side_dependency_test.clj | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/test/darkleaf/di/add_side_dependency_test.clj b/test/darkleaf/di/add_side_dependency_test.clj index 408892d0..3b6fcfdf 100644 --- a/test/darkleaf/di/add_side_dependency_test.clj +++ b/test/darkleaf/di/add_side_dependency_test.clj @@ -26,59 +26,54 @@ (defn- a {::di/kind :component} - [{log ::log}] - (swap! log conj :a) + [] :a) (defn- b {::di/kind :component} - [{log ::log}] - (swap! log conj :b) + [] :b) (defn- c {::di/kind :component} - [{log ::log}] - (swap! log conj :c) + [] :c) (defn- d {::di/kind :component} - [{log ::log}] - (swap! log conj :d) + [] :d) (defn- e {::di/kind :component} - [{log ::log}] - (swap! log conj :e) + [] :e) (defn- f {::di/kind :component} - [{log ::log}] - (swap! log conj :f) + [] :f) (defn- g {::di/kind :component} - [{log ::log}] - (swap! log conj :g) + [] :g) (defn- h {::di/kind :component} - [{log ::log}] - (swap! log conj :h) + [] :h) (defn- side-dep {::di/kind :component} - [{log ::log}] - (swap! log conj :side-dep)) + [] + :side-dep) (t/deftest bug-array-map->hash-map - (let [log (atom [])] + (let [log (atom []) + built! (fn [key obj] + (swap! log conj key)) + demolish! (fn [key obj])] (with-open [root (di/start ::root {::log log ::root (di/template @@ -90,8 +85,9 @@ :f (di/ref `f) :g (di/ref `g) :h (di/ref `h)})} - (di/add-side-dependency `side-dep))] - (t/is (= [:a :b :c :d :e :f :g :h :side-dep] @log)) + (di/add-side-dependency `side-dep) + (di/log built! demolish!))] + (t/is (= [`a `b `c `d `e `f `g `h `di/new-key#0 `side-dep ::root] @log)) (t/is (= {:a :a :b :b :c :c @@ -103,13 +99,15 @@ (defn- side-dep2 {::di/kind :component} - [{log ::log}] - (swap! log conj :side-dep2)) + [] + :side-dep2) (t/deftest bug-array-map->hash-map-2 - (let [log (atom [])] + (let [log (atom []) + built! (fn [key obj] + (swap! log conj key)) + demolish! (fn [key obj])] (with-open [root (di/start ::root - {::log log} (di/add-side-dependency `side-dep) {::root (di/template {:a (di/ref `a) @@ -120,8 +118,11 @@ :f (di/ref `f) :g (di/ref `g) :h (di/ref `h)})} - (di/add-side-dependency `side-dep2))] - (t/is (= [:a :side-dep :b :c :d :e :f :g :h :side-dep2] @log)) + (di/add-side-dependency `side-dep2) + (di/log built! demolish!))] + (t/is (= [`di/new-key#0 `side-dep `a + `b `c `d `e `f `g `h + `di/new-key#1 `side-dep2 ::root] @log)) (t/is (= {:a :a :b :b :c :c From bec5793a836f3433da9fc61106870f910171a83d Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 23 Oct 2024 14:42:53 +0400 Subject: [PATCH 4/6] wip3 --- test/darkleaf/di/dependencies_test.clj | 37 +++++++++++++------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/test/darkleaf/di/dependencies_test.clj b/test/darkleaf/di/dependencies_test.clj index 14cded89..3cde4a76 100644 --- a/test/darkleaf/di/dependencies_test.clj +++ b/test/darkleaf/di/dependencies_test.clj @@ -11,33 +11,32 @@ ;; c (defn root - {::di/stop #(swap! % conj [`root :stopped])} - [{a `a, b `b, log ::log}] - (swap! log conj [`root :built]) - log) + {::di/kind :component} + [{a `a, b `b}] + :root) (defn a - {::di/stop #(swap! % conj [`a :stopped])} - [{c `c, log ::log}] - (swap! log conj [`a :built]) - log) + {::di/kind :component} + [{c `c}] + :a) (defn b - {::di/stop #(swap! % conj [`b :stopped])} - [{c `c, log ::log}] - (swap! log conj [`b :built]) - log) + {::di/kind :component} + [{c `c}] + :b) (defn c - {::di/stop #(swap! % conj [`c :stopped])} - [{log ::log}] - (swap! log conj [`c :built]) - log) + {::di/kind :component} + [] + :c) (t/deftest order-test - (let [log (atom [])] - (-> (di/start `root {::log log}) - (di/stop)) + (let [log (atom []) + built! (fn [key obj] + (swap! log conj [key :built])) + demolish! (fn [key obj] + (swap! log conj [key :stopped]))] + (with-open [root (di/start `root (di/log built! demolish!))]) (t/is (= [[`c :built] [`a :built] [`b :built] From a1728dae32944bd184208e6590d2bd94c29cbce6 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 23 Oct 2024 17:00:14 +0400 Subject: [PATCH 5/6] wip4 Co-authored-by: KGOH --- src/darkleaf/di/core.clj | 14 ++++++++++--- test/darkleaf/di/add_side_dependency_test.clj | 21 ++++++++----------- test/darkleaf/di/dependencies_test.clj | 13 ++++++------ test/darkleaf/di/tutorial/x_log_test.clj | 13 ++++++------ 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 4c7a003c..b96998f9 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -798,7 +798,15 @@ (finally (.close resource#))))))) -(defn log [built-cb demolished-cb] +(defn log + "A logging middleware. + Calls `:after-build!` and `:after-demolish!` during `di/start`. + Must be the last one in the middleware chain. + Both callbacks are expected to accept + the following arg `{:keys [key object]}`." + [& {:keys [after-build! after-demolish!] + :or {after-build! (fn no-op [_]) + after-demolish! (fn no-op [_])}}] (fn [registry] (fn [key] (let [factory (registry key)] @@ -807,9 +815,9 @@ (p/dependencies factory)) (build [_ deps] (let [obj (p/build factory deps)] - (built-cb key obj) + (after-build! {:key key :object obj}) obj)) (demolish [_ obj] (p/demolish factory obj) - (demolished-cb key obj) + (after-demolish! {:key key :object obj}) nil)))))) diff --git a/test/darkleaf/di/add_side_dependency_test.clj b/test/darkleaf/di/add_side_dependency_test.clj index 3b6fcfdf..dbfeded6 100644 --- a/test/darkleaf/di/add_side_dependency_test.clj +++ b/test/darkleaf/di/add_side_dependency_test.clj @@ -70,13 +70,11 @@ :side-dep) (t/deftest bug-array-map->hash-map - (let [log (atom []) - built! (fn [key obj] - (swap! log conj key)) - demolish! (fn [key obj])] + (let [log (atom []) + after-build! (fn [{:keys [key]}] + (swap! log conj key))] (with-open [root (di/start ::root - {::log log - ::root (di/template + {::root (di/template {:a (di/ref `a) :b (di/ref `b) :c (di/ref `c) @@ -86,7 +84,7 @@ :g (di/ref `g) :h (di/ref `h)})} (di/add-side-dependency `side-dep) - (di/log built! demolish!))] + (di/log :after-build! after-build!))] (t/is (= [`a `b `c `d `e `f `g `h `di/new-key#0 `side-dep ::root] @log)) (t/is (= {:a :a :b :b @@ -103,10 +101,9 @@ :side-dep2) (t/deftest bug-array-map->hash-map-2 - (let [log (atom []) - built! (fn [key obj] - (swap! log conj key)) - demolish! (fn [key obj])] + (let [log (atom []) + after-build! (fn [{:keys [key]}] + (swap! log conj key))] (with-open [root (di/start ::root (di/add-side-dependency `side-dep) {::root (di/template @@ -119,7 +116,7 @@ :g (di/ref `g) :h (di/ref `h)})} (di/add-side-dependency `side-dep2) - (di/log built! demolish!))] + (di/log :after-build! after-build!))] (t/is (= [`di/new-key#0 `side-dep `a `b `c `d `e `f `g `h `di/new-key#1 `side-dep2 ::root] @log)) diff --git a/test/darkleaf/di/dependencies_test.clj b/test/darkleaf/di/dependencies_test.clj index 3cde4a76..0c87e1bf 100644 --- a/test/darkleaf/di/dependencies_test.clj +++ b/test/darkleaf/di/dependencies_test.clj @@ -31,12 +31,13 @@ :c) (t/deftest order-test - (let [log (atom []) - built! (fn [key obj] - (swap! log conj [key :built])) - demolish! (fn [key obj] - (swap! log conj [key :stopped]))] - (with-open [root (di/start `root (di/log built! demolish!))]) + (let [log (atom []) + after-build! (fn [{:keys [key]}] + (swap! log conj [key :built])) + after-demolish! (fn [{:keys [key]}] + (swap! log conj [key :stopped]))] + (with-open [root (di/start `root (di/log :after-build! after-build! + :after-demolish! after-demolish!))]) (t/is (= [[`c :built] [`a :built] [`b :built] diff --git a/test/darkleaf/di/tutorial/x_log_test.clj b/test/darkleaf/di/tutorial/x_log_test.clj index bda02d2d..ae8fa1ae 100644 --- a/test/darkleaf/di/tutorial/x_log_test.clj +++ b/test/darkleaf/di/tutorial/x_log_test.clj @@ -17,12 +17,13 @@ :c) (t/deftest log - (let [logs (atom []) - built! (fn [key obj] - (swap! logs conj [:built key (pr-str obj)])) - demolished! (fn [key obj] - (swap! logs conj [:demolished key (pr-str obj)]))] - (with-open [root (di/start `c (di/log built! demolished!))]) + (let [logs (atom []) + after-build! (fn [{:keys [key object]}] + (swap! logs conj [:built key (pr-str object)])) + after-demolish! (fn [{:keys [key object]}] + (swap! logs conj [:demolished key (pr-str object)]))] + (with-open [root (di/start `c (di/log :after-build! after-build! + :after-demolish! after-demolish!))]) (t/is (= [[:built `a ":a"] [:built `b "#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"] From ce0aec548b15167254ce4644c8c522fa1d94c9d7 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 23 Oct 2024 17:10:02 +0400 Subject: [PATCH 6/6] update doc index --- notebooks/index.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/notebooks/index.clj b/notebooks/index.clj index d536a849..03993c8e 100644 --- a/notebooks/index.clj +++ b/notebooks/index.clj @@ -90,6 +90,7 @@ [:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_add_side_dependency_test.clj")} "Add a side dependency"]] #_[: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/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"]]