Back
reader (clj)
(source)multimethod
Examples
aero
(ns aero.core-test
(:require
[aero.core :refer [read-config reader
#?@(:clj [deferred resource-resolver root-resolver])
#?(:cljs Deferred)]
#?@(:cljs [:refer-macros [deferred]])]
[clojure.test :refer [deftest is testing are]]
#?@(:clj [[clojure.java.io :as io]]
:cljs [[goog.object :as gobj]
[goog.string :as gstring]
goog.string.format
[cljs.tools.reader.reader-types
:refer [source-logging-push-back-reader]]]))
#?(:clj (:import [aero.core Deferred])))
(defn- string-reader
[str]
(#?(:cljs source-logging-push-back-reader
:clj java.io.StringReader.) str))
(defmethod reader 'expensive-network-call
[_ tag value]
(deferred
(swap! network-call-count inc)
(inc value)))
(defmethod reader 'myflavor
[opts tag value]
(if (= value :favorite) :chocolate :vanilla))
(defn source [path]
#?(:clj (io/reader path)
:cljs path))
#?(:clj
(deftest resolver-tests
(let [source (io/resource "aero/includes.edn")]
(is (read-config source {:profile :relative}))
(is (read-config source {:profile :relative-abs}))
(is (read-config source {:profile :resource :resolver resource-resolver}))
(is (read-config source {:profile :file :resolver root-resolver}))
(is (read-config (-> source slurp string-reader)
{:profile :relative-abs}))
(is (read-config source {:profile :map
:resolver {:sub-includes (io/resource "aero/sub/includes.edn")
:valid-file (io/resource "aero/valid.edn")}}))
(is (:aero/missing-include (read-config source {:profile :file-does-not-exist}))))))
(deftest dangling-ref-test
(is (= {:user {:favorite-color :blue}
:gardner {:favorite-color :blue}
:karl {:favorite-color :blue}
:color :blue}
(read-config
(string-reader
(binding [*print-meta* true]
(pr-str {:user ^:ref [:karl]
:gardner {:favorite-color ^:ref [:color]}
:karl ^:ref [:gardner]
:color :blue})))))))
(deftest default-reader-combo-test
(let [config (read-config "test/aero/default-reader.edn")]
(is (= #inst "2013-07-09T18:05:53.231-00:00" (:date config)))))
(deftest ref-in-set-test
(is (= #{10}
(:bar
(read-config
(string-reader
"{:foo 10 :bar #{#ref [:foo]}}"))))))
(deftest or-incomplete-child
(let [config-str "{:x \"foo\"
:y #or [#ref [:x] \"bar\"]
:junk0 \"0\"
:junk1 \"1\"
:junk2 \"2\"
:junk3 \"3\"
:junk4 \"4\"
:junk5 \"5\"
:junk6 \"6\"
:junk7 \"7\"
:junk8 \"8\"}"
config (string-reader config-str)]
(is (= "foo" (:x (read-config config))))))
(deftest or-dangling-ref
(let [config-str "{:y #or [#ref [:x] \"bar\"]}"
config (string-reader config-str)]
(is (= "bar" (:y (read-config config))))))
(deftest meta-preservation-test
(are [ds] (= ds
(::foo
(meta
(read-config
(string-reader
(binding [*print-meta* true]
(pr-str (with-meta ds {::foo ds}))))))))
[]
{}
#{}
()
[1]
{:a :b}
#{:a :b}
'(1)))
kit-clj/kit
(ns kit.edge.scheduling.quartz
(:require
[aero.core :as aero]
[integrant.core :as ig]
[kit.ig-utils :as ig-utils]
[troy-west.cronut :as cronut]))
(defmethod aero/reader 'cronut/trigger
[_ _ value]
(cronut/trigger-builder value))
(defmethod aero/reader 'cronut/cron
[_ _ value]
(cronut/shortcut-cron value))
(defmethod aero/reader 'cronut/interval
[_ _ value]
(cronut/shortcut-interval value))