Skip to content

Commit

Permalink
Added support to provide data readers when reading a configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
neuromantik33 committed Dec 11, 2018
1 parent b8c2293 commit 9b789f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/omniconf/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,9 @@

(defn populate-from-file
"Fill configuration from an edn file."
([edn-file quit-on-error]
(@logging-fn "WARNING: quit-on-error arity is deprecated.")
(populate-from-file edn-file))
([edn-file]
(populate-from-file edn-file nil))
([edn-file reader-opts]
(try-log
(with-open [in (java.io.PushbackReader. (io/reader edn-file))]
(letfn [(walk [prefix spec-root tree]
Expand All @@ -331,7 +330,7 @@
(set path (if (string? value)
(parse spec value)
value))))))]
(walk [] @config-scheme (edn/read in))))))
(walk [] @config-scheme (edn/read reader-opts in))))))
{:forms '([edn-file])})

(defn populate-from-properties
Expand Down
9 changes: 7 additions & 2 deletions test/omniconf/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
[clojure.java.io :as io]
[clojure.test :refer :all]))

(defrecord TestRecord [key])
(defn test-record [key] (->TestRecord key))

(cfg/define
{:help {:description "prints this help message"
:help-name "my-script"
Expand Down Expand Up @@ -67,7 +70,9 @@
:default "one"}
:two {:type :string}}}}}
:delayed-nested {:nested {:delayed {:default "foo"
:delayed-transform #(str % "bar")}}}})
:delayed-transform #(str % "bar")}}}
:record-option {:type :edn
:description "read as Record structure"}})

(defn check-basic-options []
(is (nil? (cfg/verify :silent true)))
Expand Down Expand Up @@ -186,7 +191,7 @@
(cfg/verify :silent true))

(testing "populate-from-file"
(cfg/populate-from-file "test/omniconf/test-config.edn")
(cfg/populate-from-file "test/omniconf/test-config.edn" {:readers {'omniconf/test test-record}})
(cfg/verify :silent true))

(testing "with-options"
Expand Down
7 changes: 4 additions & 3 deletions test/omniconf/test-config.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{:existing-file-option "test/omniconf/test-config.edn"
:nonempty-dir-option "test/omniconf/"
:required-option "foobar"
:nested-option {:file "./"}}
:nonempty-dir-option "test/omniconf/"
:required-option "foobar"
:nested-option {:file "./"}
:record-option #omniconf/test :test}

0 comments on commit 9b789f0

Please sign in to comment.