-
Notifications
You must be signed in to change notification settings - Fork 4
/
clconf.go
300 lines (270 loc) · 7 KB
/
clconf.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
// Package core provides functions to extract values from a set of yaml
// files after merging them.
package core
import (
"fmt"
"os"
"path"
"reflect"
"strconv"
"strings"
"dario.cat/mergo"
"github.com/mitchellh/mapstructure"
"github.com/pastdev/clconf/v3/pkg/yamljson"
)
// Fill will fill a according to DecoderConfig with the values from conf.
func Fill(keyPath string, conf interface{}, decoderConfig *mapstructure.DecoderConfig) error {
value, err := GetValue(conf, keyPath)
if err != nil {
return err
}
decoder, err := mapstructure.NewDecoder(decoderConfig)
if err != nil {
return fmt.Errorf("create decoder: %w", err)
}
err = decoder.Decode(value)
if err != nil {
return fmt.Errorf("decode: %w", err)
}
return nil
}
// FillValue will fill a struct, out, with values from conf.
func FillValue(keyPath string, conf interface{}, out interface{}) bool {
err := Fill(keyPath, conf, &mapstructure.DecoderConfig{Result: out})
return err == nil
}
// GetValue returns the value at the indicated path. Paths are separated by
// the '/' character. The empty string or "/" will return conf itself.
func GetValue(conf interface{}, keyPath string) (interface{}, error) {
if keyPath == "" || keyPath == "/" {
return conf, nil
}
var value = conf
currentPath := "/"
for _, part := range strings.Split(keyPath, "/") {
if part == "" {
continue
}
currentPath = path.Join(currentPath, part)
switch typed := value.(type) {
case map[string]interface{}:
// json deserialized
var ok bool
value, ok = typed[part]
if !ok {
return nil, fmt.Errorf(
"value at [%v] does not exist",
currentPath)
}
case map[interface{}]interface{}:
// yaml deserialized
var ok bool
value, ok = typed[part]
if ok {
continue
}
intKey, err := strconv.ParseInt(part, 10, 64)
if err == nil {
value, ok = typed[int(intKey)]
if ok {
continue
}
value, ok = typed[int8(intKey)]
if ok {
continue
}
value, ok = typed[int16(intKey)]
if ok {
continue
}
value, ok = typed[int32(intKey)]
if ok {
continue
}
value, ok = typed[int64(intKey)]
if ok {
continue
}
}
boolKey, err := strconv.ParseBool(part)
if err == nil {
value, ok = typed[boolKey]
if ok {
continue
}
}
return nil, fmt.Errorf(
"value at [%v] does not exist",
currentPath)
case []interface{}:
i, err := strconv.Atoi(part)
if err != nil {
return nil, fmt.Errorf(
"value at [%v] is array, but index [%v] is not int: %w",
path.Dir(currentPath),
part,
err)
}
value = typed[i]
default:
return nil, fmt.Errorf(
"value at [%v] not a map or slice: %v",
part,
reflect.ValueOf(value).Kind())
}
}
return value, nil
}
// ListToMap converts a list to an integer map.
func ListToMap(l []interface{}) map[interface{}]interface{} {
m := make(map[interface{}]interface{})
for i, v := range l {
m[i] = v
}
return m
}
func splitKeyPath(keyPath string) ([]string, string) {
if keyPath == "" || keyPath == "/" {
return []string{}, ""
}
parts := []string{}
for _, parentPart := range strings.Split(keyPath, "/") {
if parentPart == "" {
continue
}
parts = append(parts, parentPart)
}
lastIndex := len(parts) - 1
if lastIndex >= 0 {
return parts[:lastIndex], parts[lastIndex]
}
return parts, keyPath
}
// MergeValue will merge the values from value into config at keyPath.
// If overwrite is true, values from value will overwrite existing
// values in config.
func MergeValue(config interface{}, keyPath string, value interface{}, overwrite bool) error {
parent, key, err := getParentAndKey(config, keyPath)
if err != nil {
return err
}
// ensure the root element is always a map for the merge library
if key == "" {
parent = map[interface{}]interface{}{key: parent}
value = map[interface{}]interface{}{key: value}
} else {
parent = map[interface{}]interface{}{key: parent[key]}
value = map[interface{}]interface{}{key: value}
}
err = mergo.Merge(
&parent,
value,
func(c *mergo.Config) { c.Overwrite = overwrite })
if err != nil {
return fmt.Errorf("merge: %w", err)
}
return nil
}
func getParentAndKey(config interface{}, keyPath string) (map[interface{}]interface{}, string, error) {
configMap, ok := config.(map[interface{}]interface{})
if !ok {
return nil, "", fmt.Errorf("config not a map")
}
parentParts, key := splitKeyPath(keyPath)
if key == "" {
return configMap, "", nil
}
parent := configMap
for i, parentPart := range parentParts {
parentValue, ok := parent[parentPart]
if !ok || parentValue == nil {
parentValue = make(map[interface{}]interface{})
parent[parentPart] = parentValue
}
valueMap, ok := parentValue.(map[interface{}]interface{})
if !ok {
return nil, "", fmt.Errorf(
"parent at /%s not a map (type: %T)",
strings.Join(parentParts[0:i+1], "/"),
parentValue)
}
parent = valueMap
}
return parent, key, nil
}
// SetValue will set the value of config at keyPath to value.
func SetValue(config interface{}, keyPath string, value interface{}) error {
parent, key, err := getParentAndKey(config, keyPath)
if err != nil {
return err
}
if key == "" {
valueMap, ok := value.(map[interface{}]interface{})
if !ok {
return fmt.Errorf("if replacing root, value must be a map")
}
for k := range parent {
delete(parent, k)
}
for k := range valueMap {
parent[k] = valueMap[k]
}
return nil
}
parent[key] = value
return nil
}
// SaveConf will save config to file as yaml
func SaveConf(config interface{}, file string) error {
yamlBytes, err := yamljson.MarshalYaml(config)
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
err = os.WriteFile(file, yamlBytes, 0660) //nolint:gosec
if err != nil {
return fmt.Errorf("write: %w", err)
}
return nil
}
// ToKvMap will return a one-level map of key value pairs where the key is
// a / separated path of subkeys.
func ToKvMap(conf interface{}) map[string]string {
kvMap := make(map[string]string)
Walk(func(keyStack []string, value interface{}) {
key := "/" + strings.Join(keyStack, "/")
if value == nil {
kvMap[key] = ""
} else {
kvMap[key] = fmt.Sprintf("%v", value)
}
}, conf)
return kvMap
}
// Walk will recursively iterate over all the nodes of conf calling callback
// for each node.
func Walk(callback func(key []string, value interface{}), conf interface{}) {
walk(callback, conf, []string{})
}
func walk(callback func(key []string, value interface{}), node interface{}, keyStack []string) {
switch typed := node.(type) {
case map[string]interface{}:
// json deserialized
for k, v := range typed {
keyStack := append(keyStack, fmt.Sprintf("%v", k))
walk(callback, v, keyStack)
}
case map[interface{}]interface{}:
// yaml deserialized
for k, v := range typed {
keyStack := append(keyStack, fmt.Sprintf("%v", k))
walk(callback, v, keyStack)
}
case []interface{}:
for i, j := range typed {
keyStack := append(keyStack, strconv.Itoa(i))
walk(callback, j, keyStack)
}
default:
callback(keyStack, node)
}
}