Skip to content

Instantly share code, notes, and snippets.

@joshrotenberg
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshrotenberg/6274576fb605279ada93 to your computer and use it in GitHub Desktop.
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.
(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