diff --git a/changes/20250815151015.feature b/changes/20250815151015.feature new file mode 100644 index 0000000000..1c41d61da8 --- /dev/null +++ b/changes/20250815151015.feature @@ -0,0 +1 @@ +✨ [collection] Add ConvertMapToLoggerValues function for logr.Logger compatibility \ No newline at end of file diff --git a/utils/collection/parseLists.go b/utils/collection/parseLists.go index eccdfb1bef..9c3b47b959 100644 --- a/utils/collection/parseLists.go +++ b/utils/collection/parseLists.go @@ -137,6 +137,18 @@ func ConvertMapToSlice[K comparable, V any](pairs map[K]V) []string { return slice } +// ConvertMapToLoggerValues is similar to ConvertMapToSlice but returns a type compatible with logr.Logger. +func ConvertMapToLoggerValues[K comparable, V any](pairs map[K]V) []any { + if len(pairs) == 0 { + return nil + } + slice := make([]any, 0, len(pairs)*2) + for key, value := range pairs { + slice = append(slice, fmt.Sprintf("%v", key), value) + } + return slice +} + // ConvertMapToPairSlice converts a map to list of key value pairs e.g. ["key1=value1", "key2=value2"] func ConvertMapToPairSlice[K comparable, V any](pairs map[K]V, pairSeparator string) []string { if len(pairs) == 0 {