integrant

ben.mumford 2018-01-11T09:22:18.000024Z

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.

ben.mumford 2018-01-12T08:21:30.000268Z

@kingcode yeah we have something like that atm. i suppose manipulating the config programmatically isn't too bad

2018-01-11T10:43:14.000247Z

Hi! Could you show us the code defining the worker component list? I think that would make it a bit easier to help :)

2018-01-11T10:43:56.000165Z

(unfortunately I haven't used Spring, so not familiar with the functionality you're describing)

ben.mumford 2018-01-11T11:43:47.000185Z

{:worker-1 1 :worker-2 2 :app {:worker-1 #ig/ref :worker-1 :worker-2 #ig/ref :worker-2}}

ben.mumford 2018-01-11T11:44:12.000411Z

(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)))

ben.mumford 2018-01-11T11:44:27.000136Z

in this cut down example there are two workers

ben.mumford 2018-01-11T11:44:33.000227Z

but in our project there are lots

ben.mumford 2018-01-11T11:45:10.000024Z

what i'm trying to avoid is the painful configuration of :app

ben.mumford 2018-01-11T11:47:07.000386Z

{[:worker :worker-1] 1 [:worker :worker-2] 2 :app ?? How to get all :worker here ??}

2018-01-11T17:40:53.000046Z

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.

2018-01-11T17:45:46.000255Z

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.

2018-01-11T17:47:31.000336Z

That way you could make sure that you only have to update the code in one place when adding/changing a worker

2018-01-11T17:47:42.000281Z

Hope some of the above makes sense!

2018-01-11T19:34:14.000288Z

@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 🙂