16 func readJsonConfig(filename
string, config
interface{}) error
{
17 fileext
:= path
.Ext(filename
)
18 filename1
:= strings
.TrimSuffix(filename
, fileext
) + ".user" + fileext
20 cm
:= make(map[string]interface{})
21 for i
, name
:= range []string{filename
, filename1
} {
22 f
, err
:= os
.Open(name
)
32 data
, err
:= readJson(f
)
37 data
= bytes
.TrimPrefix(data
, []byte("\xef\xbb\xbf"))
39 cm1
:= make(map[string]interface{})
41 d
:= json
.NewDecoder(bytes
.NewReader(data
))
44 if err
= d
.Decode(&cm1
); err
!= nil {
48 if err
= mergeMap(cm
, cm1
); err
!= nil {
53 data
, err
:= json
.Marshal(cm
)
58 d
:= json
.NewDecoder(bytes
.NewReader(data
))
61 return d
.Decode(config
)
64 func readJson(r io
.Reader
) ([]byte, error
) {
65 s
, err
:= ioutil
.ReadAll(r
)
70 lines
:= make([]string, 0)
71 for _
, line
:= range strings
.Split(strings
.Replace(string(s
), "\r\n", "\n", -1), "\n") {
72 line
= strings
.TrimSpace(line
)
73 if line
== "" || strings
.HasPrefix(line
, "//") {
76 lines
= append(lines
, line
)
80 for i
, line
:= range lines
{
82 nextLine
:= strings
.TrimSpace(lines
[i
+1])
87 if strings
.HasSuffix(line
, ",") {
88 line
= strings
.TrimSuffix(line
, ",")
98 func mergeMap(m1
map[string]interface{}, m2
map[string]interface{}) error
{
99 for key
, value
:= range m2
{
101 m1v
, m1_has_key
:= m1
[key
]
102 m2v
, m2v_is_map
:= value
.(map[string]interface{})
103 m1v1
, m1v_is_map
:= m1v
.(map[string]interface{})
106 case !m1_has_key
, !m2v_is_map
:
109 return fmt
.Errorf("m1v=%#v is not a map, but m2v=%#v is a map", m1v
, m2v
)
118 func randInt(l
, u
int) int {
119 return rand
.Intn(u
-l
) + l
124 func randomHost() string {
125 a
:= make([][]byte, randInt(2, 4))
129 for j
:= 0; j
< m
; j
++ {
130 b
[j
] = byte(randInt(97, 122))
134 return string(bytes
.Join(a
, []byte{46}))