Skip to content

Commit 957ce1f

Browse files
authored
chore: extract common configuration mapping (#28237)
1 parent 304ccb7 commit 957ce1f

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

server/http_config_helper.go

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -133,48 +133,40 @@ func filterValidTemplateParams(tmpl *templates.Template, conf map[string]any) ma
133133
return res
134134
}
135135

136-
// sanitizeMasked replaces masked and private configuration properties with the `***` placeholder
137-
func sanitizeMasked(class templates.Class, conf map[string]any, hidePrivate bool) (map[string]any, error) {
136+
// mapTemplateConfig applies a mapping function to device configuration based on template parameters
137+
func mapTemplateConfig(class templates.Class, conf map[string]any, fun func(p templates.Param, k string, v any) any) (map[string]any, error) {
138138
tmpl, err := templateForConfig(class, conf)
139139
if err != nil {
140140
return nil, err
141141
}
142142

143-
res := make(map[string]any, len(conf))
144-
145-
for k, v := range conf {
146-
if i, p := tmpl.ParamByName(k); i >= 0 {
147-
if p.IsMasked() {
148-
v = masked
149-
} else if hidePrivate && p.IsPrivate() {
150-
v = masked
151-
}
143+
return filterValidTemplateParams(&tmpl, lo.MapValues(conf, func(val any, key string) any {
144+
if i, p := tmpl.ParamByName(key); i >= 0 {
145+
val = fun(p, key, val)
152146
}
153147

154-
res[k] = v
155-
}
148+
return val
149+
})), nil
150+
}
156151

157-
return filterValidTemplateParams(&tmpl, res), nil
152+
// sanitizeMasked replaces masked and private configuration properties with the `***` placeholder
153+
func sanitizeMasked(class templates.Class, conf map[string]any, hidePrivate bool) (map[string]any, error) {
154+
return mapTemplateConfig(class, conf, func(p templates.Param, _ string, v any) any {
155+
if p.IsMasked() || hidePrivate && p.IsPrivate() {
156+
return masked
157+
}
158+
return v
159+
})
158160
}
159161

160162
// mergeMasked replaces masked `***` configuration properties with their actual values
161163
func mergeMasked(class templates.Class, conf, old map[string]any) (map[string]any, error) {
162-
tmpl, err := templateForConfig(class, conf)
163-
if err != nil {
164-
return nil, err
165-
}
166-
167-
res := make(map[string]any, len(conf))
168-
169-
for k, v := range conf {
170-
if i, p := tmpl.ParamByName(k); i >= 0 && p.IsMasked() && v == masked {
171-
v = old[k]
164+
return mapTemplateConfig(class, conf, func(p templates.Param, k string, v any) any {
165+
if p.IsMasked() && v == masked {
166+
return old[k]
172167
}
173-
174-
res[k] = v
175-
}
176-
177-
return filterValidTemplateParams(&tmpl, res), nil
168+
return v
169+
})
178170
}
179171

180172
func startDeviceTimeout() (context.Context, context.CancelFunc, chan struct{}) {

0 commit comments

Comments
 (0)