hi all, i'm new to integrant so bear with me. i have several components that are all of similar type (let's call them "workers") and i want a manager component that will use all these workers. in java spring I can use setter/constructor injection with type List<ParentType> to get a list of all of the subtypes. I see that i can derive components from other components but can't see beyond that. ultimately i'm trying to avoid having a huge list of workers defined and then the same huge list of workers defined as dependencies for my manager component (something that has proved error prone in the past). any help much appreciated.
@kingcode yeah we have something like that atm. i suppose manipulating the config programmatically isn't too bad
Hi! Could you show us the code defining the worker component list? I think that would make it a bit easier to help :)
(unfortunately I haven't used Spring, so not familiar with the functionality you're describing)
{:worker-1 1 :worker-2 2 :app {:worker-1 #ig/ref :worker-1 :worker-2 #ig/ref :worker-2}}
(defmethod ig/init-key :worker-1 [_ val] val) (defmethod ig/init-key :worker-2 [_ val] val) (defmethod ig/init-key :app [_ sub-system] (vals sub-system)) (defn -main [& args] (let [config (ig/read-string (slurp (io/resource "config.edn"))) system (ig/init config)] (println system)))
in this cut down example there are two workers
but in our project there are lots
what i'm trying to avoid is the painful configuration of :app
{[:worker :worker-1] 1 [:worker :worker-2] 2 :app ?? How to get all :worker here ??}
Hm, can't think of an immediate/built-in answer. Is it possible to have e.g a :workers key with a list of worker configs as the value? Then :app could depend on :workers instead of each individual worker key.
Otherwise I suppose you could keep your config in a .clj file, keep the configs for the workers in a separate var, and create the integrant config programmatically.
That way you could make sure that you only have to update the code in one place when adding/changing a worker
Hope some of the above makes sense!
@ben.mumford If the number of workers is too high, e.g. 100s or 1000s, or if their numbers increase often (with an edit to config.edn each time), you might want to consider using a separate/batch process to populate your config, or perhaps macros. The idea is that those components’ info has to come from somewhere and can be bootstrapped into your config to alleviate the pain - however that maybe too hacky 🙂