-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf.go
404 lines (312 loc) · 9.21 KB
/
conf.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package conf
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)
// Available types for loadable config
const (
ConfigTypeYAML = 0
ConfigTypeJSON = 1
)
const (
tagConfName = "conf"
tagConfExtraOptsName = "conf_extraopts"
tagConfRequiredName = "required"
tagConfDefaultName = "default"
)
const (
regexpEnv = "ENV:(.*)"
)
// ConfigType is a loadable config type
type ConfigType int
// Settings struct contains settings config load
type Settings struct {
// ConfPath contains the path to config file
ConfPath string
// ConfType contains config file type (see `ConfigType` constants)
ConfType ConfigType
// WeaklyTypes if true makes "weak" conversions while config file decoding
// (see: https://godoc.org/github.com/mitchellh/mapstructure#DecoderConfig `WeaklyTypedInput` option)
WeaklyTypes bool
// UnknownDeny if true fails with an error if config file contains fields that no matching in the result interface
UnknownDeny bool
md mapstructure.Metadata
}
type defaultValue struct {
value string
isSet bool
}
// Load reads config
func Load(conf interface{}, s Settings) error {
// Check `conf` is a pointer
if reflect.TypeOf(conf).Kind() != reflect.Ptr {
return fmt.Errorf("config load internal error: `conf` must be a pointer")
}
cfgFile, err := ioutil.ReadFile(s.ConfPath)
if err != nil {
return fmt.Errorf("config error: %s", err)
}
rawConf := make(map[string]interface{})
switch s.ConfType {
case ConfigTypeYAML:
if err := yaml.Unmarshal(cfgFile, &rawConf); err != nil {
return fmt.Errorf("config error: %s", err)
}
case ConfigTypeJSON:
if err := json.Unmarshal(cfgFile, &rawConf); err != nil {
return fmt.Errorf("config error: %s", err)
}
default:
return fmt.Errorf("config error: unknown config type")
}
config := &mapstructure.DecoderConfig{
WeaklyTypedInput: s.WeaklyTypes,
Metadata: &s.md,
DecodeHook: s.decodeFromString,
Result: conf,
TagName: tagConfName,
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return fmt.Errorf("config error: %v", err)
}
err = decoder.Decode(rawConf)
if err != nil {
return fmt.Errorf("config error: %v", err)
}
// Set options default values
if err := s.setDefaults(reflect.ValueOf(conf), "", defaultValue{"", false}); err != nil {
return fmt.Errorf("config error: %v", err)
}
if err := s.checkUsedRequredOpts(reflect.ValueOf(conf), ""); err != nil {
return fmt.Errorf("config error: %v", err)
}
if err := s.checkUnknownOpts(); err != nil {
return fmt.Errorf("config error: %v", err)
}
return nil
}
// setDefaults sets the default values from tags.
func (s *Settings) setDefaults(val reflect.Value, parentName string, dv defaultValue) error {
if val.Kind() == reflect.Ptr && val.IsNil() == true {
return nil
}
// Check val is pointer
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
// Check val is writable
if val.CanSet() == false {
return fmt.Errorf("internal error, object is not writable")
}
switch val.Type().Kind() {
case reflect.Struct:
for i := 0; i < val.NumField(); i++ {
vf := val.Field(i)
tf := val.Type().Field(i)
elName := parentName
if elName != "" {
elName = strings.Join([]string{elName, s.fieldNameNormalize(tf)}, ".")
} else {
elName = s.fieldNameNormalize(tf)
}
v, isSet := s.tagValGet(tf.Tag.Get(tagConfExtraOptsName), tagConfDefaultName)
if err := s.setDefaults(vf, elName, defaultValue{v, isSet}); err != nil {
return err
}
}
case reflect.Slice, reflect.Array:
for i := 0; i < val.Len(); i++ {
vf := val.Index(i)
elName := fmt.Sprintf("%s[%d]", parentName, i)
if err := s.setDefaults(vf, elName, defaultValue{"", false}); err != nil {
return err
}
}
case reflect.Map:
for _, k := range val.MapKeys() {
vf := val.MapIndex(k)
// Create copy of element to make it writable
t := reflect.Indirect(reflect.New(vf.Type()))
t.Set(reflect.ValueOf(vf.Interface()))
elName := fmt.Sprintf("%s[%s]", parentName, k)
if err := s.setDefaults(t, elName, defaultValue{"", false}); err != nil {
return err
}
val.SetMapIndex(k, t)
}
default:
// If default value set for this element and this option not used in conf file, fill it with default value
if dv.isSet == true && s.optIsUsed(parentName, s.md.Keys) == false {
d, err := s.convFromString(dv.value, val.Type())
if err != nil {
return err
}
switch val.Type().Kind() {
case reflect.Bool:
val.SetBool(d.(bool))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val.SetInt(d.(int64))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val.SetUint(d.(uint64))
case reflect.Float32, reflect.Float64:
val.SetFloat(d.(float64))
case reflect.String:
val.SetString(d.(string))
default:
return fmt.Errorf("internal error, default value not available for this field type `%s`", parentName)
}
}
}
return nil
}
// checkUsedRequredOpts checks that config file contains all requirement options
func (s *Settings) checkUsedRequredOpts(val reflect.Value, parentName string) error {
if val.Kind() == reflect.Ptr && val.IsNil() == true {
return nil
}
// Check val is pointer
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
switch val.Type().Kind() {
case reflect.Struct:
for i := 0; i < val.NumField(); i++ {
vf := val.Field(i)
tf := val.Type().Field(i)
elName := parentName
if elName != "" {
elName = strings.Join([]string{elName, s.fieldNameNormalize(tf)}, ".")
} else {
elName = s.fieldNameNormalize(tf)
}
tag := tf.Tag.Get(tagConfExtraOptsName)
if s.tagKeyCheck(tag, tagConfRequiredName) == true && s.optIsUsed(elName, s.md.Keys) == false {
return fmt.Errorf("required option '%s' is not specified", elName)
}
if err := s.checkUsedRequredOpts(vf, elName); err != nil {
return err
}
}
case reflect.Slice, reflect.Array:
for i := 0; i < val.Len(); i++ {
vf := val.Index(i)
elName := fmt.Sprintf("%s[%d]", parentName, i)
if err := s.checkUsedRequredOpts(vf, elName); err != nil {
return err
}
}
case reflect.Map:
for _, k := range val.MapKeys() {
vf := val.MapIndex(k)
elName := fmt.Sprintf("%s[%s]", parentName, k)
if err := s.checkUsedRequredOpts(vf, elName); err != nil {
return err
}
}
}
return nil
}
func (s *Settings) checkUnknownOpts() error {
if s.UnknownDeny == true && len(s.md.Unused) > 0 {
return fmt.Errorf("unknown option '%s'", s.md.Unused[0])
}
return nil
}
// decodeFromString decodes values from string to other types.
// Able to use field values in format `ENV:VARIABLE_NAME` to get values from ENV variables.
func (s *Settings) decodeFromString(f reflect.Type, t reflect.Type, v interface{}) (interface{}, error) {
var str string
if f.Kind() != reflect.String {
return v, nil
}
var r = regexp.MustCompile(regexpEnv)
result := r.FindStringSubmatch(v.(string))
if result != nil {
str = os.Getenv(result[1])
if str == "" {
return v, fmt.Errorf("empty ENV variable '%s'", result[1])
}
} else {
str = v.(string)
}
return s.convFromString(str, t)
}
// convFromString converts string value to other type in accordance to `t`
func (s *Settings) convFromString(str string, t reflect.Type) (interface{}, error) {
switch t.Kind() {
case reflect.Bool:
return strconv.ParseBool(str)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.ParseInt(str, 0, t.Bits())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.ParseUint(str, 0, t.Bits())
case reflect.Float32:
return strconv.ParseFloat(str, 32)
case reflect.Float64:
return strconv.ParseFloat(str, 64)
}
return str, nil
}
// fieldNameNormalize returns either name from tag if specified, or struct field name as is
func (s *Settings) fieldNameNormalize(tf reflect.StructField) string {
tag := tf.Tag.Get(tagConfName)
str := s.tagValIndexGet(tag, 0)
if str != "" {
return str
}
return tf.Name
}
// optIsUsed checks that string slice `usedOpts` contains `opt`
func (s *Settings) optIsUsed(opt string, usedOpts []string) bool {
for _, v := range usedOpts {
if v == opt {
return true
}
}
return false
}
// tagPartsMakeMap prepairs map for tag pairs
func (s *Settings) tagPartsMakeMap(tag string) map[string]string {
tm := make(map[string]string)
p := strings.Split(tag, ",")
for _, e := range p {
s := strings.Split(e, "=")
if len(s) > 1 {
tm[strings.Trim(s[0], " \t")] = s[1]
} else {
tm[strings.Trim(s[0], " \t")] = ""
}
}
return tm
}
// tagKeyCheck cheks that `tag` contains `key`
func (s *Settings) tagKeyCheck(tag string, key string) bool {
tm := s.tagPartsMakeMap(tag)
if _, ok := tm[key]; ok {
return true
}
return false
}
// tagValGet gets from `tag` value for `key`
func (s *Settings) tagValGet(tag string, key string) (string, bool) {
tm := s.tagPartsMakeMap(tag)
v, ok := tm[key]
return v, ok
}
// tagConfGetName gets raw value (without splitting by '=') from tag by index
func (s *Settings) tagValIndexGet(tag string, i int) string {
p := strings.Split(tag, ",")
if i >= len(p) {
return ""
}
return p[i]
}