Skip to content

Commit

Permalink
Lint-fix, testing doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmoreira committed Apr 8, 2024
1 parent 70b67f6 commit 51464ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
20 changes: 10 additions & 10 deletions src/codes/clj/docs/backend/config.clj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
(ns codes.clj.docs.backend.config
(:require [aero.core :as aero]
[clojure.string :as string]))
(: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))
(let [split-configs (-> value
(string/split #","))
env-config (->> split-configs
(map string/trim)
(remove empty?))]
env-config)
value))

(defmethod aero/reader 'csv
(defmethod aero/reader 'csv
[_ _ value]
(str-var->vector-var value))
28 changes: 14 additions & 14 deletions test/unit/codes/clj/docs/backend/config_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
(use-fixtures :once helpers.malli/with-intrumentation)

(deftest str-var->vector-var-test
(testing "root-level configs should be converted to vectors"
(is (match? ["value1" "value2"]
(#'backend.config/str-var->vector-var "value1, value2"))))
(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 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,")))))
(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")))))
(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 51464ce

Please sign in to comment.