Skip to content

Instantly share code, notes, and snippets.

@ghoseb
Last active December 12, 2015 08:28
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 ghoseb/4743856 to your computer and use it in GitHub Desktop.
Save ghoseb/4743856 to your computer and use it in GitHub Desktop.
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