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

feat: env vars as vectors #33

Merged
merged 11 commits into from
Apr 8, 2024
Merged
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
5 changes: 3 additions & 2 deletions resources/config.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{:webserver/port #long #or [#env PORT 3001]
:webserver/allowed-origins ["http://docs.clj.codes" "https://docs.clj.codes"
"http://docs-frontend.fly.dev" "https://docs-frontend.fly.dev"]
:webserver/allowed-origins #csv #or [#env ALLOWED_ORIGINS
["http://docs.clj.codes" "https://docs.clj.codes"
"http://docs-frontend.fly.dev" "https://docs-frontend.fly.dev"]]

:database {:dbtype "postgres"
:dbname #or [#env DB_NAME "postgres"]
Expand Down
20 changes: 20 additions & 0 deletions src/codes/clj/docs/backend/config.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns codes.clj.docs.backend.config
(:require [aero.core :as aero]
[clojure.string :as string]))

(defn- str-var->vector-var
"Converts a string config variable to a vector of strings, when applicable.
Environment variables are expected to be set as comma-separated values."
[value]
(if (string? value)
(let [split-configs (-> value
(string/split #","))
env-config (->> split-configs
(map string/trim)
(remove empty?))]
env-config)
value))

(defmethod aero/reader 'csv
[_ _ value]
(str-var->vector-var value))
1 change: 1 addition & 0 deletions src/codes/clj/docs/backend/server.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns codes.clj.docs.backend.server
(:require [codes.clj.docs.backend.components.db-docs :as components.db-docs]
[codes.clj.docs.backend.config]
[codes.clj.docs.backend.db.datalevin :refer [read-conn-opts]]
[codes.clj.docs.backend.routes :as routes]
[com.stuartsierra.component :as component]
Expand Down
3 changes: 3 additions & 0 deletions test/resources/csv-config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{:some-config #csv #or [#env SOME "value1, value2"]
:malformed-config #csv #or [#env MALFORMED "value3, value4,value5, "]
:trailing-comma-config #csv #or [#env TRAILING_COMMA "value6, value7,"]}
28 changes: 28 additions & 0 deletions test/unit/codes/clj/docs/backend/config_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns unit.codes.clj.docs.backend.config-test
(:require [aero.core :as aero]
[clojure.test :refer [deftest is testing use-fixtures]]
[codes.clj.docs.backend.config :as backend.config]
[matcher-combinators.test :refer [match?]]
[parenthesin.helpers.malli :as helpers.malli]))

(use-fixtures :once helpers.malli/with-intrumentation)

(deftest str-var->vector-var-test
(testing "csv configs should be converted to vectors"
(is (match? ["value1" "value2"]
(#'backend.config/str-var->vector-var "value1, value2"))))

(testing "trailing and extra whitespaces should be ignored"
(is (match? ["value3" "value4" "value5"]
(#'backend.config/str-var->vector-var "value3, value4,value5, "))))

(testing "trailing commas should be ignored"
(is (match? ["value6" "value7"]
(#'backend.config/str-var->vector-var "value6, value7,")))))

(deftest csv-reader-test
(testing "tag literal #csv should turn comma-separated strings into vectors"
(is (match? {:some-config ["value1" "value2"]
:malformed-config ["value3" "value4" "value5"]
:trailing-comma-config ["value6" "value7"]}
(aero/read-config "test/resources/csv-config.edn")))))