Skip to content

Commit

Permalink
LoadJSONConfig()=>new: inline comments
Browse files Browse the repository at this point in the history
  • Loading branch information
panorix committed Jan 22, 2022
1 parent 21fe0b5 commit a2e5fe6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
20 changes: 17 additions & 3 deletions public.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a2e5fe6

Please sign in to comment.