-
Notifications
You must be signed in to change notification settings - Fork 81
/
util.go
115 lines (95 loc) · 2.24 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
)
// Gop
func readJsonConfig(filename string, config interface{}) error {
fileext := path.Ext(filename)
filename1 := strings.TrimSuffix(filename, fileext) + ".user" + fileext
cm := make(map[string]interface{})
for i, name := range []string{filename, filename1} {
f, err := os.Open(name)
if err != nil {
if i == 0 {
return err
} else {
continue
}
}
defer f.Close()
data, err := readJson(f)
if err != nil {
return err
}
data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))
cm1 := make(map[string]interface{})
d := json.NewDecoder(bytes.NewReader(data))
d.UseNumber()
if err = d.Decode(&cm1); err != nil {
return err
}
if err = mergeMap(cm, cm1); err != nil {
return err
}
}
data, err := json.Marshal(cm)
if err != nil {
return err
}
d := json.NewDecoder(bytes.NewReader(data))
d.UseNumber()
return d.Decode(config)
}
func readJson(r io.Reader) ([]byte, error) {
s, err := ioutil.ReadAll(r)
if err != nil {
return s, err
}
lines := make([]string, 0)
for _, line := range strings.Split(strings.Replace(string(s), "\r\n", "\n", -1), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "//") {
continue
}
lines = append(lines, line)
}
var b bytes.Buffer
for i, line := range lines {
if i < len(lines)-1 {
nextLine := strings.TrimSpace(lines[i+1])
if nextLine == "]" ||
nextLine == "]," ||
nextLine == "}" ||
nextLine == "}," {
if strings.HasSuffix(line, ",") {
line = strings.TrimSuffix(line, ",")
}
}
}
b.WriteString(line)
}
return b.Bytes(), nil
}
func mergeMap(m1 map[string]interface{}, m2 map[string]interface{}) error {
for key, value := range m2 {
m1v, m1_has_key := m1[key]
m2v, m2v_is_map := value.(map[string]interface{})
m1v1, m1v_is_map := m1v.(map[string]interface{})
switch {
case !m1_has_key, !m2v_is_map:
m1[key] = value
case !m1v_is_map:
return fmt.Errorf("m1v=%#v is not a map, but m2v=%#v is a map", m1v, m2v)
default:
mergeMap(m1v1, m2v)
}
}
return nil
}