Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
clj-util-properties/src/main/clojure/de/mpg/shh/util_properties/properties.clj
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
27 lines (25 sloc)
1.09 KB
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
(ns de.mpg.shh.util-properties.properties | |
(:require [clojure.java.io :as io] | |
[clojure.string :as str])) | |
(defn properties->map | |
"Convert the properties list to a map, transforming the value | |
whenever a transformer is defined for that key." | |
([properties] | |
(properties->map properties {})) | |
([properties transformer-for] | |
(letfn [(keyfor [s] (mapv keyword (str/split (key s) #"\.")))] | |
(reduce (fn [accum entry] | |
(let [k (keyfor entry)] | |
(if-let [f (transformer-for k)] | |
(assoc-in accum k (f (val entry))) | |
(assoc-in accum k (val entry))))) | |
{} properties)))) | |
(defn load-properties | |
"Load named properties file from the classpath, return a map with | |
values optionally transformed by `transform-map`." | |
([resource-name] | |
(load-properties resource-name {})) | |
([resource-name transform-map] | |
(when-let [resource (io/resource resource-name)] | |
(let [properties (doto (java.util.Properties.) (.load (io/input-stream resource)))] | |
(properties->map properties transform-map))))) |