chore: extract common configuration mapping (#28237)
Some checks failed
Some checks failed
This commit is contained in:
parent
304ccb7e54
commit
957ce1f3b4
1 changed files with 21 additions and 29 deletions
|
|
@ -133,48 +133,40 @@ func filterValidTemplateParams(tmpl *templates.Template, conf map[string]any) ma
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanitizeMasked replaces masked and private configuration properties with the `***` placeholder
|
// mapTemplateConfig applies a mapping function to device configuration based on template parameters
|
||||||
func sanitizeMasked(class templates.Class, conf map[string]any, hidePrivate bool) (map[string]any, error) {
|
func mapTemplateConfig(class templates.Class, conf map[string]any, fun func(p templates.Param, k string, v any) any) (map[string]any, error) {
|
||||||
tmpl, err := templateForConfig(class, conf)
|
tmpl, err := templateForConfig(class, conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make(map[string]any, len(conf))
|
return filterValidTemplateParams(&tmpl, lo.MapValues(conf, func(val any, key string) any {
|
||||||
|
if i, p := tmpl.ParamByName(key); i >= 0 {
|
||||||
for k, v := range conf {
|
val = fun(p, key, val)
|
||||||
if i, p := tmpl.ParamByName(k); i >= 0 {
|
|
||||||
if p.IsMasked() {
|
|
||||||
v = masked
|
|
||||||
} else if hidePrivate && p.IsPrivate() {
|
|
||||||
v = masked
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res[k] = v
|
return val
|
||||||
}
|
})), nil
|
||||||
|
}
|
||||||
|
|
||||||
return filterValidTemplateParams(&tmpl, res), nil
|
// sanitizeMasked replaces masked and private configuration properties with the `***` placeholder
|
||||||
|
func sanitizeMasked(class templates.Class, conf map[string]any, hidePrivate bool) (map[string]any, error) {
|
||||||
|
return mapTemplateConfig(class, conf, func(p templates.Param, _ string, v any) any {
|
||||||
|
if p.IsMasked() || hidePrivate && p.IsPrivate() {
|
||||||
|
return masked
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// mergeMasked replaces masked `***` configuration properties with their actual values
|
// mergeMasked replaces masked `***` configuration properties with their actual values
|
||||||
func mergeMasked(class templates.Class, conf, old map[string]any) (map[string]any, error) {
|
func mergeMasked(class templates.Class, conf, old map[string]any) (map[string]any, error) {
|
||||||
tmpl, err := templateForConfig(class, conf)
|
return mapTemplateConfig(class, conf, func(p templates.Param, k string, v any) any {
|
||||||
if err != nil {
|
if p.IsMasked() && v == masked {
|
||||||
return nil, err
|
return old[k]
|
||||||
}
|
|
||||||
|
|
||||||
res := make(map[string]any, len(conf))
|
|
||||||
|
|
||||||
for k, v := range conf {
|
|
||||||
if i, p := tmpl.ParamByName(k); i >= 0 && p.IsMasked() && v == masked {
|
|
||||||
v = old[k]
|
|
||||||
}
|
}
|
||||||
|
return v
|
||||||
res[k] = v
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return filterValidTemplateParams(&tmpl, res), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startDeviceTimeout() (context.Context, context.CancelFunc, chan struct{}) {
|
func startDeviceTimeout() (context.Context, context.CancelFunc, chan struct{}) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue