-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_util.go
94 lines (81 loc) · 2.51 KB
/
config_util.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package viperutil
import (
"encoding/json"
//"github.com/hyperledger/fabric/common/flogging"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
//var logger = flogging.MustGetLogger("viperutil")
type viperGetter func(key string) interface{}
func getKeysRecursively(base string, getKey viperGetter, nodeKeys map[string]interface{}) map[string]interface{} {
result := make(map[string]interface{})
for key := range nodeKeys {
fqKey := base + key
val := getKey(fqKey)
if m, ok := val.(map[interface{}]interface{}); ok {
//logger.Debugf("Found map[interface{}]interface{} value for %s", fqKey)
tmp := make(map[string]interface{})
for ik, iv := range m {
cik, ok := ik.(string)
if !ok {
panic("Non string key-entry")
}
tmp[cik] = iv
}
result[key] = getKeysRecursively(fqKey+".", getKey, tmp)
} else if m, ok := val.(map[string]interface{}); ok {
//logger.Debugf("Found map[string]interface{} value for %s", fqKey)
result[key] = getKeysRecursively(fqKey+".", getKey, m)
} else if m, ok := unmarshalJSON(val); ok {
//logger.Debugf("Found real value for %s setting to map[string]string %v", fqKey, m)
result[key] = m
} else {
if val == nil {
fileSubKey := fqKey + ".File"
fileVal := getKey(fileSubKey)
if fileVal != nil {
result[key] = map[string]interface{}{"File": fileVal}
continue
}
}
//logger.Debugf("Found real value for %s setting to %T %v", fqKey, val, val)
result[key] = val
}
}
return result
}
func unmarshalJSON(val interface{}) (map[string]string, bool) {
mp := map[string]string{}
s, ok := val.(string)
if !ok {
//logger.Debugf("Unmarshal JSON: value is not a string: %v", val)
return nil, false
}
err := json.Unmarshal([]byte(s), &mp)
if err != nil {
//logger.Debugf("Unmarshal JSON: value cannot be unmarshalled: %s", err)
return nil, false
}
return mp, true
}
// EnhancedExactUnmarshalKey is intended to unmarshal a config file subtreee into a structure
func EnhancedExactUnmarshalKey(baseKey string, output interface{}, conf *viper.Viper) error {
m := make(map[string]interface{})
m[baseKey] = nil
leafKeys := getKeysRecursively("", conf.Get, m)
//logger.Debugf("%+v", leafKeys)
config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
WeaklyTypedInput: true,
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(leafKeys[baseKey])
}