forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
348 lines (293 loc) · 8.09 KB
/
template.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
package templates
import (
"bytes"
_ "embed"
"fmt"
"slices"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/connectorjs/evcm/util"
)
// Template describes is a proxy device for use with cli and automated testing
type Template struct {
TemplateDefinition
title string
titles []string
}
// GuidedSetupEnabled returns true if there are linked templates or >1 usage
func (t *Template) GuidedSetupEnabled() bool {
_, p := t.ParamByName(ParamUsage)
return len(t.Linked) > 0 || (len(p.Choice) > 1 && p.IsAllInOne())
}
// UpdateParamWithDefaults adds default values to specific param name entries
func (t *Template) UpdateParamsWithDefaults() error {
for i, p := range t.Params {
if index, resultMapItem := ConfigDefaults.ParamByName(strings.ToLower(p.Name)); index > -1 {
t.Params[i].OverwriteProperties(resultMapItem)
}
}
return nil
}
// validate the template (only rudimentary for now)
func (t *Template) Validate() error {
for _, c := range t.Capabilities {
if !slices.Contains(ValidCapabilities, c) {
return fmt.Errorf("invalid capability '%s' in template %s", c, t.Template)
}
}
for _, r := range t.Requirements.EVCM {
if !slices.Contains(ValidRequirements, r) {
return fmt.Errorf("invalid requirement '%s' in template %s", r, t.Template)
}
}
for _, p := range t.Params {
switch p.Name {
case ParamUsage:
for _, c := range p.Choice {
if !slices.Contains(ValidUsageChoices, c) {
return fmt.Errorf("invalid usage choice '%s' in template %s", c, t.Template)
}
}
case ParamModbus:
for _, c := range p.Choice {
if !slices.Contains(ValidModbusChoices, c) {
return fmt.Errorf("invalid modbus choice '%s' in template %s", c, t.Template)
}
}
}
}
return nil
}
// set the language title by combining all product titles
func (t *Template) SetCombinedTitle(lang string) {
if len(t.titles) == 0 {
t.resolveTitles(lang)
}
t.title = strings.Join(t.titles, "/")
}
// set the title for this templates
func (t *Template) SetTitle(title string) {
t.title = title
}
// return the title for this template
func (t *Template) Title() string {
return t.title
}
// return the language specific product titles
func (t *Template) Titles(lang string) []string {
if len(t.titles) == 0 {
t.resolveTitles(lang)
}
return t.titles
}
// set the language specific product titles
func (t *Template) resolveTitles(lang string) {
for _, p := range t.Products {
t.titles = append(t.titles, p.Title(lang))
}
}
// add the referenced base Params and overwrite existing ones
func (t *Template) ResolvePresets() error {
currentParams := make([]Param, len(t.Params))
copy(currentParams, t.Params)
t.Params = []Param{}
for _, p := range currentParams {
if p.Preset != "" {
base, ok := ConfigDefaults.Presets[p.Preset]
if !ok {
return fmt.Errorf("could not find preset definition: %s", p.Preset)
}
t.Params = append(t.Params, base.Params...)
continue
}
if i, _ := t.ParamByName(p.Name); i > -1 {
// take the preset values as a base and overwrite it with param values
p.OverwriteProperties(t.Params[i])
t.Params[i] = p
} else {
t.Params = append(t.Params, p)
}
}
return nil
}
// check if the provided group exists
func (t *Template) ResolveGroup() error {
if t.Group == "" {
return nil
}
_, ok := ConfigDefaults.DeviceGroups[t.Group]
if !ok {
return fmt.Errorf("could not find devicegroup definition: %s", t.Group)
}
return nil
}
// return the language specific group title
func (t *Template) GroupTitle(lang string) string {
tl := ConfigDefaults.DeviceGroups[t.Group]
return tl.String(lang)
}
// Defaults returns a map of default values for the template
func (t *Template) Defaults(renderMode string) map[string]interface{} {
values := make(map[string]interface{})
for _, p := range t.Params {
values[p.Name] = p.DefaultValue(renderMode)
}
return values
}
// SetParamDefault updates the default value of a param
func (t *Template) SetParamDefault(name string, value string) {
for i, p := range t.Params {
if p.Name == name {
t.Params[i].Default = value
return
}
}
}
// return the param with the given name
func (t *Template) ParamByName(name string) (int, Param) {
for i, p := range t.Params {
if strings.EqualFold(p.Name, name) {
return i, p
}
}
return -1, Param{}
}
// Usages returns the list of supported usages
func (t *Template) Usages() []string {
if i, p := t.ParamByName(ParamUsage); i > -1 {
return p.Choice
}
return nil
}
// return all modbus choices defined in the template
func (t *Template) ModbusChoices() []string {
if i, p := t.ParamByName(ParamModbus); i > -1 {
return p.Choice
}
return nil
}
//go:embed proxy.tpl
var proxyTmpl string
// RenderProxyWithValues renders the proxy template
func (t *Template) RenderProxyWithValues(values map[string]interface{}, lang string) ([]byte, error) {
tmpl, err := template.New("yaml").Funcs(sprig.TxtFuncMap()).Parse(proxyTmpl)
if err != nil {
panic(err)
}
t.ModbusParams("", values)
for index, p := range t.Params {
for k, v := range values {
if p.Name != k {
continue
}
switch p.Type {
case TypeStringList:
for _, e := range v.([]string) {
t.Params[index].Values = append(p.Values, yamlQuote(e))
}
default:
switch v := v.(type) {
case string:
t.Params[index].Value = yamlQuote(v)
case int:
t.Params[index].Value = fmt.Sprintf("%d", v)
}
}
}
}
// remove params with no values
var newParams []Param
for _, param := range t.Params {
if !param.IsRequired() {
switch param.Type {
case TypeStringList:
if len(param.Values) == 0 {
continue
}
default:
if param.Value == "" {
continue
}
}
}
newParams = append(newParams, param)
}
t.Params = newParams
out := new(bytes.Buffer)
data := map[string]interface{}{
"Template": t.Template,
"Params": t.Params,
}
err = tmpl.Execute(out, data)
return bytes.TrimSpace(out.Bytes()), err
}
// RenderResult renders the result template to instantiate the proxy
func (t *Template) RenderResult(renderMode string, other map[string]interface{}) ([]byte, map[string]interface{}, error) {
values := t.Defaults(renderMode)
if err := util.DecodeOther(other, &values); err != nil {
return nil, values, err
}
t.ModbusValues(renderMode, values)
res := make(map[string]interface{})
// TODO this is an utterly horrible hack
//
// When decoding the actual values ("other" parameter) into the
// defaults-populated map, mismatching key case will create multiple
// map entries for the same parameter.
// The code below tries to select the best, i.e. non-empty, value for the
// parameter and assigns it to the result key.
// The actual key name is taken from the parameter to make it unique.
// Since predefined properties are not matched by actual parameters using
// ParamByName(), the lower case key name is used instead.
// All keys *must* be assigned or rendering will create "<no value>" artifacts.
for key, val := range values {
out := strings.ToLower(key)
if i, p := t.ParamByName(key); i == -1 {
if !slices.Contains(predefinedTemplateProperties, out) {
return nil, values, fmt.Errorf("invalid key: %s", key)
}
} else if p.IsDeprecated() {
continue
} else {
out = p.Name
}
switch typed := val.(type) {
case []interface{}:
var list []string
for _, v := range typed {
list = append(list, yamlQuote(fmt.Sprintf("%v", v)))
}
if res[out] == nil || len(res[out].([]interface{})) == 0 {
res[out] = list
}
case []string:
var list []string
for _, v := range typed {
list = append(list, yamlQuote(v))
}
if res[out] == nil || len(res[out].([]string)) == 0 {
res[out] = list
}
default:
if res[out] == nil || res[out].(string) == "" {
// prevent rendering nil interfaces as "<nil>" string
var s string
if val != nil {
s = yamlQuote(fmt.Sprintf("%v", val))
}
res[out] = s
}
}
}
tmpl, err := baseTmpl.Clone()
if err == nil {
tmpl, err = FuncMap(tmpl).Parse(t.Render)
}
if err != nil {
return nil, res, err
}
out := new(bytes.Buffer)
err = tmpl.Execute(out, res)
return bytes.TrimSpace(out.Bytes()), res, err
}