This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
304 lines (271 loc) · 6.12 KB
/
config.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package secretun
import (
"encoding/json"
"fmt"
"os"
"reflect"
"strings"
)
type ConfigError struct {
Errno int
Field string
}
const (
ErrNone = iota
ErrMissing
ErrInvalidType
errMax
)
var errMsgs = []string{
"no error",
"config: missing %s",
"config: %s invalid type",
"config: %s invalid subconfig",
}
func (e *ConfigError) Error() string {
if e.Errno >= errMax || e.Errno < 0 {
return "invalid ConfigError"
}
return fmt.Sprintf(errMsgs[e.Errno], e.Field)
}
func NewConfigError(errno int, field string) *ConfigError {
e := ConfigError{}
e.Errno = errno
e.Field = field
return &e
}
type ConvertFunc func(interface{}, reflect.Value) *ConfigError
type ConvertFuncs []ConvertFunc
func (c ConvertFuncs) get(val reflect.Value) ConvertFunc {
if _, ok := val.Interface().(Config); ok {
return convertConfig
}
kind := val.Kind()
if kind > reflect.Kind(len(c)) {
return nil
}
return c[kind]
}
var convertFuncs ConvertFuncs
func init() {
convertFuncs = []ConvertFunc{nil,
convertBool,
convertInt,
nil, //convertInt8,
nil, //convertInt16,
nil, //convertInt32,
nil, //convertInt64,
convertUint,
nil, //convertUint8,
nil, //convertUint16,
nil, //convertUint32,
nil, //convertUint64,
nil, //convertUintptr,
convertFloat32,
convertFloat64,
nil, //convertComplex64,
nil, //convertComplex128,
nil, //convertArray,
nil, //convertChan,
nil, //convertFunc,
nil, //convertInterface,
nil, //convertMap,
nil, //convertPtr,
convertSlice,
convertString,
convertStruct}
}
type Config struct {
Map map[string]interface{}
Name string
}
func ConfigFromJson(path string) (cfg Config, err error) {
cfg.Name = ""
var f *os.File
if f, err = os.Open(path); err != nil {
return
} else {
defer f.Close()
decoder := json.NewDecoder(f)
if err = decoder.Decode(&cfg.Map); err != nil {
return
}
}
return
}
func (c *Config) GetConfig(name string) (cfg Config, err error) {
cfg.Name = fmt.Sprintf("%s.%s", c.Name, name)
if icfg, ok := c.Map[name]; !ok {
err = NewConfigError(ErrMissing, cfg.Name)
return
} else if c, ok := icfg.(map[string]interface{}); !ok {
err = NewConfigError(ErrInvalidType, cfg.Name)
return
} else {
cfg.Map = c
}
return
}
func (c *Config) Has(name string) bool {
_, ok := c.Map[name]
return ok
}
func (c *Config) Get(name string, dest interface{}) error {
if obj, ok := c.Map[name]; !ok {
return NewConfigError(ErrMissing, c.Name+"."+name)
} else {
val := reflect.ValueOf(dest).Elem()
if convertor := convertFuncs.get(val); convertor == nil {
return NewConfigError(ErrInvalidType, c.Name+"."+name)
} else if err := convertor(obj, val); err != nil {
if len(err.Field) > 0 {
err.Field = fmt.Sprintf("%s.%s.%s", c.Name, name, err.Field)
} else {
err.Field = fmt.Sprintf("%s.%s", c.Name, name)
}
return err
}
}
return nil
}
func (c *Config) GetBool(name string) bool {
var b bool
if err := c.Get(name, &b); err != nil {
return false
}
return b
}
func convertBool(in interface{}, val reflect.Value) *ConfigError {
if b, ok := in.(bool); !ok {
return &ConfigError{ErrInvalidType, ""}
} else {
val.SetBool(b)
}
return nil
}
func convertInt(in interface{}, val reflect.Value) *ConfigError {
switch v := in.(type) {
case int:
val.SetInt(int64(v))
case int64:
val.SetInt(int64(v))
case float32:
val.SetInt(int64(v))
case float64:
val.SetInt(int64(v))
default:
return &ConfigError{ErrInvalidType, ""}
}
return nil
}
func convertUint(in interface{}, val reflect.Value) *ConfigError {
switch v := in.(type) {
case int:
val.SetUint(uint64(v))
case int64:
val.SetUint(uint64(v))
case float32:
val.SetUint(uint64(v))
case float64:
val.SetUint(uint64(v))
default:
return &ConfigError{ErrInvalidType, ""}
}
return nil
}
func convertFloat32(in interface{}, val reflect.Value) *ConfigError {
switch v := in.(type) {
case int:
val.SetFloat(float64(v))
case int64:
val.SetFloat(float64(v))
case float32:
val.SetFloat(float64(v))
case float64:
val.SetFloat(float64(v))
default:
return &ConfigError{ErrInvalidType, ""}
}
return nil
}
func convertFloat64(in interface{}, val reflect.Value) *ConfigError {
switch v := in.(type) {
case int:
val.SetFloat(float64(v))
case int64:
val.SetFloat(float64(v))
case float32:
val.SetFloat(float64(v))
case float64:
val.SetFloat(float64(v))
default:
return &ConfigError{ErrInvalidType, ""}
}
return nil
}
func convertSlice(in interface{}, val reflect.Value) *ConfigError {
if iary, ok := in.([]interface{}); !ok {
return &ConfigError{ErrInvalidType, ""}
} else if len(iary) > 0 {
var convertor ConvertFunc
ele := reflect.MakeSlice(val.Type(), 1, 1).Index(0)
convertor = convertFuncs.get(ele)
if convertor == nil {
return &ConfigError{ErrInvalidType, ""}
}
ary := reflect.MakeSlice(val.Type(), len(iary), len(iary))
for i, ele := range iary {
if err := convertor(ele, ary.Index(i)); err != nil {
if len(err.Field) > 0 {
err.Field = fmt.Sprintf("[%d].%s", i, err.Field)
} else {
err.Field = fmt.Sprintf("[%d]", i)
}
return err
}
}
val.Set(ary)
}
return nil
}
func convertString(in interface{}, val reflect.Value) *ConfigError {
if str, ok := in.(string); !ok {
return &ConfigError{ErrInvalidType, ""}
} else {
val.SetString(str)
}
return nil
}
func convertStruct(in interface{}, val reflect.Value) *ConfigError {
dict, ok := in.(map[string]interface{})
if !ok || len(dict) != val.NumField() {
return &ConfigError{ErrInvalidType, ""}
}
for k, from := range dict {
to := val.FieldByName(strings.ToTitle(k[:1]) + k[1:])
if !to.IsValid() {
return &ConfigError{ErrInvalidType, ""}
}
convertor := convertFuncs.get(to)
if convertor == nil {
return &ConfigError{ErrInvalidType, ""}
}
if err := convertor(from, to); err != nil {
if len(err.Field) > 0 {
err.Field = fmt.Sprintf("%s.%s", k, err.Field)
} else {
err.Field = k
}
return err
}
}
return nil
}
func convertConfig(in interface{}, val reflect.Value) *ConfigError {
if m, ok := in.(map[string]interface{}); !ok {
return &ConfigError{ErrInvalidType, ""}
} else {
val.Set(reflect.ValueOf(Config{m, ""}))
}
return nil
}