Skip to content

Instantly share code, notes, and snippets.

@ghoseb ghoseb/props.clj
Last active Dec 12, 2015

Embed
What would you like to do?
Read a Java Properties file into a map (with potentially nested keys).
(ns ^{:doc "Read Java properties file."
:author "Baishampayan Ghose <b.ghose@gmail.com>"}
in.freegeek.props
(:import java.util.Properties)
(:require [clojure.java.io :refer [resource reader]]
[clojure.string :refer [split]]))
(defn- load-props
"Load a Java properties file. File should be in classpath."
[props-file]
(doto (Properties.)
(.load (-> props-file resource reader))))
(defn- key->path
[k]
(map keyword (split k #"\.")))
(defn read-props
"Read a Java properties file into a map."
[props-file]
(let [props (load-props props-file)]
(reduce (fn [res k]
(assoc-in res (key->path k) (get props k)))
{}
(keys props))))
name = configuration-clj
version = 0.2.4
db.name = whacko-db
db.host = example.org
db.port = 4567
ring.handler.ns = some-ring-handler-ns
ring.handler.protocol = binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.