Skip to content

Commit

Permalink
add a test / example on how to parse/create dotted keys from ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
tolitius committed Jun 2, 2021
1 parent 28bc775 commit 64580c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions dev-resources/with-dotted-keys.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{:crux-in-crocs {:crux.and.friends/db-spec
{:crux.node/topology [cprop.tools/with-echo]
:crux.jdbc/dbtype "postgresql"
:crux.jdbc/dbname "cruxdb"
:crux.jdbc/host "OVERRIDE ME"
:crux.jdbc/user "OVERRIDE ME"
:crux.jdbc/password "OVERRIDE ME"}}}
31 changes: 30 additions & 1 deletion test/cprop/test/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [cprop.core :refer [load-config cursor]]
[cprop.source :refer [merge* from-stream from-file from-resource from-props-file from-env from-system-props]]
[cprop.tools :as tools]
[clojure.string :as s]
[clojure.edn :as edn]
[clojure.pprint :as pp]
[clojure.test :refer :all]))
Expand Down Expand Up @@ -41,7 +42,10 @@
"SOME_DATE" "7 Nov 22:44:53 2015"
"CLUSTERS__0__URL" "http://somewhere"
"CRUX__CRUX___DB_SPEC__DBNAME" "crux-db"
"CRUX_AND_FRIENDS__CRUX___AND___FRIENDS___DB_SPEC__DBNAME" "crux-and-friends-db"}
"CRUX_AND_FRIENDS__CRUX___AND___FRIENDS___DB_SPEC__DBNAME" "crux-and-friends-db"
"CRUX_IN_CROCS__CRUX_DOT_AND_DOT_FRIENDS___DB_SPEC__CRUX_DOT_JDBC___HOST" "jdbc-bc-jd"
"CRUX_IN_CROCS__CRUX_DOT_AND_DOT_FRIENDS___DB_SPEC__CRUX_DOT_JDBC___USER" "robocop"
"CRUX_IN_CROCS__CRUX_DOT_AND_DOT_FRIENDS___DB_SPEC__CRUX_DOT_JDBC___PASSWORD" "my friends call me Murphy"}
(map (fn [[k v]] [(#'cprop.source/env->path k opts)
(#'cprop.source/str->value v opts)]))
(into {})))
Expand Down Expand Up @@ -372,3 +376,28 @@
{:dbname "crux-and-friends-db"
:dbtype "postgresql"}}}
merged)))))

(deftest should-parse-dotted-namespaced-keys
"custom custom parse a dotted key
since env variables have nothing but underscore to work with.
we can use anything that suits our use case (say: _DOT_) to represent a '.' for example.
i.e. 'FOO_DOT_BAR___BAZ' env key is parsed as 'foo.bar/baz'"

(let [config (edn/read-string
(slurp "dev-resources/with-dotted-keys.edn"))
env (read-test-env {:key-parse-fn (fn [k] ;; <= custom key parse function
(-> k
(s/replace #"-dot-" ".")
keyword))})
merged (merge* config env)]

(testing "should replace namespaced dotted keys with ENV values"
(is (= {:crux-in-crocs {:crux.and.friends/db-spec
{:crux.node/topology '[cprop.tools/with-echo]
:crux.jdbc/dbtype "postgresql"
:crux.jdbc/dbname "cruxdb"
:crux.jdbc/host "jdbc-bc-jd"
:crux.jdbc/user "robocop"
:crux.jdbc/password "my friends call me Murphy"}}}
merged)))))

0 comments on commit 64580c6

Please sign in to comment.