chore: extract common configuration mapping (#28237)
Some checks failed
Release / call-build-workflow (push) Has been cancelled
Release / Publish Docker :release (push) Has been cancelled
Release / Github & APT (push) Has been cancelled
Release / Demo (push) Has been cancelled
Release / Hassio Addon (push) Has been cancelled

This commit is contained in:
andig 2026-03-15 11:57:38 +01:00 • committed by GitHub
parent 304ccb7e54
commit 957ce1f3b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -133,48 +133,40 @@ func filterValidTemplateParams(tmpl *templates.Template, conf map[string]any) ma
return res
}
// 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) {
// mapTemplateConfig applies a mapping function to device configuration based on template parameters
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)
if err != nil {
return nil, err
}
res := make(map[string]any, len(conf))
for k, v := range conf {
if i, p := tmpl.ParamByName(k); i >= 0 {
if p.IsMasked() {
v = masked
} else if hidePrivate && p.IsPrivate() {
v = masked
}
return filterValidTemplateParams(&tmpl, lo.MapValues(conf, func(val any, key string) any {
if i, p := tmpl.ParamByName(key); i >= 0 {
val = fun(p, key, val)
}
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
func mergeMasked(class templates.Class, conf, old map[string]any) (map[string]any, error) {
tmpl, err := templateForConfig(class, conf)
if err != nil {
return nil, err
}
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 mapTemplateConfig(class, conf, func(p templates.Param, k string, v any) any {
if p.IsMasked() && v == masked {
return old[k]
}
res[k] = v
}
return filterValidTemplateParams(&tmpl, res), nil
return v
})
}
func startDeviceTimeout() (context.Context, context.CancelFunc, chan struct{}) {