Skip to content

Commit

Permalink
cover specific case of quoted inner strings at properties keys
Browse files Browse the repository at this point in the history
  • Loading branch information
source-c committed Aug 10, 2022
1 parent bd5cf3e commit f25c34a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject ai.z7/java-properties "1.0.2"
(defproject ai.z7/java-properties "1.1.0"
:description "Java properties files micro parser for Clojure"
:url "https://github.com/fern-flower-lab/java-properties"
:license {:name "MIT"
Expand Down
47 changes: 39 additions & 8 deletions src/java_properties/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@
sort
(map #(-> (get d %)
(assoc :key %)))))
(defn hex [ba]
(->> (map #(format "%02x" %) ba)
(apply str)))


(defn unhex [s]
(->> (partition 2 s)
(map #(Integer/parseInt (apply str %) 16))
byte-array))

(def ^:private pairs (atom {}))

(defn extract-strings [line]
(let [strings (re-seq #"\"[^\"]+\"" line)]
(if (empty? strings)
line
(loop [li line st strings]
(let [term (first st)
hashed (some-> term .getBytes hex)]
(swap! pairs assoc hashed (some-> term (s/replace "\"" "") keyword))
(if (empty? st) li
(recur (s/replace li term hashed) (rest st))))))))

(declare group-config)

Expand Down Expand Up @@ -125,15 +147,24 @@

val))

(defn- swap-pair* [k]
(get @pairs (name k) k))

(defn- restore-keys [d]
(map-keys swap-pair* d))

(defn- group-config [d & [{with-arrays :with-arrays}]]
(cond->> d
(cond->> (map-keys extract-strings d)
true compile-dict
with-arrays convert-arrays))
with-arrays convert-arrays
(not-empty @pairs) restore-keys))

(defn load-config [app-name & [{:keys [config] :as options}]]
(group-config
(merge
(some-> app-name (str ".properties") io/resource load-props)
(when config
(some-> config io/file load-props)))
options))
(let [conf-dict (group-config
(merge
(some-> app-name (str ".properties") io/resource load-props)
(when config
(some-> config io/file load-props)))
options)]
(reset! pairs {})
conf-dict))

0 comments on commit f25c34a

Please sign in to comment.