From f25c34a05a2fdd3787ad65788e32e54eb581633a Mon Sep 17 00:00:00 2001 From: MelKori Date: Wed, 10 Aug 2022 23:42:20 +0300 Subject: [PATCH] cover specific case of quoted inner strings at properties keys --- project.clj | 2 +- src/java_properties/core.clj | 47 ++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/project.clj b/project.clj index 547c561..268b8c2 100644 --- a/project.clj +++ b/project.clj @@ -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" diff --git a/src/java_properties/core.clj b/src/java_properties/core.clj index 43d988b..c57281e 100644 --- a/src/java_properties/core.clj +++ b/src/java_properties/core.clj @@ -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) @@ -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))