-
Notifications
You must be signed in to change notification settings - Fork 34
/
config_util.go
63 lines (54 loc) · 1.22 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
package receivers
import (
"encoding/json"
"strings"
"gopkg.in/yaml.v3"
)
type DecryptFunc func(key string, fallback string) string
type CommaSeparatedStrings []string
func (r *CommaSeparatedStrings) UnmarshalJSON(b []byte) error {
var str string
if err := json.Unmarshal(b, &str); err != nil {
return err
}
if len(str) > 0 {
res := CommaSeparatedStrings(splitCommaDelimitedString(str))
*r = res
}
return nil
}
func (r *CommaSeparatedStrings) MarshalJSON() ([]byte, error) {
if r == nil {
return nil, nil
}
str := strings.Join(*r, ",")
return json.Marshal(str)
}
func (r *CommaSeparatedStrings) UnmarshalYAML(b []byte) error {
var str string
if err := yaml.Unmarshal(b, &str); err != nil {
return err
}
if len(str) > 0 {
res := CommaSeparatedStrings(splitCommaDelimitedString(str))
*r = res
}
return nil
}
func (r *CommaSeparatedStrings) MarshalYAML() ([]byte, error) {
if r == nil {
return nil, nil
}
str := strings.Join(*r, ",")
return yaml.Marshal(str)
}
func splitCommaDelimitedString(str string) []string {
split := strings.Split(str, ",")
res := make([]string, 0, len(split))
for _, s := range split {
if tr := strings.TrimSpace(s); tr != "" {
res = append(res, tr)
}
}
return res
}