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 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 29 additions & 4 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 [_]
{:kind :middleware
:middleware ::update-key
:target target}))
own-registry (zipmap (cons f-key arg-keys)
(cons f args))
target-factory (registry target)]
Expand Down Expand Up @@ -624,7 +630,18 @@
(demolish [_ _])))

(defn- var->0-service [variable]
variable)
;; todo: meta ::service

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

(defn- var->service [variable]
(let [deps (dependencies-fn variable)]
Expand Down Expand Up @@ -673,6 +690,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 +860,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
(description [this]
"Returns map with factory description."))
31 changes: 30 additions & 1 deletion test/darkleaf/di/tutorial/x_inspect_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,41 @@
:or {b :default}}]
:ok)


(t/deftest zero-arity-service-test
(t/is (= [{:key ::di/implicit-root
:dependencies {`a :required}}
{:key `a
:description {:kind :service
Copy link
Owner

Choose a reason for hiding this comment

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

мб :type?

:var #'a}}]
Copy link
Owner

Choose a reason for hiding this comment

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

:variable?

(di/inspect `a))))


;; todo: name
(t/deftest ok
(t/is (= [{:key ::di/implicit-root
:dependencies {`c :required}}
{:key `c
:dependencies {`a :required `b :optional}}
{:key `a}
{:key `a
:description {:kind :service
:var #'a}}
{:key `b
:dependencies {`a :required}}]
(di/inspect `c))))


(t/deftest update-key-test
(t/is (= [{:key ::di/implicit-root,
:dependencies {`a :required}}
{:key `a,
:dependencies {`a+di-update-key#0-target :optional,
`a+di-update-key#0-f :optional}
:description {:kind :middleware
:middleware ::di/update-key
:target `a}}
{:key `a+di-update-key#0-target
:description {:kind :service
:var #'a}}
Comment on lines +50 to +52
Copy link
Owner

Choose a reason for hiding this comment

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

Вот тут очень наглядно получилось

{:key `a+di-update-key#0-f}]
Copy link
Owner

Choose a reason for hiding this comment

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

{:kind :trivial
  :object str}

(di/inspect `a (di/update-key `a str)))))
Loading