Skip to content

Commit a76d506

Browse files
committed
Add util func to concat map values
Signed-off-by: Rajeev Ranjan <rajeev2.ranjan@intel.com>
1 parent 16cfd45 commit a76d506

3 files changed

Lines changed: 98 additions & 1 deletion

File tree

inventory/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.27.0
1+
2.27.1-dev

inventory/pkg/util/util.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"math"
1010
"math/big"
1111
"os"
12+
"sort"
13+
"strconv"
1214
"strings"
1315

1416
"github.com/google/uuid"
@@ -1072,6 +1074,34 @@ func ConvertInventoryUUIDToLenovoUUID(uuidStr string) string {
10721074
return strings.ToUpper(strings.ReplaceAll(uuidStr, "-", ""))
10731075
}
10741076

1077+
// ConcatMapValuesSorted takes a map of string keys and values, sorts the keys alphabetically,
1078+
// concatenates the corresponding non-empty values using the specified delimiter, and returns
1079+
// the resulting string wrapped in double quotes. If the map is empty or all values are empty,
1080+
// it returns an empty string.
1081+
func ConcatMapValuesSorted(m map[string]string, delimiter string) string {
1082+
if len(m) == 0 {
1083+
return ""
1084+
}
1085+
keys := make([]string, 0, len(m))
1086+
for k := range m {
1087+
keys = append(keys, k)
1088+
}
1089+
// Sort keys alphabetically
1090+
sort.Strings(keys)
1091+
values := make([]string, 0, len(keys))
1092+
for _, k := range keys {
1093+
if m[k] != "" {
1094+
values = append(values, m[k])
1095+
}
1096+
}
1097+
1098+
if len(values) == 0 {
1099+
return ""
1100+
}
1101+
1102+
return strconv.Quote(strings.Join(values, delimiter))
1103+
}
1104+
10751105
type resourceKeyCarrier interface {
10761106
GetTenantId() string
10771107
GetResourceId() string

inventory/pkg/util/util_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,3 +1264,70 @@ func TestGetResourceKeyFromResource(t *testing.T) {
12641264
assert.Equal(t, "", resourceID)
12651265
}
12661266
}
1267+
1268+
func TestConcatMapValuesSorted(t *testing.T) {
1269+
tests := []struct {
1270+
name string
1271+
input map[string]string
1272+
expected string
1273+
}{
1274+
{
1275+
name: "NilMap",
1276+
input: nil,
1277+
expected: "",
1278+
},
1279+
{
1280+
name: "EmptyMap",
1281+
input: map[string]string{},
1282+
expected: "",
1283+
},
1284+
{
1285+
name: "SingleKeyValue",
1286+
input: map[string]string{
1287+
"a": "foo",
1288+
},
1289+
expected: "\"foo\"",
1290+
},
1291+
{
1292+
name: "SingleKeyEmptyValue",
1293+
input: map[string]string{
1294+
"a": "",
1295+
},
1296+
expected: "",
1297+
},
1298+
{
1299+
name: "MultipleKeysSorted",
1300+
input: map[string]string{
1301+
"b": "bar",
1302+
"a": "foo",
1303+
"c": "baz",
1304+
},
1305+
expected: "\"foo\\x1fbar\\x1fbaz\"",
1306+
},
1307+
{
1308+
name: "KeysWithEmptyValue",
1309+
input: map[string]string{
1310+
"a": "",
1311+
"b": "bar",
1312+
},
1313+
expected: "\"bar\"",
1314+
},
1315+
{
1316+
name: "KeysWithInterspersedEmptyValue",
1317+
input: map[string]string{
1318+
"a": "foo",
1319+
"b": "",
1320+
"c": "baz",
1321+
},
1322+
expected: "\"foo\\x1fbaz\"",
1323+
},
1324+
}
1325+
for _, tt := range tests {
1326+
t.Run(tt.name, func(t *testing.T) {
1327+
got := util.ConcatMapValuesSorted(tt.input, "\x1f")
1328+
if got != tt.expected {
1329+
t.Errorf("ConcatMapValuesSorted() = %q, want %q", got, tt.expected)
1330+
}
1331+
})
1332+
}
1333+
}

0 commit comments

Comments
 (0)