forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalues.go
349 lines (313 loc) · 9.47 KB
/
values.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
// Package values is a set of value types to use in provisioning. They add custom unmarshaling logic that puts the string values
// through os.ExpandEnv.
// Usage:
//
// type Data struct {
// Field StringValue `yaml:"field"` // Instead of string
// }
//
// d := &Data{}
// // unmarshal into d
// d.Field.Value() // returns the final interpolated value from the yaml file
package values
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"github.com/grafana/grafana/pkg/setting"
)
// IntValue represents a string value in a YAML
// config that can be overridden by environment variables
type IntValue struct {
value int
Raw string
}
// UnmarshalYAML converts YAML into an *IntValue
func (val *IntValue) UnmarshalYAML(unmarshal func(interface{}) error) error {
interpolated, err := getInterpolated(unmarshal)
if err != nil {
return err
}
if len(interpolated.value) == 0 {
// To keep the same behaviour as the yaml lib which just does not set the value if it is empty.
return nil
}
val.Raw = interpolated.raw
val.value, err = strconv.Atoi(interpolated.value)
if err != nil {
return fmt.Errorf("%v: %w", "cannot convert value int", err)
}
return err
}
// Value returns the wrapped int value
func (val *IntValue) Value() int {
return val.value
}
// Int64Value represents a string value in a YAML
// config that can be overridden by environment variables
type Int64Value struct {
value int64
Raw string
}
// UnmarshalYAML converts YAML into an *Int64Value
func (val *Int64Value) UnmarshalYAML(unmarshal func(interface{}) error) error {
interpolated, err := getInterpolated(unmarshal)
if err != nil {
return err
}
if len(interpolated.value) == 0 {
// To keep the same behaviour as the yaml lib which just does not set the value if it is empty.
return nil
}
val.Raw = interpolated.raw
val.value, err = strconv.ParseInt(interpolated.value, 10, 64)
return err
}
// Value returns the wrapped int64 value
func (val *Int64Value) Value() int64 {
return val.value
}
// StringValue represents a string value in a YAML
// config that can be overridden by environment variables
type StringValue struct {
value string
Raw string
}
// UnmarshalYAML converts YAML into an *StringValue
func (val *StringValue) UnmarshalYAML(unmarshal func(interface{}) error) error {
interpolated, err := getInterpolated(unmarshal)
if err != nil {
return err
}
val.Raw = interpolated.raw
val.value = interpolated.value
return err
}
// Value returns the wrapped string value
func (val *StringValue) Value() string {
return val.value
}
// BoolValue represents a string value in a YAML
// config that can be overridden by environment variables
type BoolValue struct {
value bool
Raw string
}
// UnmarshalYAML converts YAML into an *BoolValue
func (val *BoolValue) UnmarshalYAML(unmarshal func(interface{}) error) error {
interpolated, err := getInterpolated(unmarshal)
if err != nil {
return err
}
val.Raw = interpolated.raw
val.value, err = strconv.ParseBool(interpolated.value)
return err
}
// Value returns the wrapped bool value
func (val *BoolValue) Value() bool {
return val.value
}
// JSONValue represents a string value in a YAML
// config that can be overridden by environment variables
type JSONValue struct {
value map[string]interface{}
Raw map[string]interface{}
}
// UnmarshalYAML converts YAML into an *JSONValue
func (val *JSONValue) UnmarshalYAML(unmarshal func(interface{}) error) error {
unmarshaled := make(map[string]interface{})
err := unmarshal(unmarshaled)
if err != nil {
return err
}
interpolated := make(map[string]interface{})
raw := make(map[string]interface{})
for key, val := range unmarshaled {
interpolated[key], raw[key], err = transformInterface(val)
if err != nil {
return err
}
}
val.Raw = raw
val.value = interpolated
return err
}
// Value returns the wrapped JSON value as map[string]interface{}
func (val *JSONValue) Value() map[string]interface{} {
return val.value
}
// StringMapValue represents a string value in a YAML
// config that can be overridden by environment variables
type StringMapValue struct {
value map[string]string
Raw map[string]string
}
// UnmarshalYAML converts YAML into an *StringMapValue
func (val *StringMapValue) UnmarshalYAML(unmarshal func(interface{}) error) error {
unmarshaled := make(map[string]string)
err := unmarshal(unmarshaled)
if err != nil {
return err
}
interpolated := make(map[string]string)
raw := make(map[string]string)
for key, val := range unmarshaled {
interpolated[key], raw[key], err = interpolateValue(val)
if err != nil {
return err
}
}
val.Raw = raw
val.value = interpolated
return err
}
// Value returns the wrapped map[string]string value
func (val *StringMapValue) Value() map[string]string {
return val.value
}
// JSONSliceValue represents a slice value in a YAML
// config that can be overridden by environment variables
type JSONSliceValue struct {
value []map[string]interface{}
Raw []map[string]interface{}
}
// UnmarshalYAML converts YAML into an *JSONSliceValue
func (val *JSONSliceValue) UnmarshalYAML(unmarshal func(interface{}) error) error {
unmarshaled := make([]interface{}, 0)
err := unmarshal(&unmarshaled)
if err != nil {
return err
}
interpolated := make([]map[string]interface{}, 0)
raw := make([]map[string]interface{}, 0)
for _, v := range unmarshaled {
i := make(map[string]interface{})
r := make(map[string]interface{})
for key, val := range v.(map[string]interface{}) {
i[key], r[key], err = transformInterface(val)
if err != nil {
return err
}
}
interpolated = append(interpolated, i)
raw = append(raw, r)
}
val.Raw = raw
val.value = interpolated
return err
}
// Value returns the wrapped []interface{} value
func (val *JSONSliceValue) Value() []map[string]interface{} {
return val.value
}
// transformInterface tries to transform any interface type into proper value with env expansion. It traverses maps and
// slices and the actual interpolation is done on all simple string values in the structure. It returns a copy of any
// map or slice value instead of modifying them in place and also return value without interpolation but with converted
// type as a second value.
func transformInterface(i interface{}) (interface{}, interface{}, error) {
typeOf := reflect.TypeOf(i)
if typeOf == nil {
return nil, nil, nil
}
switch typeOf.Kind() {
case reflect.Slice:
return transformSlice(i.([]interface{}))
case reflect.Map:
return transformMap(i.(map[string]interface{}))
case reflect.String:
return interpolateIfaceValue(i.(string))
default:
// Was int, float or some other value that we do not need to do any transform on.
return i, i, nil
}
}
func transformSlice(i []interface{}) (interface{}, interface{}, error) {
transformedSlice := make([]interface{}, 0, len(i))
rawSlice := make([]interface{}, 0, len(i))
for _, val := range i {
transformed, raw, err := transformInterface(val)
if err != nil {
return nil, nil, err
}
transformedSlice = append(transformedSlice, transformed)
rawSlice = append(rawSlice, raw)
}
return transformedSlice, rawSlice, nil
}
func transformMap(i map[string]interface{}) (interface{}, interface{}, error) {
transformed := make(map[string]interface{})
raw := make(map[string]interface{})
for key, val := range i {
var err error
transformed[key], raw[key], err = transformInterface(val)
if err != nil {
return nil, nil, err
}
}
return transformed, raw, nil
}
func interpolateIfaceValue(val string) (interface{}, string, error) {
parts := strings.Split(val, "$$")
if len(parts) > 1 {
return interpolateValue(val)
}
expanded, err := setting.ExpandVar(val)
if err != nil {
return val, val, fmt.Errorf("failed to interpolate value '%s': %w", val, err)
}
expandedEnv := os.ExpandEnv(expanded)
if expandedEnv != val {
// If the value is an environment variable, consider it may not be a string
intV, err := strconv.ParseInt(expandedEnv, 10, 64)
if err == nil {
return intV, val, nil
}
floatV, err := strconv.ParseFloat(expandedEnv, 64)
if err == nil {
return floatV, val, nil
}
boolV, err := strconv.ParseBool(expandedEnv)
if err == nil {
return boolV, val, nil
}
}
return expandedEnv, val, nil
}
// interpolateValue returns the final value after interpolation. In addition to environment variable interpolation,
// expanders available for the settings file are expanded here.
// For a literal '$', '$$' can be used to avoid interpolation.
func interpolateValue(val string) (string, string, error) {
parts := strings.Split(val, "$$")
interpolated := make([]string, len(parts))
for i, v := range parts {
expanded, err := setting.ExpandVar(v)
if err != nil {
return val, val, fmt.Errorf("failed to interpolate value '%s': %w", val, err)
}
v = expanded
interpolated[i] = os.ExpandEnv(v)
}
return strings.Join(interpolated, "$"), val, nil
}
type interpolated struct {
value string
raw string
}
// getInterpolated unmarshals the value as string and runs interpolation on it. It is the responsibility of each
// value type to convert this string value to appropriate type.
func getInterpolated(unmarshal func(interface{}) error) (*interpolated, error) {
var veryRaw string
err := unmarshal(&veryRaw)
if err != nil {
return &interpolated{}, err
}
// We get new raw value here which can have a bit different type, as yaml types nested maps as
// map[interface{}]interface and we want it to be map[string]interface{}
value, raw, err := interpolateValue(veryRaw)
if err != nil {
return &interpolated{}, err
}
return &interpolated{raw: raw, value: value}, nil
}