Skip to content

Simple property loading, coercing, and validating

License

Notifications You must be signed in to change notification settings

clojusc/propertea

 
 

Repository files navigation

propertea

Build Status Dependencies Status Clojars Project Tag Clojure version

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

Release Clojure Maintainer Notes
1.5.0 1.10.0 clojusc Backwards-compatible function changes; breaking arg changes
1.4.2 1.10.0 clojusc 100% compatible with 1.4.1
1.4.1 1.5.1 Joshua Eckroth Version from which clojusc forked
1.3.1 1.2.0 Jay Fields Version 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.

About

Simple property loading, coercing, and validating

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Clojure 100.0%