Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fern-flower-lab/java-properties

Java properties files micro parser for Clojure
https://github.com/fern-flower-lab/java-properties

clj clojure clojure-library compatibility configuration edn java java-properties parser properties-parser tiny

Last synced: about 15 hours ago
JSON representation

Java properties files micro parser for Clojure

Awesome Lists containing this project

README

        

= Java Properties

image:https://img.shields.io/github/license/fern-flower-lab/java-properties?style=for-the-badge[GitHub]
image:https://img.shields.io/clojars/v/ai.z7/java-properties.svg?style=for-the-badge[]
image:https://img.shields.io/github/v/tag/fern-flower-lab/java-properties?style=for-the-badge[GitHub tag (latest by date)]
image:https://img.shields.io/github/last-commit/fern-flower-lab/java-properties?style=for-the-badge[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:

[source, 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

[source, clojure]
----
(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:

[source, bash]
----
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:

[source, java]
----
(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
[source, clojure]
----
(-> 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
[source, clojure]
----
(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
[source, clojure]
----
(jconf/split-comma-separated "a,b,c , d,e, f g,h")
; ("a" "b" "c" "d" "e" "f g" "h")
----

.Un-kebab
[source, clojure]
----
(jconf/kebab-conf-to-camelcase {:a-getter-b {:foo "bar"}})
{:aGetterB {:foo "bar"}}
----

== License

© 2022-2023 Fern Flower Lab

Distributed under the MIT License.