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 25 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
181 changes: 150 additions & 31 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,38 @@
(throw-many! exs)))))

(defn- nil-registry [key]
nil)
(reify
p/Factory
(dependencies [_])
(build [_ _] nil)
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :trivial
:object nil})))

(defn- trivial [map key]
darkleaf marked this conversation as resolved.
Show resolved Hide resolved
(when-some [factory (get map key)]
(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- apply-middleware [registry middleware]
(cond
(fn? middleware) (middleware registry)
(map? middleware) (fn [key]
(?? (get middleware key)
(?? (trivial middleware key)
(registry key)))
(seqable? middleware) (reduce apply-middleware
registry middleware)
Expand Down Expand Up @@ -389,15 +414,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 +440,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 +545,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 +555,23 @@
f (deps f-key)
args (map deps arg-keys)]
(apply f t args)))
(demolish [_ _]))
(demolish [_ _])
p/FactoryDescription
(description [_]
{::kind :middleware
:middleware ::update-key
:target target
:f f
:args args}))
own-registry (zipmap (cons f-key arg-keys)
(cons f args))
(cons f args)

;; kind update-f ????
;; knid update-arg ????
#_
(for [obj (cons f args)]
(reify ...
...)))
darkleaf marked this conversation as resolved.
Show resolved Hide resolved
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 +604,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 +649,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 +738,16 @@
(service-factory variable deps)))

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

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

(extend-protocol p/FactoryDescription
nil
(description [this]
{})

Object
(description [this]
{}))

(c/derive ::root ::instance)
(c/derive ::template ::instance)
(c/derive ::service ::instance)
Expand Down Expand Up @@ -715,13 +816,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 +880,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 +928,8 @@
(fn [registry]
(fn [key]
(let [factory (registry key)]
(reify p/Factory
(reify
p/Factory
(dependencies [_]
(p/dependencies factory))
(build [_ deps]
Expand All @@ -825,7 +939,11 @@
(demolish [_ obj]
(p/demolish factory obj)
(after-demolish! {:key key :object obj})
nil))))))
nil)
p/FactoryDescription
(description [_]
(assoc (p/description factory)
::logged true)))))))


(defn- inspect-middleware []
Expand All @@ -836,7 +954,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