From 42e428906e120992f43ae0f91459db55fe9fb483 Mon Sep 17 00:00:00 2001 From: dez Date: Sun, 21 Aug 2016 04:16:05 +0300 Subject: [PATCH 1/4] Added lists support --- src/clojure_ini/core.clj | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/clojure_ini/core.clj b/src/clojure_ini/core.clj index 920cfa2..adc71b4 100644 --- a/src/clojure_ini/core.clj +++ b/src/clojure_ini/core.clj @@ -2,6 +2,23 @@ (:require [clojure.string :as s] [clojure.java.io :as io])) + +(defn- is-list? [kword] + (let [rkw (reverse (name kword))] + (if (and (= (first rkw) \]) (= (second rkw) \[)) + true + false))) + + +(defn strip-braces [kw] + (let [kword (name kw)] + (let [n (.indexOf kword "[")] + (if (not (neg? n)) + (if (keyword? kw) + (keyword (subs kword 0 n)) + (subs kword 0 n)))))) + + (defn- parse-line [s kw trim] (if (= (first s) \[) (-> s (subs 1 (.indexOf s "]")) trim kw) @@ -11,6 +28,7 @@ [(-> s (subs 0 n) trim kw) (-> s (subs (inc n)) trim)])))) + (defn- strip-comment [s chr allow-anywhere?] (let [n (.indexOf s (int chr))] (if (and (not (neg? n)) @@ -19,6 +37,7 @@ (subs s 0 n) s))) + (defn- mapify [coll] (loop [xs coll m {} key nil] (if-let [x (first xs)] @@ -27,14 +46,19 @@ (recur (rest xs) (assoc m (first x) (second x)) key) - (recur (rest xs) - (assoc-in m [key (first x)] (second x)) - key)) + (if (is-list? (first x)) + (recur (rest xs) + (update-in m [key (strip-braces (first x))] conj (second x)) + key) + (recur (rest xs) + (assoc-in m [key (first x)] (second x)) + key))) (recur (rest xs) (assoc m x {}) x)) m))) + (defn read-ini "Read an .ini-file into a Clojure map. @@ -63,3 +87,4 @@ (remove (fn [s] (every? #(Character/isWhitespace %) s))) (map #(parse-line % kw trim)) mapify)))) + From c873ce9535283ced694129e23ce5dee5972dd526 Mon Sep 17 00:00:00 2001 From: dez Date: Sun, 21 Aug 2016 04:31:01 +0300 Subject: [PATCH 2/4] Change visibility for strip-braces function --- src/clojure_ini/core.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clojure_ini/core.clj b/src/clojure_ini/core.clj index adc71b4..b6cad1b 100644 --- a/src/clojure_ini/core.clj +++ b/src/clojure_ini/core.clj @@ -10,7 +10,7 @@ false))) -(defn strip-braces [kw] +(defn- strip-braces [kw] (let [kword (name kw)] (let [n (.indexOf kword "[")] (if (not (neg? n)) From 6f450dd7c533afe2a2f160fb3baba40f79f8001d Mon Sep 17 00:00:00 2001 From: Alex V Gonchar Date: Mon, 22 Aug 2016 04:54:37 -0400 Subject: [PATCH 3/4] Update README and small change of is-list? function --- README.md | 9 ++++++++- src/clojure_ini/core.clj | 4 +--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 32fbc56..d94709b 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,19 @@ Add `[clojure-ini "0.0.2"]` as a dependency to your `project.clj`. server=192.0.2.62 port=143 file = payroll.dat + + [access-allowed] + users[]=mike + users[]=nike + users[]=joe + REPL session: > (use 'clojure-ini.core) > (read-ini "conf.ini" :keywordize? true) - {:database {:file "payroll.dat" + {:access-allowed {:users ("tom" "joe" "nika" "mike")} + :database {:file "payroll.dat" :port "143" :server "192.0.2.62"} :organization "Acme Widgets Inc." diff --git a/src/clojure_ini/core.clj b/src/clojure_ini/core.clj index b6cad1b..8237d81 100644 --- a/src/clojure_ini/core.clj +++ b/src/clojure_ini/core.clj @@ -5,9 +5,7 @@ (defn- is-list? [kword] (let [rkw (reverse (name kword))] - (if (and (= (first rkw) \]) (= (second rkw) \[)) - true - false))) + (and (= (first rkw) \]) (= (second rkw) \[)))) (defn- strip-braces [kw] From d038c8f0325fadb0278bade6ed449c24f14cfcd7 Mon Sep 17 00:00:00 2001 From: Alex V Gonchar Date: Mon, 22 Aug 2016 05:18:02 -0400 Subject: [PATCH 4/4] Update README and small change of is-list? function --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d94709b..7c32ad2 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Add `[clojure-ini "0.0.2"]` as a dependency to your `project.clj`. users[]=mike users[]=nike users[]=joe + users[]=tom REPL session: