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

Add FactoryDescription protocol #33

Merged
merged 51 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
e63ec59
Add FactoryDescription protocol
krevedkokun Nov 2, 2024
18ff704
Fix test
krevedkokun Nov 2, 2024
93724f2
refactoring & :kind :service
darkleaf Nov 9, 2024
41f8f26
Merge branch 'master' into inspect-with-meta
darkleaf Jan 9, 2025
cc7c9d5
var -> variable
darkleaf Jan 9, 2025
65fff01
Merge branch 'master' into inspect-with-meta
darkleaf Jan 9, 2025
aabfc83
progress
darkleaf Jan 9, 2025
726cfd1
::di/kind
darkleaf Jan 9, 2025
a079997
fix
darkleaf Jan 9, 2025
c5f5ecc
update-key
darkleaf Jan 9, 2025
f45074d
template
darkleaf Jan 9, 2025
428c41f
extract implicit-root
darkleaf Jan 9, 2025
bb5ab4a
derive
darkleaf Jan 9, 2025
c896d0e
progress
darkleaf Jan 9, 2025
8e14a45
Progress
devleifr Jan 9, 2025
904acb4
Merge branch 'master' into inspect-with-meta
darkleaf Jan 10, 2025
faddfcb
fix test
darkleaf Jan 10, 2025
340b675
ns
darkleaf Jan 10, 2025
bcc5ba4
fix
darkleaf Jan 10, 2025
29c1623
env-parsing
darkleaf Jan 10, 2025
00a3022
log
darkleaf Jan 10, 2025
fbe09cf
more space
darkleaf Jan 10, 2025
9551315
variable
darkleaf Jan 10, 2025
63ec50b
fix
darkleaf Jan 10, 2025
46851e0
save
darkleaf Jan 10, 2025
ba4fdd8
Update core.clj
KGOH Jan 10, 2025
05e1cff
Update x_inspect_test.clj
KGOH Jan 10, 2025
ca8d8ea
Update x_inspect_test.clj
KGOH Jan 10, 2025
bd76f49
fix no-description
darkleaf Jan 15, 2025
98a308f
fix tests
darkleaf Jan 15, 2025
9ef7635
trivial-factory
darkleaf Jan 15, 2025
1f230b5
fix variable factory
darkleaf Jan 15, 2025
c985893
var+description
darkleaf Jan 17, 2025
7c9f7ca
refactoring
darkleaf Jan 17, 2025
762d901
fix
darkleaf Jan 17, 2025
de4f874
variable
darkleaf Jan 17, 2025
d4a56fe
fix grammar
darkleaf Jan 17, 2025
5dbb45c
remove comment
darkleaf Jan 20, 2025
fd29fe5
ref
darkleaf Jan 20, 2025
f0011a5
update-key
darkleaf Jan 20, 2025
e9e79ea
refactoring
darkleaf Jan 20, 2025
d330569
remove trivial-factory
darkleaf Jan 20, 2025
a16f30f
revert changes
darkleaf Jan 20, 2025
74d4e1c
fix
darkleaf Jan 20, 2025
21e2018
comment
darkleaf Jan 20, 2025
5970136
update-key
darkleaf Jan 20, 2025
c84de61
log
darkleaf Jan 20, 2025
46adb61
no-desription test
darkleaf Jan 21, 2025
b195453
update-key
darkleaf Jan 21, 2025
b41c5be
target
darkleaf Jan 21, 2025
0341a62
::variable for consistency
darkleaf Jan 22, 2025
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
177 changes: 147 additions & 30 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,33 @@
(defn- nil-registry [key]
nil)

(defn- trivial-factory [factory]
;; nil has default implementation
(when (some? factory)
(reify
p/Factory
(dependencies [_]
(p/dependencies factory))
(build [_ deps]
(p/build factory deps))
(demolish [_ obj]
(p/demolish factory obj))
p/FactoryDescription
(description [_]
(if-some [desc (not-empty (p/description factory))]
desc
{::kind :trivial
:object factory})))))
darkleaf marked this conversation as resolved.
Show resolved Hide resolved

(defn- trivial-registry [map key]
(trivial-factory (get map key)))
darkleaf marked this conversation as resolved.
Show resolved Hide resolved


(defn- apply-middleware [registry middleware]
(cond
(fn? middleware) (middleware registry)
(map? middleware) (fn [key]
(?? (get middleware key)
(?? (trivial-registry middleware key)
(registry key)))
(seqable? middleware) (reduce apply-middleware
registry middleware)
Expand Down Expand Up @@ -389,15 +411,20 @@
[form]
^{:type ::template
::print form}
(reify p/Factory
(reify
p/Factory
(dependencies [_]
(->> form
(tree-seq coll? seq)
(map ref/deps)
(reduce combine-dependencies)))
(build [_ deps]
(w/postwalk #(ref/build % deps) form))
(demolish [_ _])))
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :template
:template form})))

(defn derive
"Applies `f` to an object built from `key`.
Expand All @@ -410,12 +437,19 @@
[key f & args]
{:pre [(key? key)
(ifn? f)]}
(reify p/Factory
(reify
p/Factory
(dependencies [_]
{key :optional})
(build [_ deps]
(apply f (deps key) args))
(demolish [_ _])))
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :derive
:key key
:f f
:args args})))

;; We currently don't need this middleware.
;; It should be rewritten as `update-key`.
Expand Down Expand Up @@ -508,7 +542,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 @@ -517,9 +552,21 @@
f (deps f-key)
args (map deps arg-keys)]
(apply f t args)))
(demolish [_ _]))
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :middleware
:middleware ::update-key
:target-key target
:new-target-key new-key
:f-key f-key
:f f
:arg-keys arg-keys
:args args}))
darkleaf marked this conversation as resolved.
Show resolved Hide resolved
own-registry (zipmap (cons f-key arg-keys)
(cons f args))
own-registry (update-vals own-registry
trivial-factory)
target-factory (registry target)]
(when (nil? target-factory)
(throw (ex-info (str "Can't update non-existent key " target)
Expand Down Expand Up @@ -552,14 +599,20 @@
[dep-key]
(fn [registry]
(let [new-key (symbol (str "darkleaf.di.core/new-key#" (*next-id*)))
new-factory (reify p/Factory
new-factory (reify
p/Factory
(dependencies [_]
;; array-map preserves order of keys
{new-key :required
dep-key :required})
(build [_ deps]
(new-key deps))
(demolish [_ _]))]
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :middleware
:middleware ::add-side-dependency
:dep-key dep-key}))]
(fn [key]
(cond
(= ::implicit-root key) new-factory
Expand Down Expand Up @@ -591,39 +644,64 @@

(defn- var->0-component [variable]
(let [stop (stop-fn variable)]
(reify p/Factory
(reify
p/Factory
(dependencies [_])
(build [_ _]
(doto (variable)
(validate-obj! variable)))
(demolish [_ obj]
(stop obj)))))
(stop obj))
p/FactoryDescription
(description [_]
{::kind :component
:variable variable}))))


(defn- var->1-component [variable]
(let [deps (dependencies-fn variable)
stop (stop-fn variable)]
(reify p/Factory
(reify
p/Factory
(dependencies [_]
deps)
(build [_ deps]
(doto (variable deps)
(validate-obj! variable)))
(demolish [_ obj]
(stop obj)))))
(stop obj))
p/FactoryDescription
(description [_]
{::kind :component
:variable variable}))))

(defn- service-factory [variable declared-deps]
(reify p/Factory
(reify
p/Factory
(dependencies [_]
declared-deps)
(build [_ deps]
(-> variable
(partial deps)
(with-meta {:type ::service
::print variable})))
(demolish [_ _])))
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :service
:variable variable})))

(defn- var->0-service [variable]
variable)
(reify
p/Factory
(dependencies [_])
(build [_ _]
variable)
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :service
:variable variable})))

(defn- var->service [variable]
(let [deps (dependencies-fn variable)]
Expand Down Expand Up @@ -655,7 +733,19 @@
(service-factory variable deps)))

(defn- var->factory-default [variable]
@variable)
(let [val @variable]
(reify
p/Factory
(dependencies [_]
(p/dependencies val))
(build [_ deps]
(p/build val deps))
(demolish [_ obj]
(p/demolish val obj))
p/FactoryDescription
(description [_]
{::kind :variable
:variable variable}))))
darkleaf marked this conversation as resolved.
Show resolved Hide resolved
darkleaf marked this conversation as resolved.
Show resolved Hide resolved

(defn- var->factory [variable]
(?? (var->factory-meta-deps variable)
Expand All @@ -673,6 +763,16 @@
(build [this _] this)
(demolish [_ _] nil))

(extend-protocol p/FactoryDescription
nil
(description [this]
{::kind :trivial
:object nil})
darkleaf marked this conversation as resolved.
Show resolved Hide resolved

Object
(description [this]
{}))

(c/derive ::root ::instance)
(c/derive ::template ::instance)
(c/derive ::service ::instance)
Expand Down Expand Up @@ -715,13 +815,19 @@
key-name (name key)
parser (cmap key-ns)]
(if (some? parser)
(reify p/Factory
(dependencies [_]
{key-name :optional})
(build [_ deps]
(some-> key-name deps parser))
(demolish [_ _]))
(registry key))))))
(reify
p/Factory
(dependencies [_]
{key-name :optional})
(build [_ deps]
(some-> key-name deps parser))
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :middleware
:middleware ::env-parsing
:cmap cmap}))
(registry key))))))

;; (defn rename-deps [target rmap]
;; (let [inverted-rmap (set/map-invert rmap)]
Expand Down Expand Up @@ -773,12 +879,18 @@
(map symbol))
deps (zipmap component-symbols
(repeat :required))]
(reify p/Factory
(reify
p/Factory
(dependencies [_this]
deps)
(build [_this deps]
(update-keys deps #(-> % name keyword)))
(demolish [_ _])))
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :middleware
:middleware ::ns-publics
:ns component-ns})))
(registry key)))))

(defmacro with-open
Expand Down Expand Up @@ -815,7 +927,8 @@
(fn [registry]
(fn [key]
(let [factory (registry key)]
(reify p/Factory
(reify
p/Factory
(dependencies [_]
(p/dependencies factory))
(build [_ deps]
Expand All @@ -825,8 +938,11 @@
(demolish [_ obj]
(p/demolish factory obj)
(after-demolish! {:key key :object obj})
nil))))))

nil)
p/FactoryDescription
(description [_]
(assoc (p/description factory)
::will-be-logged true)))))))

(defn- inspect-middleware []
(fn [registry]
Expand All @@ -836,7 +952,8 @@
info (into {}
(filter (fn [[k v]] (some? v)))
{:key key
:dependencies declared-deps})]
:dependencies (not-empty declared-deps)
:description (not-empty (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
darkleaf marked this conversation as resolved.
Show resolved Hide resolved
(description [this]
"Returns map with factory description."))
darkleaf marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 7 additions & 1 deletion src/darkleaf/di/ref.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns ^:no-doc darkleaf.di.ref
(:require
[darkleaf.di.core :as-alias di]
[darkleaf.di.protocols :as p])
(:import
(java.io Writer)))
Expand All @@ -15,7 +16,12 @@
{key type})
(build [_ deps]
(deps key))
(demolish [_ _]))
(demolish [_ _])
p/FactoryDescription
(description [_]
{::di/kind :ref
:key key
:type type}))

;; в шаблонах нельзя использовать все фабрики
;; если испльзовать var, то будут не уникальные инстансы
Expand Down
Loading
Loading