From 416fe474fda480997178db2524ee26ad4ff5a947 Mon Sep 17 00:00:00 2001 From: Nirmal Sunny Date: Fri, 15 Aug 2025 15:10:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20[collection]=20Add=20ConvertMapToLo?= =?UTF-8?q?ggerValues=20function=20for=20logr.Logger=20compatibility?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changes/20250815151015.feature | 1 + utils/collection/parseLists.go | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 changes/20250815151015.feature 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 {