Skip to content

Commit

Permalink
Add :value-formatter to sectioned-properties
Browse files Browse the repository at this point in the history
Allows for using some other formatting for values in name-values based
config, eg. pr-str.
  • Loading branch information
hugoduncan committed Dec 12, 2015
1 parent 1229cd9 commit 00db370
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/pallet/config_file/format.clj
Expand Up @@ -8,22 +8,33 @@
The properties are written \"key value\", one per line.
m a key-value map
:separator chars separator to use between key and value
(default is a single space)"
[m & {:keys [separator] :or {separator " "}}]
(default is a single space)
:value-formatter function used to format values to strings"
[m & {:keys [separator value-formatter]
:or {separator " " value-formatter str}}]
(string/join
(map
(fn [[key-name value]] (format "%s%s%s\n" (name key-name) separator value))
(fn [[key-name value]]
(str (name key-name) separator (value-formatter value) \newline))
m)))

(defn sectioned-properties
"A sectioned property file.
This is modeled as a map of maps. The keys of the outer map are the section
names. The inner maps are keyword value maps."
[m & {:keys [separator] :or {separator " = "}}]
names. The inner maps are keyword value maps.
Options:
:separator chars separator to use between key and value
(default is a single space)
:value-formatter function used to format values to strings"
[m & {:keys [separator value-formatter]
:or {separator " = " value-formatter str}}]
(letfn [(format-section
[[section-name kv-map]]
(let [n (name section-name)
vs (name-values kv-map :separator separator)]
vs (name-values kv-map
:separator separator
:value-formatter value-formatter)]
(if (string/blank? n)
vs
(format "[%s]\n%s\n" n vs))))]
Expand Down
6 changes: 5 additions & 1 deletion test/pallet/config_file/format_test.clj
Expand Up @@ -15,4 +15,8 @@
(is (= "a 1\nb some-path\n"
(name-values (array-map :a 1 "b" "some-path"))))
(is (= "a=1\nb=some-path\n"
(name-values (array-map :a 1 "b" "some-path") :separator "="))))
(name-values (array-map :a 1 "b" "some-path") :separator "=")))
(is (= "a=1\nb=\"some-path\"\n"
(name-values (array-map :a 1 "b" "some-path")
:separator "="
:value-formatter pr-str))))

0 comments on commit 00db370

Please sign in to comment.