Skip to content

Commit 631fb2c

Browse files
authored
[collection] Expose utilities to convert slices into maps (#617)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description expose utilities ### 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 e624ebc commit 631fb2c

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

changes/20250516152950.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `[collection]` Expose utilities to convert slices into maps

utils/collection/parseLists.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,26 @@ func ParseCommaSeparatedList(input string) []string {
5858

5959
// ParseCommaSeparatedListToMap returns a map of key value pairs from a string containing a comma separated list
6060
func ParseCommaSeparatedListToMap(input string) (pairs map[string]string, err error) {
61-
inputSplit := ParseCommaSeparatedList(input)
62-
numElements := len(inputSplit)
61+
pairs, err = ConvertSliceToMap[string](ParseCommaSeparatedList(input))
62+
return
63+
}
64+
65+
// ConvertSliceToMap converts a slice of elements into a map e.g. [key1, value1, key2, values2] -> {key1: value1, key2: value2}
66+
func ConvertSliceToMap[T comparable](input []T) (pairs map[T]T, err error) {
67+
if len(input) == 0 {
68+
return
69+
}
70+
numElements := len(input)
6371

6472
if numElements%2 != 0 {
65-
err = commonerrors.Newf(commonerrors.ErrInvalid, "could not parse comma separated list '%v' into map as it did not have an even number of elements", input)
73+
err = commonerrors.Newf(commonerrors.ErrInvalid, "could not convert the list into a map as it does not have an even number of elements")
6674
return
6775
}
6876

69-
pairs = make(map[string]string, numElements/2)
77+
pairs = make(map[T]T, numElements/2)
7078
// TODO use slices.Chunk introduced in go 23 when library is upgraded
7179
for i := 0; i < numElements; i += 2 {
72-
pairs[inputSplit[i]] = inputSplit[i+1]
80+
pairs[input[i]] = input[i+1]
7381
}
7482

7583
return
@@ -81,17 +89,24 @@ func ParseCommaSeparatedListOfPairsToMap(input, pairSeparator string) (pairs map
8189
pairs, err = ParseCommaSeparatedListToMap(input)
8290
return
8391
}
84-
inputSplit := ParseCommaSeparatedList(input)
85-
pairs = make(map[string]string, len(inputSplit))
86-
for i := range inputSplit {
87-
pair := ParseListWithCleanup(inputSplit[i], pairSeparator)
92+
pairs, err = ConvertListOfPairsToMap(ParseCommaSeparatedList(input), pairSeparator)
93+
return
94+
}
95+
96+
func ConvertListOfPairsToMap(input []string, pairSeparator string) (pairs map[string]string, err error) {
97+
if len(input) == 0 {
98+
return
99+
}
100+
pairs = make(map[string]string, len(input))
101+
for i := range input {
102+
pair := ParseListWithCleanup(input[i], pairSeparator)
88103
switch len(pair) {
89104
case 0:
90105
continue
91106
case 2:
92107
pairs[pair[0]] = pair[1]
93108
default:
94-
err = commonerrors.Newf(commonerrors.ErrInvalid, "could not parse key value pair '%v'", inputSplit[i])
109+
err = commonerrors.Newf(commonerrors.ErrInvalid, "could not parse key value pair '%v'", input[i])
95110
return
96111
}
97112
}

0 commit comments

Comments
 (0)