-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaml.go
189 lines (168 loc) · 3.82 KB
/
yaml.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// package gt
package gt
import (
"errors"
"fmt"
"github.com/go-yaml/yaml"
"io/ioutil"
"log"
"reflect"
"strconv"
"strings"
"unicode"
)
// go tool yaml
// use go-yaml
type Yaml struct {
// yaml data
data map[interface{}]interface{}
}
// load the default app.yaml data
func (c *Yaml) loadYaml(path string) error {
yamlS, readErr := ioutil.ReadFile(path)
if readErr != nil {
return readErr
}
// yaml解析的时候c.data如果没有被初始化,会自动为你做初始化
err := yaml.Unmarshal(yamlS, &c.data)
if err != nil {
return errors.New("can not parse " + path + " config")
}
return nil
}
// 从配置文件中获取值
func (c *Yaml) Get(name string) interface{} {
path := strings.Split(name, ".")
data := c.data
for key, value := range path {
v, ok := data[value]
if !ok {
break
}
if (key + 1) == len(path) {
return v
}
// print yaml v3 problem!
// use go-yaml v2 replace
//log.Println(name, "&&",reflect.TypeOf(v).String())
if reflect.TypeOf(v).String() == "map[interface {}]interface {}" {
data = v.(map[interface{}]interface{})
}
}
return nil
}
// string
func (c *Yaml) GetString(name string) string {
value := c.Get(name)
switch value := value.(type) {
case string:
return value
case bool, float64, int:
return fmt.Sprint(value)
default:
return ""
}
}
// int
func (c *Yaml) GetInt(name string) int {
value := c.Get(name)
switch value := value.(type) {
case string:
i, err := strconv.Atoi(value)
log.Println("[YAML type error]: ", err)
return i
case int:
return value
case bool:
if value {
return 1
}
return 0
case float64:
return int(value)
default:
return 0
}
}
// bool
func (c *Yaml) GetBool(name string) bool {
value := c.Get(name)
switch value := value.(type) {
case string:
str, _ := strconv.ParseBool(value)
return str
case int:
if value != 0 {
return true
}
return false
case bool:
return value
case float64:
if value != 0.0 {
return true
}
return false
default:
return false
}
}
// 从配置文件中获取Struct类型的值
// 这里的struct是你自己定义的根据配置文件
func (c *Yaml) GetStruct(name string, s interface{}) {
d := c.Get(name)
switch d.(type) {
case string:
_ = c.setField(s, name, d)
case map[interface{}]interface{}:
c.mapToStruct(d.(map[interface{}]interface{}), s)
}
}
func (c *Yaml) mapToStruct(m map[interface{}]interface{}, s interface{}) interface{} {
for key, value := range m {
switch key.(type) {
case string:
_ = c.setField(s, key.(string), value)
}
}
return s
}
func (c *Yaml) setField(s interface{}, name string, value interface{}) error {
for i, v := range name {
name = string(unicode.ToUpper(v)) + name[i+1:]
break
}
// reflect.Indirect 返回value对应的值
structValue := reflect.Indirect(reflect.ValueOf(s))
structFieldValue := structValue.FieldByName(name)
// isValid 显示的测试一个空指针
if !structFieldValue.IsValid() {
return errors.New("No such field: " + name)
}
// CanSet判断值是否可以被更改
if !structFieldValue.CanSet() {
return errors.New("Cannot set field value" + name)
}
// 获取要更改值的类型
structFieldType := structFieldValue.Type()
val := reflect.ValueOf(value)
if structFieldType.Kind() == reflect.Struct && val.Kind() == reflect.Map {
vint := val.Interface()
switch vint.(type) {
case map[interface{}]interface{}:
for key, value := range vint.(map[interface{}]interface{}) {
_ = c.setField(structFieldValue.Addr().Interface(), key.(string), value)
}
case map[string]interface{}:
for key, value := range vint.(map[string]interface{}) {
_ = c.setField(structFieldValue.Addr().Interface(), key, value)
}
}
} else {
if structFieldType != val.Type() {
return errors.New("provided value type didn't match field type")
}
structFieldValue.Set(val)
}
return nil
}