Convenience tag to add methods for static keys
tlingard opened this issue · comments
tlingard commented
Was wondering if your position on reader tags for constants has changed, or if you would be open to adding a convenience tag that would add a method to the init-key
. It would still be a method of init-key
but without the clutter of having to write out multiples of the same fn with only the dispatch key changing. I started using something like the following, and it seems to work well enough. Though, I'm not sure if there are any downsides or gotchas to this approach that you may be aware of?
(defn const
[key]
(when-not (get (methods ig/init-key) key)
(.addMethod ^clojure.lang.MultiFn ig/init-key (ig/normalize-key key) (fn [_ v] v)))
(ig/normalize-key key))
(defmethod ig/init-key :handler/greet-all [_ {:keys [names]}]
(fn [_]
{:status 200
:body (str "Hello " (clojure.string/join ", " (map :name names)))}))
(def system
(ig/init
(ig/read-string {:readers {'ig/const const}}
"{:handler/greet-all {:names #ig/refset :const/name}
#ig/const [:const/name :const.name/alice] {:name \"Alice\"}
#ig/const [:const/name :const.name/bob] {:name \"Bob\"}}")))
(comment
((system :handler/greet-all) {})
(ig/init-key [:const/name :const.name/alice] "Alice")
(methods ig/init-key))
James Reeves commented
I'm not sure if this is the right way I want to handle constants in future. I still need to think about it, I'm afraid.