integrant 2020-04-16

goodevening

is there a way to tell integrant which of derived keys is preffered within this particula system?

(def system
  {:handler/greet {:name "Alice"}
   ::greeter {:greet (integrant/ref :handler/greet)}})

(derive :handler/greet :handler/greet1)
(derive :handler/greet :handler/greet2)
(defmethod integrant/init-key :handler/greet1 [_ {:keys [] :as opts}]
  (prn :INIT1)
  :init1)
(defmethod integrant/init-key :handler/greet2 [_ {:keys [] :as opts}]
  (prn :INIT2)
  :init2
)


(defmethod integrant/resolve-key :handler/greet [_ _]
  :handler/greet1)

(defmethod integrant/init-key ::greeter [_ {:keys [] :as opts}]
  (prn :OPTS opts))

(comment
  (integrant/init system))

this code fails, because integrant dont know which :handler/greet to use

This would give priority to :handler/greet1 if a key derives from both (prefer-method ig/init-key :handler/greet1 :handler/greet2)

oh yeah

super! thanks a lot!!!

You're very welcome 🙂

but this is affects global scope, right? I cant change on per system basis?

It's global yes, so is using derive

so is defmethod …

I don't think integrant supports accepting a keyword hierarchy

right 🙂

So the only option is globally defining this I think

I can also prefer-new->start-system->revert-to-prefer-prev 🙂

anyway, thanks!

what do you think about ability to attach init-key resolution via metadata attached to the system map?

Where you describe what derives from what, you mean?

To prevent deriving in global scope?

yes, but not sure I was clear enough: i was thinking maybe it make sense to be able to use defmethod and some local method attached to system-map

(with-meta system-map {:handler/gree {:init (fn ...) }})

I think a better solution would be if ig/init accepted a custom hierarchy https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/make-hierarchy

also possible

thanks and have a great avond

Fijne avond 🙂