diff --git a/src/pallet/config_file/format.clj b/src/pallet/config_file/format.clj index 2c73532b..52fdc2d2 100644 --- a/src/pallet/config_file/format.clj +++ b/src/pallet/config_file/format.clj @@ -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))))] diff --git a/test/pallet/config_file/format_test.clj b/test/pallet/config_file/format_test.clj index f50513d6..95350431 100644 --- a/test/pallet/config_file/format_test.clj +++ b/test/pallet/config_file/format_test.clj @@ -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))))