Last active
December 12, 2015 08:28
-
-
Save ghoseb/4743856 to your computer and use it in GitHub Desktop.
Read a Java Properties file into a map (with potentially nested keys).
This file contains hidden or 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
(ns ^{:doc "Read Java properties file." | |
:author "Baishampayan Ghose <[email protected]>"} | |
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)))) |
This file contains hidden or 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
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