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

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
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})))))

(defn- trivial-registry [map key]
(trivial-factory (get map key)))


(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}))
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}))))
Comment on lines +736 to +748
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут попал на регрессию, в var может быть reify от фабрики, это тоже нужно было учесть

Comment on lines +736 to +748
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и вот тут тоже нужно если описание нет, то писать описание, а так можно же в reify внутри var положить с реализацией, и он вызываться не будет


(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})

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."))
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