Skip to content

Java properties files micro parser for Clojure

License

Notifications You must be signed in to change notification settings

fern-flower-lab/java-properties

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Properties

GitHub java properties GitHub tag (latest by date) GitHub last commit

Objectives

This reader for Clojure is a small but powerful library that makes possible to use a regular .properties configurations that familiar to every Java programmer in any Clojure project. No more headaches even if you need to import the configurations from other (not Clojure) project.

The library has no dependencies and written in pure Clojure.

In contrary to other libraries it not delivers any specific typings or other sugar.

Mainly intended to hybrid projects, migrations and for use by pure-Java integrations.

Usage

Writing the config

The config file is a well known dead simple Java properties:

core.journal.port = 26333
core.journal.host = 4.3.2.1

backend.0.name="string"
backend.0.type="another string"
backend.0.enabled=true

backend.2.name="weak-connection-api"
backend.2.type="http"
backend.2.method="post"

front.2.0.mode=my-symbol-1
front.2.1.type=my-symbol-2
front.4.2.mode=my-symbol-3
front.4.2.type=1

tomato.0 = "vvv"
tomato.1 = "zzz"

service[0]="foo"
service[1].bar = "bar"
service[2][1]= "baz"
service[2][2].zoo = 11
service[4][3]= true

foo[0].maa[0] = 1
foo[0].maa[1] = 2

service."my.package.path.bean.group.threadCount"=12

Reading the config

(require '[java-properties.core :as jconf])
;; nil
(def conf-simple (jconf/load-config "test"))
;; conf-simple
;; {:backend {:0 {:enabled true, :bootstrap-servers "127.0.0.1:9092", :type "kafka", :name "test"}, :2 {:type "http", :name "weak-connection-api", :method "post"}}, :front {:2 {:0 {:mode some}, :1 {:type ttt}}, :4 {:2 {:type 1, :mode zzz}}}, :core {:test-list a, :journal {:port 26333}}}
(def conf-full (jconf/load-config "test" {:with-arrays true}))
;; conf-full
;; {:backend [{:enabled true, :bootstrap-servers "127.0.0.1:9092", :type "kafka", :name "test"} nil {:type "http", :name "weak-connection-api", :method "post"}], :front [nil nil [{:mode some} {:type ttt}] nil [nil nil {:type 1, :mode zzz}]], :core {:test-list a, :journal {:port 26333}}}
(slurp "ext.properties")
;; "external.property.name = foo"
(def conf-full-override (jconf/load-config "test" {:with-arrays true :config "./ext.properties"}))
;; conf-full-override
;; {:backend [{:enabled true, :bootstrap-servers "127.0.0.1:9092", :type "kafka", :name "test"} nil {:type "http", :name "weak-connection-api", :method "post"}], :front [nil nil [{:mode some} {:type ttt}] nil [nil nil {:type 1, :mode zzz}]], :core {:test-list a, :journal {:port 26333}}, :external {:property {:name foo}}}

Overriding values

The library supports regular overrides via commandline, like:

java -Dapi.port=12345 -jar /path/to/your/compiled.jar

i.e. -Dapi.port=12345 is stands to override properies file defined api.port=98765 pair. The same result can be achieved by classical call to Java’s setProperty method upon parsing the configuration:

(System/setProperty "api.port" "12345")

For more sofistical example, see test cases in the main repo.

Helpers

The library also contains some handy methods-helpers

Pretty formatting
(-> conf-full jconf/pretty println)
;; {:backend
;;  [{:enabled true,
;;    :bootstrap-servers "127.0.0.1:9092",
;;    :type "kafka",
;;    :name "test"}
;;   nil
;;   {:type "http", :name "weak-connection-api", :method "post"}],
;;  :front
;;  [nil
;;   nil
;;   [{:mode some} {:type ttt}]
;;   nil
;;   [nil nil {:type 1, :mode zzz}]],
;;  :core {:test-list a, :journal {:port 26333}}}
;nil
Reading date-time into Java
(jconf/parse-java-util-date "2012-11-10 09:08:07.006Z")
; #inst "2012-11-10T09:08:07.006-00:00"
Parse comma-separated lists
(jconf/split-comma-separated "a,b,c  , d,e, f g,h")
; ("a" "b" "c" "d" "e" "f g" "h")
Un-kebab
(jconf/kebab-conf-to-camelcase {:a-getter-b {:foo "bar"}})
{:aGetterB {:foo "bar"}}

License

© 2022-2023 Fern Flower Lab

Distributed under the MIT License.