-
Notifications
You must be signed in to change notification settings - Fork 27
/
values.go
214 lines (179 loc) · 4.87 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
package utils
import (
"encoding/json"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/peterbourgon/mergemap"
"github.com/segmentio/go-camelcase"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
k8syaml "sigs.k8s.io/yaml"
utils_checksum "github.com/flant/shell-operator/pkg/utils/checksum"
)
const (
GlobalValuesKey = "global"
)
// Values stores values for modules or hooks by name.
type Values map[string]interface{}
// ModuleNameToValuesKey returns camelCased name from kebab-cased (very-simple-module become verySimpleModule)
func ModuleNameToValuesKey(moduleName string) string {
return camelcase.Camelcase(moduleName)
}
// ModuleNameFromValuesKey returns kebab-cased name from camelCased (verySimpleModule become ver-simple-module)
func ModuleNameFromValuesKey(moduleValuesKey string) string {
b := make([]byte, 0, 64)
l := len(moduleValuesKey)
i := 0
for i < l {
c := moduleValuesKey[i]
switch {
case c >= 'A' && c <= 'Z':
if i > 0 {
// Appends dash module name parts delimiter.
b = append(b, '-')
}
// Appends lowercased symbol.
b = append(b, c+('a'-'A'))
case c >= '0' && c <= '9':
if i > 0 {
// Appends dash module name parts delimiter.
b = append(b, '-')
}
b = append(b, c)
default:
b = append(b, c)
}
i++
}
return string(b)
}
// NewValuesFromBytes loads values sections from maps in yaml or json format
func NewValuesFromBytes(data []byte) (Values, error) {
var values map[string]interface{}
err := k8syaml.Unmarshal(data, &values)
if err != nil {
return nil, fmt.Errorf("bad values data: %s\n%s", err, string(data))
}
return values, nil
}
// NewValues load all sections from input data and makes sure that input map
// can be marshaled to yaml and that yaml is compatible with json.
func NewValues(data map[string]interface{}) (Values, error) {
yamlDoc, err := k8syaml.Marshal(data)
if err != nil {
return nil, fmt.Errorf("data is not compatible with JSON and YAML: %s, data:\n%s", err, spew.Sdump(data))
}
var values Values
if err := k8syaml.Unmarshal(yamlDoc, &values); err != nil {
return nil, fmt.Errorf("convert data YAML to values: %s, data:\n%s", err, spew.Sdump(data))
}
return values, nil
}
// NewGlobalValues creates Values with global section loaded from input string.
func NewGlobalValues(globalSectionContent string) (Values, error) {
var section map[string]interface{}
if err := k8syaml.Unmarshal([]byte(globalSectionContent), §ion); err != nil {
return nil, fmt.Errorf("global section is not compatible with JSON and YAML: %s, data:\n%s", err, globalSectionContent)
}
return Values{
GlobalValuesKey: section,
}, nil
}
func MergeValues(values ...Values) Values {
res := make(Values)
for _, v := range values {
res = mergemap.Merge(res, v)
}
return res
}
// DebugString returns values as yaml or an error line if dump is failed
func (v Values) DebugString() string {
b, err := v.YamlBytes()
if err != nil {
return "bad values: " + err.Error()
}
return string(b)
}
func (v Values) Checksum() (string, error) {
valuesJson, err := json.Marshal(v)
if err != nil {
return "", err
}
return utils_checksum.CalculateChecksum(string(valuesJson)), nil
}
func (v Values) HasKey(key string) bool {
_, has := v[key]
return has
}
func (v Values) HasGlobal() bool {
_, has := v[GlobalValuesKey]
return has
}
func (v Values) Global() Values {
globalValues, has := v[GlobalValuesKey]
if has {
data := map[string]interface{}{GlobalValuesKey: globalValues}
newV, err := NewValues(data)
if err != nil {
log.Errorf("get global Values: %s", err)
}
return newV
}
return make(Values)
}
func (v Values) SectionByKey(key string) Values {
sectionValues, has := v[key]
if has {
data := map[string]interface{}{key: sectionValues}
newV, err := NewValues(data)
if err != nil {
log.Errorf("get section '%s' Values: %s", key, err)
}
return newV
}
return make(Values)
}
func (v Values) AsBytes(format string) ([]byte, error) {
switch format {
case "json":
return json.Marshal(v)
case "yaml":
fallthrough
default:
return yaml.Marshal(v)
}
}
func (v Values) AsString(format string) (string, error) {
b, err := v.AsBytes(format)
if err != nil {
return "", err
}
return string(b), nil
}
// AsConfigMapData returns values as map that can be used as a 'data' field in the ConfigMap.
func (v Values) AsConfigMapData() (map[string]string, error) {
res := make(map[string]string)
for k, value := range v {
dump, err := yaml.Marshal(value)
if err != nil {
return nil, err
}
res[k] = string(dump)
}
return res, nil
}
func (v Values) JsonString() (string, error) {
return v.AsString("json")
}
func (v Values) JsonBytes() ([]byte, error) {
return v.AsBytes("json")
}
func (v Values) YamlString() (string, error) {
return v.AsString("yaml")
}
func (v Values) YamlBytes() ([]byte, error) {
return v.AsBytes("yaml")
}
func (v Values) IsEmpty() bool {
return len(v) == 0
}