Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (?) implicit root #35

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
(defn- build-obj [built-map head]
(let [factory (:factory head)
declared-deps (:declared-deps head)
built-deps (select-keys built-map (keys declared-deps))]
built-deps (select-keys built-map (map first declared-deps))]
(p/build factory built-deps)))

(defn- build [{:keys [registry *stop-list]} key]
Expand Down Expand Up @@ -207,10 +207,19 @@
(declare ref template)

(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)}]
:else [::implicit-root {::implicit-root (-> key ref)}]))
(let [factory (cond
(vector? key) (->> key (map ref) template)
(map? key) (-> key (update-vals ref) template)
:else (-> key ref))
factory (reify p/Factory
(dependencies [_]
(concat (p/dependencies factory)
[[::side-dependency :optional]]))
(build [_ deps]
(p/build factory (dissoc deps ::side-dependency)))
(demolish [_ obj]
(p/demolish factory obj)))]
[::implicit-root {::implicit-root factory}]))

(defn- ->next-id []
(let [id (atom -1)]
Expand Down Expand Up @@ -271,9 +280,9 @@
(let [[key root-registry] (key->key&registry key)

middlewares (concat [with-env
with-ns
root-registry]
middlewares)
with-ns]
middlewares
[root-registry])
registry (apply-middleware nil-registry middlewares)
ctx {:registry registry
:*stop-list (volatile! '())}
Expand Down Expand Up @@ -560,20 +569,19 @@
```"
[dep-key]
(fn [registry]
(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 [_ _]))]
(let [factory (registry ::side-dependency)
factory (reify p/Factory
(dependencies [_]
(concat (p/dependencies factory)
[[dep-key :required]]))
(build [_ deps]
(p/build factory (dissoc deps dep-key)))
(demolish [_ obj]
(p/demolish factory obj)))]
(fn [key]
(cond
(= ::implicit-root key) new-factory
(= new-key key) (registry ::implicit-root)
:else (registry key))))))
(case key
::side-dependency factory
(registry key))))))

(defn- arglists [variable]
(-> variable meta :arglists))
Expand Down
2 changes: 0 additions & 2 deletions test/darkleaf/di/dependencies_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
[`a :built]
[`b :built]
[`root :built]
[::di/implicit-root :built]

[::di/implicit-root :stopped]
[`root :stopped]
[`b :stopped]
[`a :stopped]
Expand Down
4 changes: 1 addition & 3 deletions test/darkleaf/di/tutorial/x_inspect_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
:ok)

(t/deftest ok
(t/is (= [{:key ::di/implicit-root
:dependencies {`c :required}}
{:key `c
(t/is (= [{:key `c
:dependencies {`a :required `b :optional}}
{:key `a}
{:key `b
Expand Down
2 changes: 0 additions & 2 deletions test/darkleaf/di/tutorial/x_log_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
[: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
Loading