Skip to content

Commit

Permalink
Add FactoryDescription protocol
Browse files Browse the repository at this point in the history
Co-authored-by: Gleb Eliseev <[email protected]>
Co-authored-by: Mikhail Kuzmin <[email protected]>
  • Loading branch information
3 people committed Nov 9, 2024
1 parent 75142df commit e63ec59
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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)]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/darkleaf/di/protocols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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."))
15 changes: 15 additions & 0 deletions test/darkleaf/di/tutorial/x_inspect_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

0 comments on commit e63ec59

Please sign in to comment.