Last active
August 29, 2015 14:13
-
-
Save joshrotenberg/6274576fb605279ada93 to your computer and use it in GitHub Desktop.
Quick attempt to take a nice Clojure map and convert it to a format that will play nice with Java Properties. Inspired by similar clojure.walk functions.
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
(require '[clojure.walk :refer [postwalk]] | |
[clojure.string :as s]) | |
(defn keyword-to-property | |
"Replace :foo-bar with \"foo.bar\", etc." | |
[k] | |
(s/replace (name k) #"\-" ".")) | |
(defn propertyize-map | |
"Recursively transforms all map keys from keywords to Java property strings and values to strings (if necessary)." | |
[m] | |
(let [f (fn [[k v]] | |
(let [k (keyword-to-property k)] | |
(cond | |
(string? v) [k v] | |
(keyword? v) [k (name v)] | |
:else [k (str v)])))] | |
(postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m))) | |
; (propertyize-map {:doof-cha :hi :bruh "sup" :what-the-heck 99}) | |
; => {"what.the.heck" "99", "bruh" "sup", "doof.cha" "hi"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment