Skip to content

Commit

Permalink
add ::di/service meta key
Browse files Browse the repository at this point in the history
Co-authored-by: Gleb Eliseev <[email protected]>
  • Loading branch information
darkleaf and devleifr committed Jun 15, 2024
1 parent 9eef2c2 commit 476ad5c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,28 @@
obj))

(defn- build-fn [variable deps]
(let [max-arity (->> variable
(let [service? (-> variable meta ::service)
max-arity (->> variable
arglists
(map count)
(reduce max 0) long)]
(case max-arity
0 (-> (variable)
(wrap-stop variable))
1 (-> (variable deps)
(wrap-stop variable))
(reduce max 0)
long)]
(cond
(and service? (= 0 max-arity))
variable

(and service? (= 1 max-arity))
(partial variable deps)

(= 0 max-arity)
(-> (variable)
(wrap-stop variable))

(= 1 max-arity)
(-> (variable deps)
(wrap-stop variable))

:else
(partial variable deps))))

(defn- var->factory-defn [variable]
Expand Down
47 changes: 47 additions & 0 deletions test/darkleaf/di/tutorial/y_multi_arity_service_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,50 @@
(t/is (= [::result {`a :a, `b :b} :a1 :a2] (s)))
(t/is (= [::result {`a :a, `b :b} :arg1 :a2] (s :arg1)))
(t/is (= [::result {`a :a, `b :b} :arg1 :arg2] (s :arg1 :arg2)))))

;; other


(comment
(defn ^::di/service xxx-service-0 []
(gensym))

;; or?
(defn xxx-service-0
{::di/kind :service}
[]
(gensym))

,,,)


(defn ^::di/service xxx-service-0 []
(gensym))

(t/deftest xxx-service-0-test
(with-open [s (di/start `xxx-service-0)]
(t/is (not= (s) (s))))

,,)


(defn ^::di/service xxx-service-1 [-deps]
(gensym))

(t/deftest xxx-service-1-test
(with-open [s (di/start `xxx-service-1)]
(t/is (not= (s) (s))))

,,)


(defn ^::di/service xxx-service-0-1
([]
0)
([-deps]
1))

(t/deftest xxx-service-0-1-test
(with-open [s (di/start `xxx-service-0-1)]
(t/is (= 1 (s))))
,,)

0 comments on commit 476ad5c

Please sign in to comment.