Skip to content

Commit 50426c0

Browse files
collection Add functions for converting maps to slices in a stable/ordered way (#728)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description <!-- Please add any detail or context that would be useful to a reviewer. --> Add functions for converting maps to slices in a stable/ordered way ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] 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 920521d commit 50426c0

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

changes/20251015131907.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `collection` Add functions for converting maps to slices in a stable/ordered way

utils/collection/parseLists.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package collection
66

77
import (
8+
"cmp"
89
"fmt"
10+
"maps"
911
"slices"
1012
"strings"
1113
"unicode"
@@ -170,3 +172,20 @@ func ConvertMapToCommaSeparatedList[K comparable, V any](pairs map[K]V) string {
170172
func ConvertMapToCommaSeparatedPairsList[K comparable, V any](pairs map[K]V, pairSeparator string) string {
171173
return ConvertSliceToCommaSeparatedList[string](ConvertMapToPairSlice[K, V](pairs, pairSeparator))
172174
}
175+
176+
// ConvertMapToOrderedSlice converts a map to a slice and ensures that the ordering will always be the same
177+
func ConvertMapToOrderedSlice[K cmp.Ordered, V any](pairs map[K]V) []string {
178+
if len(pairs) == 0 {
179+
return nil
180+
}
181+
slice := make([]string, 0, len(pairs)*2)
182+
for _, key := range slices.Sorted(maps.Keys(pairs)) {
183+
slice = append(slice, fmt.Sprintf("%v", key), fmt.Sprintf("%v", pairs[key]))
184+
}
185+
return slice
186+
}
187+
188+
// ConvertMapToOrderedCommaSeparatedList converts a map to a commar-separated list and ensures that the ordering will always be the same
189+
func ConvertMapToOrderedCommaSeparatedList[K cmp.Ordered, V any](pairs map[K]V) string {
190+
return ConvertSliceToCommaSeparatedList[string](ConvertMapToOrderedSlice[K, V](pairs))
191+
}

utils/collection/parseLists_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func TestParseCommaSeparatedPairListToMap(t *testing.T) {
181181
{"Normal 10", ",, ,, ,", map[string]string{}, nil, "+"},
182182
{"Normal 11", ConvertMapToCommaSeparatedPairsList[string, string](randomMap, "/"), randomMap, nil, "/"},
183183
{"Normal 12", ConvertMapToCommaSeparatedPairsList[string, string](randomMap, " "), randomMap, nil, " "},
184+
{"Normal 13", ConvertMapToOrderedCommaSeparatedList[string, string](randomMap), randomMap, nil, ","},
184185
{"Bad 1", "one", nil, commonerrors.ErrInvalid, "+"},
185186
{"Bad 1", "one, two, three", nil, commonerrors.ErrInvalid, "+"},
186187
{"Bad 2", "one element with spaces", nil, commonerrors.ErrInvalid, "+"},
@@ -195,3 +196,11 @@ func TestParseCommaSeparatedPairListToMap(t *testing.T) {
195196
})
196197
}
197198
}
199+
200+
func TestConvertMapToCommaSeparatedListStable(t *testing.T) {
201+
testMap := map[string]string{"hello": "world", "adrien": "cabarbaye", "fish": "cake", "apple": "pie"}
202+
expected := "adrien,cabarbaye,apple,pie,fish,cake,hello,world"
203+
for range 100 {
204+
assert.Equal(t, expected, ConvertMapToOrderedCommaSeparatedList(testMap))
205+
}
206+
}

0 commit comments

Comments
 (0)