-
Notifications
You must be signed in to change notification settings - Fork 27
/
values.go
272 lines (224 loc) · 6.1 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
package utils
import (
"encoding/json"
"fmt"
"reflect"
"github.com/davecgh/go-spew/spew"
"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(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 {
valuesJson, _ := json.Marshal(v)
return utils_checksum.CalculateChecksum(string(valuesJson))
}
func (v Values) HasKey(key string) bool {
_, has := v[key]
return has
}
func (v Values) GetKeySection(key string) Values {
section, has := v[key]
if !has {
return Values{}
}
switch sec := section.(type) {
case map[string]interface{}:
return sec
case Values:
return sec
}
return Values{}
}
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)
}
// Deprecated: some useless copy here, probably we don't need that
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 {
b, _ := v.AsBytes(format)
return string(b)
}
// 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 {
return v.AsString("json")
}
func (v Values) JsonBytes() ([]byte, error) {
return v.AsBytes("json")
}
func (v Values) YamlString() string {
return v.AsString("yaml")
}
func (v Values) YamlBytes() ([]byte, error) {
return v.AsBytes("yaml")
}
func (v Values) IsEmpty() bool {
return len(v) == 0
}
// Copy returns full deep copy of the Values
func (v Values) Copy() Values {
return deepCopyMap(v)
}
func deepCopyMap(originalMap map[string]interface{}) map[string]interface{} {
copiedMap := make(map[string]interface{})
for key, value := range originalMap {
copiedMap[key] = valueDeepCopy(value)
}
return copiedMap
}
func valueDeepCopy(item interface{}) interface{} {
if item == nil {
return nil
}
typ := reflect.TypeOf(item)
val := reflect.ValueOf(item)
switch typ.Kind() {
case reflect.Ptr:
newVal := reflect.New(typ.Elem())
newVal.Elem().Set(reflect.ValueOf(valueDeepCopy(val.Elem().Interface())))
return newVal.Interface()
case reflect.Map:
newMap := reflect.MakeMap(typ)
for _, k := range val.MapKeys() {
newMap.SetMapIndex(k, reflect.ValueOf(valueDeepCopy(val.MapIndex(k).Interface())))
}
return newMap.Interface()
case reflect.Slice:
newSlice := reflect.MakeSlice(typ, val.Len(), val.Cap())
for i := 0; i < val.Len(); i++ {
newSlice.Index(i).Set(reflect.ValueOf(valueDeepCopy(val.Index(i).Interface())))
}
return newSlice.Interface()
}
return item
}