forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
yaml_utils.go
100 lines (92 loc) · 2.03 KB
/
yaml_utils.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
package checkconfig
import (
"fmt"
"strconv"
)
// StringArray is list of string with a yaml un-marshaller that support both array and string.
// See test file for example usage.
// Credit: https://github.com/go-yaml/yaml/issues/100#issuecomment-324964723
type StringArray []string
// Number can unmarshal yaml string or integer
type Number int
// Boolean can unmarshal yaml string or bool value
type Boolean bool
//UnmarshalYAML unmarshalls StringArray
func (a *StringArray) UnmarshalYAML(unmarshal func(interface{}) error) error {
var multi []string
err := unmarshal(&multi)
if err != nil {
var single string
err := unmarshal(&single)
if err != nil {
return err
}
*a = []string{single}
} else {
*a = multi
}
return nil
}
//UnmarshalYAML unmarshalls Number
func (n *Number) UnmarshalYAML(unmarshal func(interface{}) error) error {
var integer int
err := unmarshal(&integer)
if err != nil {
var str string
err := unmarshal(&str)
if err != nil {
return err
}
num, err := strconv.Atoi(str)
if err != nil {
return err
}
*n = Number(num)
} else {
*n = Number(integer)
}
return nil
}
//UnmarshalYAML unmarshalls Boolean
func (b *Boolean) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value bool
err := unmarshal(&value)
if err != nil {
var str string
err := unmarshal(&str)
if err != nil {
return err
}
switch str {
case "true":
value = true
case "false":
value = false
default:
return fmt.Errorf("cannot convert `%s` to boolean", str)
}
value = str == "true"
*b = Boolean(value)
} else {
*b = Boolean(value)
}
return nil
}
//UnmarshalYAML unmarshalls MetricTagConfigList
func (a *MetricTagConfigList) UnmarshalYAML(unmarshal func(interface{}) error) error {
var multi []MetricTagConfig
err := unmarshal(&multi)
if err != nil {
var tags []string
err := unmarshal(&tags)
if err != nil {
return err
}
multi = []MetricTagConfig{}
for _, tag := range tags {
multi = append(multi, MetricTagConfig{symbolTag: tag})
}
}
*a = multi
return nil
}