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

Munge special characters in env variables #8

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 23 additions & 1 deletion src/omniconf/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@
(walk (conj prefix kw-name) nested))))]
(walk [] @config-scheme)))

(def env-char-replacements
"Valid characters in a keyword and their replacements in environment variables.
Check out https://clojure.org/reference/reader for a list of allowed characters."
{\/ "__SLASH__"
\. "__DOT__"
\* "__STAR__"
\+ "__PLUS__"
\! "__EXCL__"
\- "__"
\_ "__BAR__"
\' "__TICK__"
\? "__Q__"})

(defn munge-env-fragment
"Takes a keyword and replaces all special characters to make it a valid
environment variable fragment."
[fragment]
(->> (name fragment)
(map (fn [c] (get env-char-replacements c c)))
(apply str)
(.toUpperCase)))

(defn define
"Declare the configuration that the program supports. `scheme` is a map of
keyword names to specs.
Expand Down Expand Up @@ -189,7 +211,7 @@
(assoc :name kw-name)
(update-in [:env-name]
#(->> (conj prefix (or % kw-name))
(map (fn [x] (.replace (.toUpperCase (name x)) "-" "_")))
(map munge-env-fragment)
(str/join "__")))
(update-in [:opt-name]
#(->> (conj prefix (or % kw-name))
Expand Down