Files
windows_exporter/internal/config/flatten.go
2026-10-08 12:57:04 +02:00

71 lines
2.0 KiB
Go

// SPDX-License-Identifier: Apache-2.0
//
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
package config
import "fmt"
// convertMap converts a map with any comparable key type to a map with string keys.
func convertMap[K comparable, V any](originalMap map[K]V) map[string]V {
convertedMap := make(map[string]V, len(originalMap))
for key, value := range originalMap {
if keyString, ok := any(key).(string); ok {
convertedMap[keyString] = value
}
}
return convertedMap
}
// flatten flattens a nested map, joining keys with dots. Each key maps to its
// value, or to the elements of a list. Keys without a value are left out.
// e.g. {"a": {"b":"c", "d":["e","f"]}} => {"a.b":["c"], "a.d":["e","f"]}
func flatten(data map[string]any) map[string][]string {
result := make(map[string][]string)
flattenHelper("", data, result)
return result
}
func flattenHelper(prefix string, data map[string]any, result map[string][]string) {
for k, v := range data {
fullKey := k
if prefix != "" {
fullKey = prefix + "." + k
}
switch val := v.(type) {
case map[any]any:
flattenHelper(fullKey, convertMap(val), result)
case map[string]any:
flattenHelper(fullKey, val, result)
case []any:
strSlice := make([]string, len(val))
for i, elem := range val {
strSlice[i] = fmt.Sprint(elem)
}
result[fullKey] = strSlice
case nil:
// A key without a value, like "file:", leaves the flag unchanged.
default:
result[fullKey] = []string{fmt.Sprint(val)}
}
}
}