Liking cljdoc? Tell your friends :D

propertea

[Build Status]travis [Dependencies Status]deps [Clojars Project]clojars [Tag]tag Clojure version

[]logo-large

Simple property loading, coercing, and validating

Contents

Usage

Reading Property Files and Streams

propertea can be used to load a property file, convert, and validate. The following snippet shows loading a file and converting a few of the properties to their desired types.

Given the properties file test/fake.properties with the following contents:

string-example=hello-string
int-example=1
boolean-example=true
empty-string=
nested.example.depth=5
nested.example.leaves=2
nested.withCamelCase=get-dashed
(require '[propertea.core :as prop])

(prop/read "test/fake.properties"
           :as-int [:int-example]
           :as-bool [:boolean-example])
{: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:

(prop/read
  (.getResourceAsStream
    (.getContextClassLoader (Thread/currentThread))
    "some.properties"))

or

(require '[clojure.java.io :as io])

(-> (io/resource "some.properties")
    (io/file)
    (io/input-stream)
    (prop/read))

propertea can also validate that required properties are specified. Assuming the same properties file as above:

(prop/read "test/fake.properties" :required [:req-prop]))
java.lang.RuntimeException: (:req-prop) are required, but not found

Maps <-> Properties

Some Java libraries expect data (often configuration data) to be in the form of java.util.Properties. propertea makes this a sane prospect when interoping with such libraries:

(def m2p (prop/map->props {:thing1 "one" :thing2 "two"}))
m2p
{"thing2" "two", "thing1" "one"}
(type m2p)
java.util.Properties

You can, of course, take this in the other direction:

(prop/props->map m2p)
{"thing2" "two", "thing1" "one"}

As you can see, by default that returns hash-maps with keys as strings. If you pass a function in addition to the data, that function will be applied to each keyword:

(prop/props->map m2p keyword)
{:thing2 "two", :thing1 "one"}

Installing

The easiest way is via Leiningen. Add the following dependency to your project.clj file:

[clojusc/propertea "1.5.0"]

To build from source and install locally, run the following commands:

$ lein deps
$ lein jar
$ lein install

Version History

ReleaseClojureMaintainerNotes
1.5.01.10.0clojuscBackwards-compatible function changes; breaking arg changes
1.4.21.10.0clojusc100% compatible with 1.4.1
1.4.11.5.1Joshua EckrothVersion from which clojusc forked
1.3.11.2.0Jay FieldsVersion from which Josh forked

License

BSD 3-Clause License

Copyright © 2010-2012, Jay Fields

Copyright © 2013-2014, Joshua Eckroth

Copyright © 2019, Clojure-Aided Enrichment Center

All rights reserved.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close