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

service name #5

Draft
wants to merge 1 commit 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
47 changes: 44 additions & 3 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,51 @@
(defn- var->0-service [variable]
variable)



(comment

(let [f (with-meta
(fn
([] 0)
([a b c d] 4)
([a b c d & _] :n))
{:foo :bar})]
[(f)
(f 1 2 3 4)
(f 1 2 3 4 5)])



,,,)


(defn- var->service [variable]
(let [deps (dependencies-fn variable)]
(let [deps (dependencies-fn variable)
name (str "#di/service[" variable "]")]
;; => #di/service[#'darkleaf.di.service-name-test/service]
(reify p/Factory
(dependencies [_]
deps)
deps)
(build [_ deps]
(partial variable deps))
(-> variable
(partial deps)

;; with-meta медленнее partial, т.к. фактически делает (apply f args)
;; https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/AFunction.java#L31
;; и все равно в сообщении об ошибке будет имя класса, а не print-method

;; Можно сделать свой (deftype Service [variable deps] IFn ...)
;; defrecord тут нельзя
;; Так в ошибках будет
;; class darkleaf.di.service.Service cannot be cast to class
;; вместо
;; partial: class clojure.core$partial$fn__5908 cannot be cast to class
;; with-meta: class clojure.lang.AFunction$1 cannot be cast to class

(with-meta
{:type ::service
:name name})))
(demolish [_ _]))))

(defn- var->factory-defn [variable]
Expand Down Expand Up @@ -635,6 +673,9 @@
(binding [*out* w]
(pr (-> o meta ::print))))

(defmethod print-method ::service [o ^Writer w]
(.write w (-> o meta :name str)))
Copy link
Owner Author

Choose a reason for hiding this comment

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

Нужно var передавать сюда, и уже тут форматировать.
Тут одинаковая логика и для функций и для мультиметодов


(defn- try-namespace [x]
(when (ident? x)
(namespace x)))
Expand Down
23 changes: 23 additions & 0 deletions test/darkleaf/di/service_name_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns darkleaf.di.service-name-test
(:require
[clojure.test :as t]
[darkleaf.di.core :as di]))

(defn service [arg])

(t/deftest ok
(with-open [s (di/start `service)]
(conj @s 1))
,,)

;; It was:
;; class clojure.core$partial$fn__5908 cannot be cast to class
;; clojure.lang.IPersistentCollection (clojure.core$partial$fn__5908
;; and clojure.lang.IPersistentCollection are in unnamed module of
;; loader 'app'

(t/deftest ok2
(with-open [s (di/start `service)]
@s)
;; => #di/service[#'darkleaf.di.service-name-test/service]
,,)
Loading