-
Notifications
You must be signed in to change notification settings - Fork 5
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
krevedkokun
wants to merge
32
commits into
master
Choose a base branch
from
inspect-with-meta
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
e63ec59
Add FactoryDescription protocol
krevedkokun 18ff704
Fix test
krevedkokun 93724f2
refactoring & :kind :service
darkleaf 41f8f26
Merge branch 'master' into inspect-with-meta
darkleaf cc7c9d5
var -> variable
darkleaf 65fff01
Merge branch 'master' into inspect-with-meta
darkleaf aabfc83
progress
darkleaf 726cfd1
::di/kind
darkleaf a079997
fix
darkleaf c5f5ecc
update-key
darkleaf f45074d
template
darkleaf 428c41f
extract implicit-root
darkleaf bb5ab4a
derive
darkleaf c896d0e
progress
darkleaf 8e14a45
Progress
devleifr 904acb4
Merge branch 'master' into inspect-with-meta
darkleaf faddfcb
fix test
darkleaf 340b675
ns
darkleaf bcc5ba4
fix
darkleaf 29c1623
env-parsing
darkleaf 00a3022
log
darkleaf fbe09cf
more space
darkleaf 9551315
variable
darkleaf 63ec50b
fix
darkleaf 46851e0
save
darkleaf ba4fdd8
Update core.clj
KGOH 05e1cff
Update x_inspect_test.clj
KGOH ca8d8ea
Update x_inspect_test.clj
KGOH bd76f49
fix no-description
darkleaf 98a308f
fix tests
darkleaf 9ef7635
trivial-factory
darkleaf 1f230b5
fix variable factory
darkleaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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`. | ||
|
@@ -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`. | ||
|
@@ -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))) | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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)] | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и вот тут тоже нужно если описание нет, то писать описание, а так можно же в reify внутри var положить с реализацией, и он вызываться не будет |
||
|
||
(defn- var->factory [variable] | ||
(?? (var->factory-meta-deps variable) | ||
|
@@ -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) | ||
|
@@ -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)] | ||
|
@@ -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 | ||
|
@@ -815,7 +927,8 @@ | |
(fn [registry] | ||
(fn [key] | ||
(let [factory (registry key)] | ||
(reify p/Factory | ||
(reify | ||
p/Factory | ||
(dependencies [_] | ||
(p/dependencies factory)) | ||
(build [_ deps] | ||
|
@@ -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] | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тут попал на регрессию, в var может быть reify от фабрики, это тоже нужно было учесть