Skip to content

Commit

Permalink
Merge pull request #33 from clj-codes/feat/env-vars
Browse files Browse the repository at this point in the history
feat: env vars as vectors
  • Loading branch information
rafaeldelboni authored Apr 8, 2024
2 parents 8a4075c + 51464ce commit 740f6cf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
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")))))

0 comments on commit 740f6cf

Please sign in to comment.