Skip to content

Instantly share code, notes, and snippets.

@walterl

walterl/demo.clj Secret

Created December 1, 2021 23:34
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 walterl/67a2690ebea01990b7c64432a0ea10df to your computer and use it in GitHub Desktop.
Save walterl/67a2690ebea01990b7c64432a0ea10df to your computer and use it in GitHub Desktop.
Integrant demo: swapping out components using Clojure's key hierarchy
(ns demo
(:require [integrant.core :as ig]))
(def config
{::main {:widget (ig/ref ::base-widget)}
::prod-widget {:name "Production widget"}})
(derive ::prod-widget ::base-widget)
(derive ::mock-widget ::base-widget)
(defmethod ig/init-key ::prod-widget
[_ config]
(str "Prod: " (:name config)))
(defmethod ig/init-key ::mock-widget
[_ config]
(str "Mock: " (:type config)))
(defmethod ig/init-key ::main
[_ {:keys [widget]}]
(println "Widget:" widget))
(defn prod-system
[_]
(println "Production system:" (ig/init config)))
(defn test-system
[_]
(let [test-config (-> config
(dissoc ::prod-widget)
(assoc ::mock-widget {:type "my mock"}))]
(println "Test system with mock component:" (ig/init test-config))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment