From a2e5fe6161ae6ec2ad3bfc982de9292177dceaf7 Mon Sep 17 00:00:00 2001 From: Kamiar Bahri Date: Sat, 22 Jan 2022 06:23:54 +0000 Subject: [PATCH] LoadJSONConfig()=>new: inline comments --- README.md | 2 +- public.go | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f17d32d..ee36f3c 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ func LoadJSONConfig(path string) (map[string]interface{}, []byte) # notes for this key "some-array" : [ - "value 1", + "value 1", # inline comment will also be omitted /* more notes */ "value 2" ] diff --git a/public.go b/public.go index a56a1e6..357b1d0 100644 --- a/public.go +++ b/public.go @@ -363,15 +363,29 @@ func LoadJSONConfig(path string) (map[string]interface{}, []byte) { str := string(b) lines := strings.Split(str, "\n") + var lines2 []string + for i := 0; i < len(lines); i++ { - s := strings.ReplaceAll(lines[i], "\t", "") - s = strings.ReplaceAll(s, "\n", "") + s := lines[i] + s = strings.Trim(s, " ") + s = strings.ReplaceAll(lines[i], "\t", "") + if s == "" || strings.HasPrefix(s, "#") { continue } - jsonStrArry = fmt.Sprintf("%s%s", jsonStrArry, s) + + // take out the inline comments + v := strings.Split(s, "#") + if len(v) > 1 { + s = v[0] + } + + lines2 = append(lines2, s) } + + jsonStrArry = strings.Join(lines2, "") + jsonStrArry = RemovePhraseFromString(jsonStrArry, "/*", "*/") b = []byte(jsonStrArry)