From 957ce1f3b4d60572d522a0ad04c3520a4c4fc29e Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 15 Mar 2026 11:57:38 +0100 Subject: [PATCH] chore: extract common configuration mapping (#28237) --- server/http_config_helper.go | 50 +++++++++++++++--------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/server/http_config_helper.go b/server/http_config_helper.go index 69cef6197..10cfedd35 100644 --- a/server/http_config_helper.go +++ b/server/http_config_helper.go @@ -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{}) {