-
Notifications
You must be signed in to change notification settings - Fork 2
/
readjson.go
114 lines (100 loc) · 2.28 KB
/
readjson.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
package goconfig
import (
"encoding/json"
"errors"
"reflect"
"strconv"
"strings"
)
// json 只有2种通用结构
// map[string]interface{} 和 []interface{}
func (c *config) readJson() error {
err := json.Unmarshal(c.Read, &c.sjson)
if err != nil {
return err
}
if err := c.parseJson("", c.sjson); err != nil {
return err
}
return nil
}
func (c *config) parseJson(module string, value map[string]interface{}) error {
for k, v := range value {
// refType := reflect.TypeOf(v)
refValue := reflect.ValueOf(v)
switch refValue.Kind() {
case reflect.Float64, reflect.Float32:
// 如果是整数, 去掉后面的
f := strconv.FormatFloat(refValue.Float(), 'f', 6, 64)
fl := strings.Split(f, ".")
if fl[1] == "000000" {
f = fl[0]
}
if module == "" {
c.KeyValue[k] = f
} else {
c.KeyValue[module+"."+k] = f
}
case reflect.Bool:
//
bs := "false"
b := refValue.Bool()
if b {
bs = "true"
}
if module == "" {
c.KeyValue[k] = bs
} else {
c.KeyValue[module+"."+k] = bs
}
case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
//
if module == "" {
c.KeyValue[k] = strconv.FormatInt(refValue.Int(), 10)
} else {
c.KeyValue[module+"."+k] = strconv.FormatInt(refValue.Int(), 10)
}
case reflect.Uint64, reflect.Uint16, reflect.Uint32, reflect.Uint8:
//
if module == "" {
c.KeyValue[k] = strconv.FormatUint(refValue.Uint(), 10)
} else {
c.KeyValue[module+"."+k] = strconv.FormatUint(refValue.Uint(), 10)
}
case reflect.Map:
//
var next map[string]interface{}
n, _ := json.Marshal(v)
err := json.Unmarshal(n, &next)
if err != nil {
return err
}
if module == "" {
if err := c.parseJson(k, next); err != nil {
return err
}
} else {
if err := c.parseJson(module+"."+k, next); err != nil {
return err
}
}
case reflect.Slice:
//
b, _ := json.Marshal(refValue.Interface())
if module == "" {
c.KeyValue[k] = string(b)
} else {
c.KeyValue[module+"."+k] = string(b)
}
case reflect.String:
if module == "" {
c.KeyValue[k] = refValue.String()
} else {
c.KeyValue[module+"."+k] = refValue.String()
}
default:
return errors.New("not a valid config file")
}
}
return nil
}