forked from uber-go/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml.go
421 lines (357 loc) · 10.4 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package config
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
"github.com/pkg/errors"
"golang.org/x/text/transform"
"gopkg.in/yaml.v2"
)
type yamlConfigProvider struct {
root yamlNode
}
var (
_envSeparator = ":"
_emptyDefault = `""`
)
func newYAMLProviderCore(files ...io.Reader) (*yamlConfigProvider, error) {
var root interface{}
for _, v := range files {
var curr interface{}
if err := unmarshalYAMLValue(v, &curr); err != nil {
if file, ok := v.(*os.File); ok {
return nil, errors.Wrapf(err, "in file: %q", file.Name())
}
return nil, err
}
tmp, err := mergeMaps(root, curr)
if err != nil {
return nil, err
}
root = tmp
}
return &yamlConfigProvider{
root: yamlNode{
nodeType: getNodeType(root),
key: Root,
value: root,
},
}, nil
}
// We need to have a custom merge map because yamlV2 doesn't unmarshal
// `map[interface{}]map[interface{}]interface{}` as we expect: it will
// replace second level maps with new maps on each unmarshal call,
// instead of merging them.
//
// The merge strategy for two objects A and B is following:
// If A and B are maps, A and B will form a new map with keys from A and B and
// values from B will overwrite values of A. e.g.:
// A: B: merge(A, B):
// keep:A new:B keep:A
// update:fromA update:fromB update:fromB
// new:B
//
// If A is a map and B is not, this function will return an error,
// e.g. key:value and -slice.
//
// In all the remaining cases B will overwrite A.
func mergeMaps(dst interface{}, src interface{}) (interface{}, error) {
if dst == nil {
return src, nil
}
if src == nil {
return dst, nil
}
switch s := src.(type) {
case map[interface{}]interface{}:
dstMap, ok := dst.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf(
"can't merge map[interface{}]interface{} and %T. Source: %q. Destination: %q",
dst,
src,
dst)
}
for k, v := range s {
oldVal := dstMap[k]
if oldVal == nil {
dstMap[k] = v
} else {
tmp, err := mergeMaps(oldVal, v)
if err != nil {
return nil, err
}
dstMap[k] = tmp
}
}
default:
dst = src
}
return dst, nil
}
// NewYAMLProviderFromFiles creates a configuration provider from a set of YAML
// file names. All the objects are going to be merged and arrays/values
// overridden in the order of the files.
func NewYAMLProviderFromFiles(files ...string) (Provider, error) {
readClosers, err := filesToReaders(files...)
if err != nil {
return nil, err
}
readers := make([]io.Reader, len(readClosers))
for i, r := range readClosers {
readers[i] = r
}
provider, err := NewYAMLProviderFromReader(readers...)
for _, r := range readClosers {
nerr := r.Close()
if err == nil {
err = nerr
}
}
return provider, err
}
// NewYAMLProviderWithExpand creates a configuration provider from a set of YAML
// file names with ${var} or $var values replaced based on the mapping function.
// Variable names not wrapped in curly braces will be parsed according
// to the shell variable naming rules:
//
// ...a word consisting solely of underscores, digits, and
// alphabetics from the portable character set. The first
// character of a name is not a digit.
//
// For variables wrapped in braces, all characters between '{' and '}'
// will be passed to the expand function. e.g. "${foo:13}" will cause
// "foo:13" to be passed to the expand function. The sequence '$$' will
// be replaced by a literal '$'. All other sequences will be ignored
// for expansion purposes.
func NewYAMLProviderWithExpand(mapping func(string) (string, bool), files ...string) (Provider, error) {
readClosers, err := filesToReaders(files...)
if err != nil {
return nil, err
}
readers := make([]io.Reader, len(readClosers))
for i, r := range readClosers {
readers[i] = r
}
provider, err := NewYAMLProviderFromReaderWithExpand(mapping,
readers...)
for _, r := range readClosers {
nerr := r.Close()
if err == nil {
err = nerr
}
}
return provider, err
}
// NewYAMLProviderFromReader creates a configuration provider from a list of io.Readers.
// As above, all the objects are going to be merged and arrays/values overridden in the order of the files.
func NewYAMLProviderFromReader(readers ...io.Reader) (Provider, error) {
p, err := newYAMLProviderCore(readers...)
if err != nil {
return nil, err
}
return newCachedProvider(p)
}
// NewYAMLProviderFromReaderWithExpand creates a configuration provider from
// a list of `io.Readers and uses the mapping function to expand values
// in the underlying provider.
func NewYAMLProviderFromReaderWithExpand(
mapping func(string) (string, bool),
readers ...io.Reader) (Provider, error) {
expandFunc := replace(mapping)
ereaders := make([]io.Reader, len(readers))
for i, reader := range readers {
ereaders[i] = transform.NewReader(
reader,
&expandTransformer{expand: expandFunc})
}
return NewYAMLProviderFromReader(ereaders...)
}
// NewYAMLProviderFromBytes creates a config provider from a byte-backed YAML
// blobs. As above, all the objects are going to be merged and arrays/values
// overridden in the order of the yamls.
func NewYAMLProviderFromBytes(yamls ...[]byte) (Provider, error) {
readers := make([]io.Reader, len(yamls))
for i, yml := range yamls {
readers[i] = bytes.NewReader(yml)
}
return NewYAMLProviderFromReader(readers...)
}
func filesToReaders(files ...string) ([]io.ReadCloser, error) {
// load the files, read their bytes
readers := []io.ReadCloser{}
for _, v := range files {
if reader, err := os.Open(v); err != nil {
for _, r := range readers {
r.Close()
}
return nil, err
} else if reader != nil {
readers = append(readers, reader)
}
}
return readers, nil
}
func (y yamlConfigProvider) getNode(key string) *yamlNode {
if key == Root {
return &y.root
}
return y.root.Find(key)
}
// Name returns the config provider name.
func (y yamlConfigProvider) Name() string {
return "yaml"
}
// Get returns a configuration value by name
func (y yamlConfigProvider) Get(key string) Value {
node := y.getNode(key)
if node == nil {
return NewValue(y, key, nil, false)
}
return NewValue(y, key, node.value, true)
}
// nodeType is a simple YAML reader.
type nodeType int
const (
valueNode nodeType = iota
objectNode
arrayNode
)
type yamlNode struct {
nodeType nodeType
key string
value interface{}
children []*yamlNode
}
func (n yamlNode) String() string {
return fmt.Sprintf("%v", n.value)
}
func (n yamlNode) Type() reflect.Type {
return reflect.TypeOf(n.value)
}
// Find the first longest match in child nodes for the dottedPath.
func (n *yamlNode) Find(dottedPath string) *yamlNode {
for curr := dottedPath; len(curr) != 0; {
for _, v := range n.Children() {
if strings.EqualFold(v.key, curr) {
if curr == dottedPath {
return v
}
if node := v.Find(dottedPath[len(curr)+1:]); node != nil {
return node
}
}
}
if last := strings.LastIndex(curr, _separator); last > 0 {
curr = curr[:last]
} else {
break
}
}
return nil
}
// Children returns a slice containing this node's child nodes.
func (n *yamlNode) Children() []*yamlNode {
if n.children == nil {
n.children = []*yamlNode{}
switch n.nodeType {
case objectNode:
for k, v := range n.value.(map[interface{}]interface{}) {
n2 := &yamlNode{
nodeType: getNodeType(v),
// We need to use a default format, because key may be not a string.
key: fmt.Sprintf("%v", k),
value: v,
}
n.children = append(n.children, n2)
}
case arrayNode:
for k, v := range n.value.([]interface{}) {
n2 := &yamlNode{
nodeType: getNodeType(v),
key: strconv.Itoa(k),
value: v,
}
n.children = append(n.children, n2)
}
}
}
nodes := make([]*yamlNode, len(n.children))
copy(nodes, n.children)
return nodes
}
func unmarshalYAMLValue(reader io.Reader, value interface{}) error {
raw, err := ioutil.ReadAll(reader)
if err != nil {
return errors.Wrap(err, "failed to read the yaml config")
}
return yaml.Unmarshal(raw, value)
}
// Function to expand environment variables in returned values that have form: ${ENV_VAR:DEFAULT_VALUE}.
// For example, if an HTTP_PORT environment variable should be used for the HTTP module
// port, the config would look like this:
//
// modules:
// http:
// port: ${HTTP_PORT:8080}
//
// In the case that HTTP_PORT is not provided, default value (in this case 8080)
// will be used.
func replace(lookUp func(string) (string, bool)) func(in string) (string, error) {
return func(in string) (string, error) {
sep := strings.Index(in, _envSeparator)
var key string
var def string
if sep == -1 {
// separator missing - everything is the key ${KEY}
key = in
} else {
// ${KEY:DEFAULT}
key = in[:sep]
def = in[sep+1:]
}
if envVal, ok := lookUp(key); ok {
return envVal, nil
}
if def == "" {
return "", fmt.Errorf(`default is empty for %q (use "" for empty string)`, key)
} else if def == _emptyDefault {
return "", nil
}
return def, nil
}
}
func getNodeType(val interface{}) nodeType {
switch val.(type) {
case map[interface{}]interface{}:
return objectNode
case []interface{}:
return arrayNode
default:
return valueNode
}
}