Skip to content

Instantly share code, notes, and snippets.

@KingCode
Last active January 10, 2018 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KingCode/75026a8bdae9415e773a25f0aac99e5d to your computer and use it in GitHub Desktop.
Save KingCode/75026a8bdae9415e773a25f0aac99e5d to your computer and use it in GitHub Desktop.
When the config is in a file, (ig/ref ...) doesn't get evaluated in the dependent method- see core.clj below. It works as expected when config is a literal (see core2.clj)
{:hello-integrant/data
{:my-data {:a 1 :b 2}}
:hello-integrant/consumer
{:id 1
:param (ig/ref :hello-integrant/data)}}
(ns hello-integrant.core
(:require [integrant.core :as ig]))
(defmethod ig/init-key :hello-integrant/data [_ {:keys [my-data]}]
my-data)
(defmethod ig/init-key :hello-integrant/consumer [_ {:keys [id param]}]
(println :ID id :PARAM param)
param)
(def config (ig/read-string (slurp "src/hello_integrant/config.edn")))
(ig/init config)
;; => :ID 1 :PARAM (ig/ref :hello-integrant/data)
(ns hello-integrant.core2
(:require [integrant.core :as ig]))
(def config {:hello-integrant/data
{:my-data {:a 1 :b 2}}
:hello-integrant/consumer
{:id 1
:param (ig/ref :hello-integrant/data)}})
(defmethod ig/init-key :hello-integrant/data [_ {:keys [my-data]}]
my-data)
(defmethod ig/init-key :hello-integrant/consumer [_ {:keys [id param]}]
(println :ID id :PARAM param)
param)
(ig/init config)
;; => :ID 1 :PARAM {:a 1, :b 2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment