Skip to content

Commit ce14ccb

Browse files
authored
feat(core): new util func to convert enum value slices to string slices (#3714)
1 parent b88f351 commit ce14ccb

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## Release (2025-xx-xx)
2-
- `core`: [v0.18.0](core/CHANGELOG.md#v0180)
3-
- **New:** Added duration utils
2+
- `core`:
3+
- [v0.19.0](core/CHANGELOG.md#v0190)
4+
- **New:** Added new `EnumSliceToStringSlice ` util func
5+
- [v0.18.0](core/CHANGELOG.md#v0180)
6+
- **New:** Added duration utils
47
- `stackitmarketplace`:
58
- [v1.17.0](services/stackitmarketplace/CHANGELOG.md#v1170)
69
- **Feature:** Add new field `Scope` in `CatalogProductPricingOption` model

core/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.19.0
2+
- **New:** Added new `EnumSliceToStringSlice ` util func
3+
14
## v0.18.0
25
- **New:** Added duration utils
36

core/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.18.0
1+
v0.19.0

core/utils/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ func Contains[T comparable](slice []T, element T) bool {
1313
}
1414
return false
1515
}
16+
17+
// EnumSliceToStringSlice is a generic function to convert a slice of any type T
18+
// that has the underlying type 'string' to a slice of string.
19+
// The constraint ~string allows T to be any type whose
20+
// underlying type is string (like the enum types from the generated STACKIT SDK modules).
21+
func EnumSliceToStringSlice[T ~string](inputSlice []T) []string {
22+
result := make([]string, len(inputSlice))
23+
24+
for i, element := range inputSlice {
25+
result[i] = string(element)
26+
}
27+
28+
return result
29+
}

core/utils/utils_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"reflect"
45
"testing"
56
)
67

@@ -37,3 +38,51 @@ func TestContainsInt(t *testing.T) {
3738
t.Fatalf("Should not be contained")
3839
}
3940
}
41+
42+
func TestEnumSliceToStringSlice(t *testing.T) {
43+
type TestEnum string
44+
45+
const TESTENUM_CREATING TestEnum = "CREATING"
46+
const TESTENUM_ACTIVE TestEnum = "ACTIVE"
47+
const TESTENUM_UPDATING TestEnum = "UPDATING"
48+
const TESTENUM_DELETING TestEnum = "DELETING"
49+
const TESTENUM_ERROR TestEnum = "ERROR"
50+
51+
type args[T interface{ ~string }] struct {
52+
inputSlice []T
53+
}
54+
type test[T interface{ ~string }] struct {
55+
name string
56+
args args[T]
57+
want []string
58+
}
59+
tests := []test[TestEnum]{
60+
{
61+
name: "default",
62+
args: args[TestEnum]{
63+
inputSlice: []TestEnum{
64+
TESTENUM_CREATING,
65+
TESTENUM_ACTIVE,
66+
TESTENUM_UPDATING,
67+
TESTENUM_DELETING,
68+
TESTENUM_ERROR,
69+
},
70+
},
71+
want: []string{"CREATING", "ACTIVE", "UPDATING", "DELETING", "ERROR"},
72+
},
73+
{
74+
name: "empty input slice",
75+
args: args[TestEnum]{
76+
inputSlice: []TestEnum{},
77+
},
78+
want: []string{},
79+
},
80+
}
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
if got := EnumSliceToStringSlice(tt.args.inputSlice); !reflect.DeepEqual(got, tt.want) {
84+
t.Errorf("EnumSliceToStringSlice() = %v, want %v", got, tt.want)
85+
}
86+
})
87+
}
88+
}

0 commit comments

Comments
 (0)