Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle generating nested extractable records #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/outpace/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.pprint]
[outpace.config.bootstrap :refer [find-config-source]]))

(def generating? false)
Expand Down Expand Up @@ -53,6 +54,9 @@
(defmethod print-method EnvVal [^EnvVal ev ^java.io.Writer w]
(.write w (str "#config/env " (pr-str (.name ev)))))

(defmethod clojure.pprint/simple-dispatch EnvVal [v]
(pr v))

(defn read-env
"Returns an EnvVal identified by the specified string name."
[name]
Expand All @@ -69,6 +73,9 @@
(defmethod print-method PropVal [^PropVal pv ^java.io.Writer w]
(.write w (str "#config/property " (pr-str (.name pv)))))

(defmethod clojure.pprint/simple-dispatch PropVal [v]
(pr v))

(defn read-property
"Returns a PropVal identified by the specified string name."
[name]
Expand All @@ -85,6 +92,9 @@
(defmethod print-method FileVal [^FileVal fv ^java.io.Writer w]
(.write w (str "#config/file " (pr-str (.path fv)))))

(defmethod clojure.pprint/simple-dispatch FileVal [v]
(pr v))

(defn read-file
"Returns a FileVal identified by the specified string path."
[path]
Expand Down Expand Up @@ -117,6 +127,9 @@
(.write w "#config/or ")
(.write w (pr-str (.vals v))))

(defmethod clojure.pprint/simple-dispatch OrVal [v]
(pr v))

(defn read-or
"Returns an OrVal from a vector."
[source]
Expand All @@ -134,6 +147,9 @@
(defmethod print-method EdnVal [^EdnVal ev ^java.io.Writer w]
(.write w (str "#config/edn " (pr-str (.source ev)))))

(defmethod clojure.pprint/simple-dispatch EdnVal [v]
(pr v))

(defn read-edn
"Returns an EdnVal from a string value. Can be composed with other readers."
[source]
Expand Down
30 changes: 25 additions & 5 deletions test/outpace/config_test.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns outpace.config-test
(:require [clojure.test :as t :refer [deftest is testing]]
[clojure.pprint]
[clojure.string :as str]
[outpace.config :as c :refer [defconfig defconfig!]])
(:import (clojure.lang ExceptionInfo)
(java.io File)
Expand All @@ -14,6 +16,11 @@
(unmap-non-fn-vars *ns*)
(f)))

(defn pprint-str [v]
(str/trim-newline
(with-out-str
(clojure.pprint/pprint v))))

(deftest test-EnvVal
(let [name "name"
value "value"
Expand All @@ -27,7 +34,9 @@
(is (= value (c/extract ev)))
(is (= defined? (c/provided? ev))))
(testing "EnvVal edn-printing"
(is (= (str "#config/env " (pr-str name)) (pr-str ev))))))
(is (= (str "#config/env " (pr-str name)) (pr-str ev))))
(testing "EnvVal nested pprinting"
(is (= (str "{:foo #config/env " (pr-str name) "}") (pprint-str {:foo ev}))))))

(defn env-var-name []
(let [name (first (keys (java.lang.System/getenv)))]
Expand Down Expand Up @@ -66,7 +75,9 @@
(is (= value (c/extract ev)))
(is (= defined? (c/provided? ev))))
(testing "PropVal edn-printing"
(is (= (str "#config/property " (pr-str name)) (pr-str ev))))))
(is (= (str "#config/property " (pr-str name)) (pr-str ev))))
(testing "PropVal nested pprinting"
(is (= (str "{:foo #config/property " (pr-str name) "}") (pprint-str {:foo ev}))))))

(defn prop-var-name []
(let [name (first (keys (java.lang.System/getProperties)))]
Expand Down Expand Up @@ -105,7 +116,9 @@
(is (= contents (c/extract fv)))
(is (= exists? (c/provided? fv))))
(testing "FileVal edn-printing"
(is (= (str "#config/file " (pr-str path)) (pr-str fv))))))
(is (= (str "#config/file " (pr-str path)) (pr-str fv))))
(testing "FileVal nested pprinting"
(is (= (str "{:foo #config/file " (pr-str path) "}") (pprint-str {:foo fv}))))))

(deftest test-read-file
(let [file (File/createTempFile "test-read-file" ".txt")
Expand Down Expand Up @@ -156,7 +169,12 @@
(is (= 3.14 (c/extract or-val)))
(is (true? (c/provided? or-val)))
(is (= "#config/or [#config/env \"foo\" #config/env \"bar\"]"
(pr-str or-val))))))
(pr-str or-val)))))
(testing "OrVal nested pprinting"
(let [or-val (c/->OrVal [(c/->EnvVal "foo" nil false)
(c/->EnvVal "bar" 3.14 true)])]
(is (= "{:key #config/or [#config/env \"foo\" #config/env \"bar\"]}"
(pprint-str {:key or-val}))))))

(deftest test-read-or
(testing "no values"
Expand Down Expand Up @@ -211,7 +229,9 @@
(is (= value (c/extract ev)))
(is (= source-provided? (c/provided? ev))))
(testing "EdnVal edn-printing"
(is (= (str "#config/edn " (pr-str source)) (pr-str ev))))))
(is (= (str "#config/edn " (pr-str source)) (pr-str ev))))
(testing "EdnVal nested pprinting"
(is (= (str "{:foo #config/edn " (pr-str source) "}") (pprint-str {:foo ev}))))))

(defn extractable [value provided]
(reify
Expand Down