From 7124a638207b7f7616d59d9dc639d3c79e426cf4 Mon Sep 17 00:00:00 2001 From: Joshua Eckroth Date: Wed, 10 Dec 2014 08:41:20 -0500 Subject: [PATCH] Updated deps, added support for reading streams, updated version. --- README.md | 4 ++++ project.clj | 8 ++++---- src/propertea/core.clj | 9 ++++++++- test/propertea/test/core.clj | 3 +++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 82eebeb..e97a9ce 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ few of the properties to their desired types. (println props) ; => {:int-example 1, :string-example "hello-string", :boolean-example true} +An input stream can be used instead of a filename. This is useful to read property files in a Jar: +
+(read-properties (.getResourceAsStream (.getContextClassLoader (Thread/currentThread)) "some.properties"))
+
propertea can also validate that required properties are specified.
 ; assuming the same properties file as above
diff --git a/project.clj b/project.clj
index 6be85b1..1e405a4 100644
--- a/project.clj
+++ b/project.clj
@@ -1,5 +1,5 @@
-(defproject cc.artifice/propertea "1.3.2"
+(defproject cc.artifice/propertea "1.4.0"
   :description "load, coerce, and validate property files"
-  :dependencies [[org.clojure/clojure "1.4.0"]
-                 [expectations "1.4.33"]]
-  :plugins [[lein-expectations "0.0.7"]])
+  :dependencies [[org.clojure/clojure "1.5.1"]
+                 [expectations "2.0.13"]]
+  :plugins [[lein-expectations "0.0.8"]])
diff --git a/src/propertea/core.clj b/src/propertea/core.clj
index fdcf433..a28a890 100644
--- a/src/propertea/core.clj
+++ b/src/propertea/core.clj
@@ -1,6 +1,6 @@
 (ns propertea.core
   (:require clojure.walk clojure.set clojure.string)
-  (:import [java.io FileReader]
+  (:import [java.io FileReader InputStream]
            [java.util Properties]))
 
 (defn keywordize-keys-unless [m b]
@@ -25,6 +25,9 @@
           {}
           props))
 
+(defn input-stream->properties [stream]
+  (doto (Properties.) (.load stream)))
+
 (defn file-name->properties [file-name]
   (doto (Properties.)
     (.load (FileReader. file-name))))
@@ -108,6 +111,10 @@
     (validate required)
     (dump dump-fn)))
 
+(defmethod read-properties InputStream [stream & x]
+  (let [props (input-stream->properties stream)]
+    (apply read-properties props x)))
+
 (defmethod read-properties :default [file & x]
   (let [props (file-name->properties file)]
     (apply read-properties props x)))
diff --git a/test/propertea/test/core.clj b/test/propertea/test/core.clj
index c09a16f..5ac1297 100644
--- a/test/propertea/test/core.clj
+++ b/test/propertea/test/core.clj
@@ -6,10 +6,13 @@
 
 (def fp (clojure.java.io/file "test/fake.properties"))
 
+(def instream (clojure.java.io/input-stream fp))
+
 (def props (doto (Properties.) (.setProperty "key-from-properties" "value-in-properties")))
 
 ;;; read a string
 (expect "hello-string" (:string-example (read-properties fp)))
+(expect "hello-string" (:string-example (read-properties instream)))
 
 ;;; read a string from properties object
 (expect "value-in-properties" (:key-from-properties (read-properties props)))