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

Add split and split-re tags (#55) #93

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
add split tags
igrishaev committed Jun 2, 2020

Verified

This commit was signed with the committer’s verified signature.
Zaczero Kamil Monicz
commit aa02a2ad9f32c2ba9afac1ca594012b18ceec066
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -130,6 +130,27 @@ Use `#or` when you want to provide a list of possibilities, perhaps with a defau
#env PROD_PASSWD]}
```

### split

`#split` is used to split a string with comma. These values often come from
environment variables that lack structure and thus use delimiters.

```clojure
{:hosts #split "app1.host.com,app2.host.com"}

{:hosts #split #env APP_HOSTS}
```

### split-re

`#split-re` works the same but allows to specify a custom separator as the first
item of the following vector. As EDN doesn't support `#"regex"` syntax, use a
plain string which gets converted with `re-pattern` under the hood:

```clojure
{:hosts #split-re ["\\t" "app1.host.com\tapp2.host.com"]}
```

### profile

Use profile as a kind of reader conditional.
16 changes: 15 additions & 1 deletion src/aero/core.cljc
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
[expand expand-scalar-repeatedly expand-case eval-tagged-literal
reassemble kv-seq]]
[aero.impl.walk :refer [postwalk]]
[clojure.string :as str]
#?@(:clj [[clojure.edn :as edn]
[aero.impl.macro :as macro]]
:cljs [[cljs.tools.reader.edn :as edn]
@@ -101,6 +102,19 @@
[opts tag values]
(apply merge values))

(letfn [(-split [value re]
(if-not (str/blank? value)
(str/split value re)
[]))]

(defmethod reader 'split
[opts tag value]
(-split value #","))

(defmethod reader 'split-re
[opts tag [re value]]
(-split value (re-pattern re))))

#?(:clj
(defn relative-resolver [source include]
(let [fl
@@ -240,7 +254,7 @@
::value tl})))
(assoc expansion ::value (get env value)))))

(defmethod eval-tagged-literal 'profile
(defmethod eval-tagged-literal 'profile
[tl opts env ks]
(expand-case (:profile opts) tl opts env ks))

4 changes: 4 additions & 0 deletions test/aero/config.edn
Original file line number Diff line number Diff line change
@@ -14,6 +14,10 @@
:false-boolean #boolean "false"
:trivial-false-boolean #boolean "OTHER"
:nil-false-boolean #boolean ""
:split-comma #split "host1,host2,host3"
:split-comma-nil #split nil
:split-comma-empty #split ""
:split-re #split-re ["\\t" "foo\tbar\tbaz"]
:long #long "1234"
:double #double "4567.8"
:keyword #keyword "foo/bar"
12 changes: 12 additions & 0 deletions test/aero/core_test.cljc
Original file line number Diff line number Diff line change
@@ -151,6 +151,18 @@
(let [config (read-config "test/aero/config.edn" {:profile :dev})]
(is (= "dummy" (:dummy config)))))

(deftest split-test
(let [config (read-config "test/aero/config.edn" {:profile :dev})]
(is (= ["host1" "host2" "host3"]
(:split-comma config)))
(is (= [] (:split-comma-nil config)))
(is (= [] (:split-comma-empty config)))))

(deftest split-re-test
(let [config (read-config "test/aero/config.edn" {:profile :dev})]
(is (= ["foo" "bar" "baz"]
(:split-re config)))))

#?(:clj
(deftest resolver-tests
(let [source (io/resource "aero/includes.edn")]