integrant

kirill.salykin 2020-04-16T17:35:54.008200Z

goodevening

kirill.salykin 2020-04-16T17:36:35.009100Z

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

kirill.salykin 2020-04-16T17:37:43.009900Z

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

kwrooijen 2020-04-16T17:39:56.010300Z

I think you're looking for this https://clojuredocs.org/clojure.core/prefer-method

kwrooijen 2020-04-16T17:42:29.011500Z

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

kirill.salykin 2020-04-16T17:44:36.011800Z

oh yeah

kirill.salykin 2020-04-16T17:44:41.012Z

super! thanks a lot!!!

kwrooijen 2020-04-16T17:44:53.012200Z

You're very welcome 🙂

kirill.salykin 2020-04-16T17:45:41.012800Z

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

kwrooijen 2020-04-16T17:46:28.013300Z

It's global yes, so is using derive

kirill.salykin 2020-04-16T17:46:55.014200Z

so is defmethod …

kwrooijen 2020-04-16T17:46:56.014300Z

I don't think integrant supports accepting a keyword hierarchy

kirill.salykin 2020-04-16T17:46:57.014500Z

right 🙂

kwrooijen 2020-04-16T17:47:21.014900Z

So the only option is globally defining this I think

kirill.salykin 2020-04-16T17:48:17.015600Z

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

kirill.salykin 2020-04-16T17:48:25.015800Z

anyway, thanks!

kirill.salykin 2020-04-16T17:49:56.016700Z

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

kwrooijen 2020-04-16T17:51:07.017Z

Where you describe what derives from what, you mean?

kwrooijen 2020-04-16T17:52:09.017300Z

To prevent deriving in global scope?

kirill.salykin 2020-04-16T17:56:12.019500Z

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 ...) }})

kwrooijen 2020-04-16T17:57:00.020100Z

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

kirill.salykin 2020-04-16T17:57:23.020300Z

also possible

kirill.salykin 2020-04-16T17:57:36.020900Z

thanks and have a great avond

kwrooijen 2020-04-16T17:58:04.021100Z

Fijne avond 🙂