Skip to content

Commit dcf2791

Browse files
authored
✨ [collection] Add ConvertMapToLoggerValues function for logr.Logger compatibility (#680)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description ConvertMapToLoggerValues is similar to ConvertMapToSlice but returns a type compatible with logr.Logger. ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [ ] This change is covered by existing or additional automated tests. - [x] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 369a014 commit dcf2791

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

changes/20250815151015.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
✨ [collection] Add ConvertMapToLoggerValues function for logr.Logger compatibility

utils/collection/parseLists.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ func ConvertMapToSlice[K comparable, V any](pairs map[K]V) []string {
137137
return slice
138138
}
139139

140+
// ConvertMapToLoggerValues is similar to ConvertMapToSlice but returns a type compatible with logr.Logger.
141+
func ConvertMapToLoggerValues[K comparable, V any](pairs map[K]V) []any {
142+
if len(pairs) == 0 {
143+
return nil
144+
}
145+
slice := make([]any, 0, len(pairs)*2)
146+
for key, value := range pairs {
147+
slice = append(slice, fmt.Sprintf("%v", key), value)
148+
}
149+
return slice
150+
}
151+
140152
// ConvertMapToPairSlice converts a map to list of key value pairs e.g. ["key1=value1", "key2=value2"]
141153
func ConvertMapToPairSlice[K comparable, V any](pairs map[K]V, pairSeparator string) []string {
142154
if len(pairs) == 0 {

0 commit comments

Comments
 (0)