Skip to content

Commit

Permalink
Throw exception on non-existent key for update-key (#23)
Browse files Browse the repository at this point in the history
* Add `update-key-test`

Co-authored-by: Mikhail Kuzmin <[email protected]>
Co-authored-by: Nikita Domnitskii <[email protected]>

* Fix `update-key`

Co-authored-by: Mikhail Kuzmin <[email protected]>
Co-authored-by: Nikita Domnitskii <[email protected]>

* Refactor `update-key`

Co-authored-by: Nikita Domnitskii <[email protected]>
Co-authored-by: Gleb Eliseev <[email protected]>

* use ex-info

* mark the test as a bug test

* fix style

* use thrown-with-msg?

---------

Co-authored-by: Gleb Eeliseev <[email protected]>
Co-authored-by: Nikita Domnitskii <[email protected]>
  • Loading branch information
3 people authored Oct 25, 2024
1 parent 25d0658 commit 21d978e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -507,27 +507,32 @@
[target f & args]
{:pre [(key? target)]}
(fn [registry]
(let [prefix (str (symbol target) "+di-update-key#" (*next-id*))
new-key (symbol (str prefix "-target"))
f-key (symbol (str prefix "-f"))
arg-keys (for [i (-> args count range)]
(symbol (str prefix "-arg#" i)))
new-factory (reify p/Factory
(dependencies [_]
(zipmap (concat [new-key f-key] arg-keys)
(repeat :optional)))
(build [_ deps]
(let [t (deps new-key)
f (deps f-key)
args (map deps arg-keys)]
(apply f t args)))
(demolish [_ _]))
own-registry (zipmap (cons f-key arg-keys)
(cons f args))]
(let [prefix (str (symbol target) "+di-update-key#" (*next-id*))
new-key (symbol (str prefix "-target"))
f-key (symbol (str prefix "-f"))
arg-keys (for [i (-> args count range)]
(symbol (str prefix "-arg#" i)))
new-factory (reify p/Factory
(dependencies [_]
(zipmap (concat [new-key f-key] arg-keys)
(repeat :optional)))
(build [_ deps]
(let [t (deps new-key)
f (deps f-key)
args (map deps arg-keys)]
(apply f t args)))
(demolish [_ _]))
own-registry (zipmap (cons f-key arg-keys)
(cons f args))
target-factory (registry target)]
(when (nil? target-factory)
(throw (ex-info (str "Can't update non-existent key " target)
{:type ::non-existent-key
:key target})))
(fn [key]
(cond
(= new-key key)
(registry target)
target-factory

(= target key)
new-factory
Expand Down
14 changes: 14 additions & 0 deletions test/darkleaf/di/update_key_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns darkleaf.di.update-key-test
(:require
[clojure.test :as t]
[darkleaf.di.core :as di])
(:import
(clojure.lang ExceptionInfo)))

(t/deftest bug--update-non-existent-key-test
(t/is (thrown-with-msg?
ExceptionInfo
#"\ACan't update non-existent key :darkleaf.di.update-key-test/component-with-typo\z"
(di/start ::component
{::component 1}
(di/update-key ::component-with-typo inc)))))

0 comments on commit 21d978e

Please sign in to comment.