generated from parenthesin/microservice-boilerplate-malli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from clj-codes/feat/env-vars
feat: env vars as vectors
- Loading branch information
Showing
5 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))))) |