Back

refset (clj)

(source)

function

(refset key)
Create a set of references to all matching top-level keys in a config map.

Examples

integrant
(ns integrant.core-test
  (:require #?(:clj  [clojure.test :refer [are deftest is testing]]
               :cljs [cljs.test :refer-macros [are deftest is testing]])
            [integrant.core :as ig]
            [weavejester.dependency :as dep]))

(deftest refset-test
  (is (ig/refset? (ig/refset ::foo)))
  (is (ig/refset? (ig/refset [::foo ::bar])))
  (is (ig/reflike? (ig/refset ::foo)))
  (is (ig/reflike? (ig/refset [::foo ::bar]))))

#?(:clj
   (deftest read-string-test
     (is (= (ig/read-string "{:foo/a #ig/ref :foo/b, :foo/b 1}")
            {:foo/a (ig/ref :foo/b), :foo/b 1}))
     (is (= (ig/read-string "{:foo/a #ig/refset :foo/b, :foo/b 1}")
            {:foo/a (ig/refset :foo/b), :foo/b 1}))
     (is (= (ig/read-string {:readers {'test/var find-var}}
                            "{:foo/a #test/var clojure.core/+}")
            {:foo/a #'+}))))

(deftest dependency-graph-test
  (let [m {::a (ig/ref ::p), ::b (ig/refset ::ppp) ::p 1, ::pp 2}]
    (testing "graph with refsets"
      (let [g (ig/dependency-graph m)]
        (is (dep/depends? g ::a ::p))
        (is (dep/depends? g ::b ::p))
        (is (dep/depends? g ::b ::pp))))

    (testing "graph without refsets"
      (let [g (ig/dependency-graph m {:include-refsets? false})]
        (is (dep/depends? g ::a ::p))
        (is (not (dep/depends? g ::b ::p)))
        (is (not (dep/depends? g ::b ::pp)))))))

(deftest expand-test
  (testing "default expand"
    (is (= (ig/expand {::unique 1})
           {::unique 1})))
  (testing "empty map values"
    (is (= (ig/expand {::unique {}})
           {::unique {}}))
    (is (= (ig/expand {::a {}, ::mod-a {:x 1}})
           {::a {:x 1}}))
    (is (= (ig/expand {::z {}, ::mod-z {:x 1}})
           {::z {:x 1}})))
  (testing "single expand"
    (is (= (ig/expand {::mod 1})
           {::a 1, ::b {:v 1}})))
  (testing "expand with unrelated keys"
    (is (= (ig/expand {::mod 1, ::b {:x 1}, ::c 2})
           {::a 1, ::b {:v 1, :x 1}, ::c 2})))
  (testing "expand with direct override"
    (is (= (ig/expand {::mod {:x 1}, ::a ^:override {:x 2}})
           {::a {:x 2}, ::b {:v {:x 1}}})))
  (testing "expand with nested override"
    (is (= (ig/expand {::mod 1, ::b ^:override {:v 2}})
           {::a 1, ::b {:v 2}}))
    (is (= (ig/expand {::mod-c 1, ::c ^:override {:x {:y {:z 2}}}})
           {::c {:x {:y {:z 2}}}})))
  (testing "unresolved conflicting index"
    (is (thrown-with-msg?
         #?(:clj clojure.lang.ExceptionInfo :cljs cljs.core.ExceptionInfo)
         (re-pattern (str "^Conflicting values at index "
                          "\\[:integrant\\.core-test/a\\] "
                          "for expansions: :integrant\\.core-test/mod, "
                          ":integrant\\.core-test/mod-a\\. Use the "
                          "\\^:override metadata to set the preferred "
                          "value\\.$"))
         (ig/expand {::mod 1, ::mod-a 2}))))
  (testing "unresolved conflicting nested index"
    (is (thrown-with-msg?
         #?(:clj clojure.lang.ExceptionInfo :cljs cljs.core.ExceptionInfo)
         (re-pattern (str "^Conflicting values at index "
                          "\\[:integrant\\.core-test/b :v\\] "
                          "for expansions: :integrant\\.core-test/mod, "
                          ":integrant\\.core-test/mod-b\\. Use the "
                          "\\^:override metadata to set the preferred "
                          "value\\.$"))
         (ig/expand {::mod 1, ::mod-b 2}))))
  (testing "resolved conflict"
    (is (= (ig/expand {::mod {:x 1}, ::mod-a {:x 2}, ::a ^:override {:x 3}})
           {::a {:x 3}, ::b {:v {:x 1}}})))
  (testing "resolved nested conflict"
    (is (= (ig/expand {::mod 1, ::mod-b 2, ::b ^:override {:v 3}})
           {::a 1, ::b {:v 3}}))
    (is (= (ig/expand {[::one ::mod-c] 1
                       [::two ::mod-c] 2
                       ::c ^:override {:x {:y {:z 3}}}})
           {::c {:x {:y {:z 3}}}})))
  (testing "expand with refs"
    (let [m {::a (ig/ref ::b) ::b 1}]
      (is (= m (ig/expand m))))
    (let [m {::a (ig/refset ::b) ::b 1}]
      (is (= m (ig/expand m))))))

  (testing "with refsets"
    (reset! log [])
    (let [m (ig/init {::a (ig/refset ::ppp), ::p 1, ::pp 2})]
      (is (= m {::a [#{[1] [2]}], ::p [1], ::pp [2]}))
      (is (= @log [[:init ::p 1]
                   [:init ::pp 2]
                   [:init ::a #{[1] [2]}]]))))

  (testing "with refsets and keys"
    (reset! log [])
    (let [m {::a (ig/refset ::ppp), ::p 1, ::pp 2}]
      (is (= (ig/init m [::a])      {::a [#{}]}))
      (is (= (ig/init m [::a ::p])  {::a [#{[1]}] ::p [1]}))
      (is (= (ig/init m [::a ::pp]) {::a [#{[1] [2]}] ::p [1] ::pp [2]}))))
kit-clj/kit
(deftest inject-clj-test
  (let [zloc     (z/of-string "(ns foo.core\n  (:require\n    [clojure.tools.logging :as log]\n    [integrant.core :as ig]))")
        requires [['foo :as 'bar]
                  ['bar :as 'baz]]]
    (= "(ns foo.core\n (:require\n   [clojure.tools.logging :as log]\n   [integrant.core :as ig] \n   [foo :as bar] \n   [bar :as baz]))"
       (z/root-string (append-requires zloc requires)))))

(deftest injection-test
  (when (.exists (jio/file test-config))
    (jio/delete-file test-config))
  (spit test-config (slurp config-template))
  (is
    (=
      "{:system/env #profile {:dev :dev, :test :test, :prod :prod}, :server/http {:port #long #or [#env PORT 3000], :handler #ig/ref :handler/ring}, :handler/ring {:router #ig/ref :router/core, :api-path \"/api\"}, :reitit.routes/api {:base-path \"/api\", :env #ig/ref :system/env}, :router/routes {:routes #ig/refset :reitit/routes}, :router/core {:routes #ig/ref :router/routes}, :foo :bar}
"
      (io/edn->str
        (inject {:type   :edn
                 :target (io/str->edn (slurp "test/resources/generated/system.edn"))
                 :query  []
                 :action :merge
                 :value  {:foo :bar}})))))
duct-framework/core
(ns duct.core-test
  (:require [clojure.java.io :as io]
            [clojure.test :refer :all]
            [duct.core :as core]
            [duct.core.merge :as merge]
            [integrant.core :as ig]))

(deftest test-merge-configs
  (are [a b c] (= (core/merge-configs a b) c)
    {::a 1}                {::a 2}                       {::a 2}
    {::a {:x 1}}           {::a {:y 2}}                  {::a {:x 1 :y 2}}
    {::a {:x 1}}           {::a ^:displace {:x 2}}       {::a {:x 1}}
    {}                     {::a ^:displace {:y 2}}       {::a {:y 2}}
    {::aa 1}               {::a 2}                       {::aa 2}
    {::aa 1 ::ab 2}        {::a 3}                       {::aa 3 ::ab 3}
    {::aa {:x 1}}          {::a {:y 2}}                  {::aa {:x 1 :y 2}}
    {::a 1}                {::aa 2}                      {::aa 2}
    {::a {:x 1}}           {::aa {:y 2}}                 {::aa {:x 1 :y 2}}
    {::a {:x 1}}           {::aa {:y 2} ::ab {:z 3}}     {::aa {:x 1 :y 2} ::ab {:x 1 :z 3}}
    {::a 1}                {::a (merge/displace 2)}      {::a 1}
    {::a {:x 1}}           {::a {:x (merge/displace 2)}} {::a {:x 1}}
    {::a [:x :y]}          {::a [:y :z]}                 {::a [:x :y :y :z]}
    {::a [:x :y]}          {::a ^:distinct [:y :z]}      {::a [:x :y :z]}
    {::a {:x 1}}           {::a ^:demote {:x 2, :y 3}}   {::a {:x 1, :y 3}}
    {::a ^:promote {:x 1}} {::a {:x 2, :y 3}}            {::a {:x 1, :y 3}}
    {::a (ig/ref ::b)}     {::a {:x 1}}                  {::a {:x 1}}
    {::a {:x 1}}           {::a (ig/ref ::b)}            {::a (ig/ref ::b)}
    {::a (ig/refset ::b)}  {::a {:x 1}}                  {::a {:x 1}}
    {::a {:x 1}}           {::a (ig/refset ::b)}         {::a (ig/refset ::b)}))

(deftest test-read-config
  (is (= (core/read-config (io/resource "duct/readers.edn") {'custom/bar (fn [x] {:x x})})
         {:foo/a {:x "bar"}
          :foo/b {:bar/a {:x 1}, :bar/b (ig/ref :bar/a) :bar/c {:baz/a {:x 1}}}
          :foo/c (core/resource "duct/config.edn")
          :foo/d (ig/ref :foo/a)
          :foo/e (ig/refset :foo/b)
          :foo/f (merge/displace 1)
          :foo/g (merge/replace [1 2])})))

(deftest test-merge-readers
  (let [conf (core/read-config (io/resource "duct/readers.edn") {'custom/bar (fn [x] {:x x})})]
    (is (= (core/merge-configs {:foo/f 2 :foo/g [3 4]} conf)
           {:foo/a {:x "bar"}
            :foo/b {:bar/a {:x 1}, :bar/b (ig/ref :bar/a) :bar/c {:baz/a {:x 1}}}
            :foo/c (core/resource "duct/config.edn")
            :foo/d (ig/ref :foo/a)
            :foo/e (ig/refset :foo/b)
            :foo/f 2
            :foo/g [1 2]}))))

(deftest test-profile-keyword
  (core/load-hierarchy)
  (let [m {:duct.profile/base  {::a 1, ::b (ig/ref ::a)}
           [:duct/profile ::x] {::a 2, ::c (ig/refset ::b)}}
        p (ig/prep m)]
    (is (= p
           {:duct.profile/base  {::a 1, ::b (core/->InertRef ::a)}
            [:duct/profile ::x] {::a 2, ::c (core/->InertRefSet ::b)
                                 ::core/requires (ig/refset :duct.profile/base)}}))
    (is (= (core/fold-modules (ig/init p))
           {::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b)}))))

(deftest test-build-config
  (core/load-hierarchy)
  (let [m {:duct.profile/base  {::a 1, ::b (ig/ref ::a)}
           [:duct/profile ::x] {::a 2, ::c (ig/refset ::b)}
           [:duct/profile ::y] {::d 3}}]
    (is (= (core/build-config m)
           {::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b), ::d 3}))
    (is (= (core/build-config m [::x])
           {::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b)}))
    (is (= (core/build-config m [:y])
           {::a 1, ::b (ig/ref ::a), ::d 3}))))

(deftest test-prep-config
  (let [m {:duct.profile/base  {::prep 1, ::a (ig/ref ::prep)}
           [:duct/profile ::x] {::prep 2, ::b (ig/refset ::a)}
           [:duct/profile ::y] {::c 3}}]
    (is (= (core/prep-config m)
           {::prep 3, ::a (ig/ref ::prep), ::b (ig/refset ::a), ::c 3}))
    (is (= (core/prep-config m [::x])
           {::prep 3, ::a (ig/ref ::prep), ::b (ig/refset ::a)}))
    (is (= (core/prep-config (assoc m [:duct.profile/base ::z] {::c 1, ::d 4}))
           {::prep 3, ::a (ig/ref ::prep), ::b (ig/refset ::a), ::c 3, ::d 4}))))

(deftest test-profile-dev-keyword
  (core/load-hierarchy)
  (let [m {:duct.profile/base {::a 1, ::b (ig/ref ::a)}
           :duct.profile/dev  {::a 2, ::c (ig/refset ::b)}}
        p (ig/prep m)]
    (is (= p
           {:duct.profile/base {::a 1, ::b (core/->InertRef ::a)}
            :duct.profile/dev  {::a 2, ::c (core/->InertRefSet ::b)
                                ::core/requires (ig/refset :duct.profile/base)
                                ::core/environment :development}}))
    (is (= (core/fold-modules (ig/init p))
           {::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b)
            ::core/environment :development}))))

(deftest test-profile-test-keyword
  (core/load-hierarchy)
  (let [m {:duct.profile/base {::a 1, ::b (ig/ref ::a)}
           :duct.profile/test {::a 2, ::c (ig/refset ::b)}}
        p (ig/prep m)]
    (is (= p
           {:duct.profile/base {::a 1, ::b (core/->InertRef ::a)}
            :duct.profile/test {::a 2, ::c (core/->InertRefSet ::b)
                                ::core/requires (ig/refset :duct.profile/base)
                                ::core/environment :test}}))
    (is (= (core/fold-modules (ig/init p))
           {::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b)
            ::core/environment :test}))))

(deftest test-profile-prod-keyword
  (core/load-hierarchy)
  (let [m {:duct.profile/base {::a 1, ::b (ig/ref ::a)}
           :duct.profile/prod {::a 2, ::c (ig/refset ::b)}}
        p (ig/prep m)]
    (is (= p
           {:duct.profile/base {::a 1, ::b (core/->InertRef ::a)}
            :duct.profile/prod {::a 2, ::c (core/->InertRefSet ::b)
                                ::core/requires (ig/refset :duct.profile/base)
                                ::core/environment :production}}))
    (is (= (core/fold-modules (ig/init p))
           {::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b)
            ::core/environment :production}))))
duct-framework/core
(ns duct.repl-test
  (:require [clojure.test :refer :all]
            [duct.core :as core]
            [duct.core.repl :as repl]
            [fipp.edn :as fipp]
            [integrant.core :as ig]))

(deftest test-pprint
  (is (= (with-out-str (fipp/pprint
                        {:a (ig/ref :duct.router/cascading)
                         :b (ig/refset :duct/module)
                         :c (core/resource "duct/core.clj")}))
         (str "{:a #ig/ref :duct.router/cascading,\n"
              " :b #ig/refset :duct/module,\n"
              " :c #duct/resource \"duct/core.clj\"}\n"))))
madstap/igviz
(ns kafka
  (:require [integrant.core :as ig]
            [madstap.igviz.alpha :as igviz]
            [medley.core :as medley]))

(def config
  {::db                     {:url "..."}
   ::cache                  {:url "..."}
   ::server                 {:db         (ig/ref ::db)
                             :cache      (ig/ref ::cache)
                             :daemons    (ig/refset :duct/daemon)
                             :all-topics (ig/refset ::topic)
                             ;; :all-consumers (ig/refset ::consumer)
                             }
   [::consumer1 ::consumer] {:topics [(ig/ref ::foo-topic)
                                      (ig/ref ::bar-topic)]}
   [::consumer2 ::consumer] {:topics [(ig/ref ::foo-topic)
                                      (ig/ref ::bar-topic)
                                      (ig/ref ::baz-topic)]}
   ::producer               {:topic (ig/ref ::baz-topic)}
   ::error-component        {:producer (ig/ref ::producer)
                             :consumer (ig/ref ::consumer1)
                             :foo (ig/ref ::bar-topic)}
   [::foo-topic ::topic]    {:topic-name "foo"}
   [::bar-topic ::topic]    {:topic-name "bar"}
   [::baz-topic ::topic]    {:topic-name "baz"}})