Skip to content

Commit

Permalink
Merge pull request #32 from darkleaf/fix-add-side-dependency
Browse files Browse the repository at this point in the history
fix add-side-dependency
  • Loading branch information
devleifr authored Oct 31, 2024
2 parents cae5f88 + a8e2580 commit 467ce95
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
34 changes: 14 additions & 20 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@
(defn- key->key&registry [key]
(cond
(vector? key) [::implicit-root {::implicit-root (->> key (map ref) template)}]
(map? key) [::implicit-root {::implicit-root (-> key (update-vals ref) template)}]
true [key nil]))
(map? key) [::implicit-root {::implicit-root (-> key (update-vals ref) template)}]
:else [::implicit-root {::implicit-root (-> key ref)}]))

(defn- ->next-id []
(let [id (atom -1)]
Expand Down Expand Up @@ -560,26 +560,20 @@
```"
[dep-key]
(fn [registry]
(let [*orig-key (volatile! nil)
*orig-factory (volatile! nil)
new-key (symbol (str "darkleaf.di.core/new-key#" (*next-id*)))
new-factory (reify p/Factory
(dependencies [_]
;; array-map preserves order of keys
{new-key :required
dep-key :required})
(build [_ deps]
(new-key deps))
(demolish [_ _]))]
(let [new-key (symbol (str "darkleaf.di.core/new-key#" (*next-id*)))
new-factory (reify p/Factory
(dependencies [_]
;; array-map preserves order of keys
{new-key :required
dep-key :required})
(build [_ deps]
(new-key deps))
(demolish [_ _]))]
(fn [key]
(when (nil? @*orig-key)
(vreset! *orig-key key))
(when (nil? @*orig-factory)
(vreset! *orig-factory (registry key)))
(cond
(= @*orig-key key) new-factory
(= new-key key) @*orig-factory
:else (registry key))))))
(= ::implicit-root key) new-factory
(= new-key key) (registry ::implicit-root)
:else (registry key))))))

(defn- arglists [variable]
(-> variable meta :arglists))
Expand Down
24 changes: 20 additions & 4 deletions test/darkleaf/di/add_side_dependency_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
:h (di/ref `h)})}
(di/add-side-dependency `side-dep)
(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 `b `c `d `e `f `g `h
::root `di/new-key#0 `side-dep ::di/implicit-root] @log))
(t/is (= {:a :a
:b :b
:c :c
Expand Down Expand Up @@ -117,9 +118,12 @@
:h (di/ref `h)})}
(di/add-side-dependency `side-dep2)
(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))
(t/is (= [`a `b `c `d `e `f `g `h
::root
`di/new-key#0 `side-dep
`di/new-key#1 `side-dep2
::di/implicit-root]
@log))
(t/is (= {:a :a
:b :b
:c :c
Expand All @@ -128,3 +132,15 @@
:f :f
:g :g
:h :h} @root)))))

(t/deftest bug-with-update-key
(let [info (di/inspect ::root
{::root 42
::unused 0
::side-dep :side-dep}
(di/add-side-dependency ::side-dep)
(di/update-key ::unused inc))
keys (into #{}
(map :key)
info)]
(t/is (contains? keys ::side-dep))))
12 changes: 7 additions & 5 deletions test/darkleaf/di/dependencies_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
[`a :built]
[`b :built]
[`root :built]
[::di/implicit-root :built]

[::di/implicit-root :stopped]
[`root :stopped]
[`b :stopped]
[`a :stopped]
Expand Down Expand Up @@ -76,7 +78,7 @@

(t/deftest error-path-test
(t/is (= {:type ::di/missing-dependency
:stack [`missing-path `b-path `root-path]}
:stack [`missing-path `b-path `root-path ::di/implicit-root]}
(try-ex-data (di/start `root-path)))))


Expand All @@ -87,11 +89,11 @@

(t/deftest missing-dependency-test
(t/is (= {:type ::di/missing-dependency
:stack [`missing-root]}
:stack [`missing-root ::di/implicit-root]}
(try-ex-data (di/start `missing-root))))

(t/is (= {:type ::di/missing-dependency
:stack [`missing-key `parent]}
:stack [`missing-key `parent ::di/implicit-root]}
(try-ex-data (di/start `parent)))))


Expand All @@ -109,10 +111,10 @@

(t/deftest circular-dependency-test
(t/is (= {:type ::di/circular-dependency
:stack [`recursion-a `recursion-b `recursion-a]}
:stack [`recursion-a `recursion-b `recursion-a ::di/implicit-root]}
(try-ex-data (di/start `recursion-a))))


(t/is (= {:type ::di/circular-dependency
:stack [`recursion-c `recursion-c]}
:stack [`recursion-c `recursion-c ::di/implicit-root]}
(try-ex-data (di/start `recursion-c)))))
8 changes: 6 additions & 2 deletions test/darkleaf/di/tutorial/x_inspect_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
:ok)

(t/deftest ok
(t/is (= [{:key `c :dependencies {`a :required `b :optional}}
(t/is (= [{:key ::di/implicit-root
:dependencies {`c :required}}
{:key `c
:dependencies {`a :required `b :optional}}
{:key `a}
{:key `b :dependencies {`a :required}}]
{:key `b
:dependencies {`a :required}}]
(di/inspect `c))))
2 changes: 2 additions & 0 deletions test/darkleaf/di/tutorial/x_log_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
[:built `b
"#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"]
[:built `c ":c"]
[:built ::di/implicit-root ":c"]
[:demolished ::di/implicit-root ":c"]
[:demolished `c ":c"]
[:demolished `b
"#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"]
Expand Down

0 comments on commit 467ce95

Please sign in to comment.