diff --git a/CHANGELOG.md b/CHANGELOG.md index 200d471a7..84d20f08e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ## Release (2025-xx-xx) -- `core`: [v0.18.0](core/CHANGELOG.md#v0180) - - **New:** Added duration utils +- `core`: + - [v0.19.0](core/CHANGELOG.md#v0190) + - **New:** Added new `EnumSliceToStringSlice ` util func + - [v0.18.0](core/CHANGELOG.md#v0180) + - **New:** Added duration utils - `stackitmarketplace`: - [v1.17.0](services/stackitmarketplace/CHANGELOG.md#v1170) - **Feature:** Add new field `Scope` in `CatalogProductPricingOption` model diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 84e4ef57c..3bea787af 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,3 +1,6 @@ +## v0.19.0 +- **New:** Added new `EnumSliceToStringSlice ` util func + ## v0.18.0 - **New:** Added duration utils diff --git a/core/VERSION b/core/VERSION index a86d3df72..96fb87f8e 100644 --- a/core/VERSION +++ b/core/VERSION @@ -1 +1 @@ -v0.18.0 +v0.19.0 diff --git a/core/utils/utils.go b/core/utils/utils.go index a0915445a..e36612c0e 100644 --- a/core/utils/utils.go +++ b/core/utils/utils.go @@ -13,3 +13,17 @@ func Contains[T comparable](slice []T, element T) bool { } return false } + +// EnumSliceToStringSlice is a generic function to convert a slice of any type T +// that has the underlying type 'string' to a slice of string. +// The constraint ~string allows T to be any type whose +// underlying type is string (like the enum types from the generated STACKIT SDK modules). +func EnumSliceToStringSlice[T ~string](inputSlice []T) []string { + result := make([]string, len(inputSlice)) + + for i, element := range inputSlice { + result[i] = string(element) + } + + return result +} diff --git a/core/utils/utils_test.go b/core/utils/utils_test.go index b50765983..056f089fb 100644 --- a/core/utils/utils_test.go +++ b/core/utils/utils_test.go @@ -1,6 +1,7 @@ package utils import ( + "reflect" "testing" ) @@ -37,3 +38,51 @@ func TestContainsInt(t *testing.T) { t.Fatalf("Should not be contained") } } + +func TestEnumSliceToStringSlice(t *testing.T) { + type TestEnum string + + const TESTENUM_CREATING TestEnum = "CREATING" + const TESTENUM_ACTIVE TestEnum = "ACTIVE" + const TESTENUM_UPDATING TestEnum = "UPDATING" + const TESTENUM_DELETING TestEnum = "DELETING" + const TESTENUM_ERROR TestEnum = "ERROR" + + type args[T interface{ ~string }] struct { + inputSlice []T + } + type test[T interface{ ~string }] struct { + name string + args args[T] + want []string + } + tests := []test[TestEnum]{ + { + name: "default", + args: args[TestEnum]{ + inputSlice: []TestEnum{ + TESTENUM_CREATING, + TESTENUM_ACTIVE, + TESTENUM_UPDATING, + TESTENUM_DELETING, + TESTENUM_ERROR, + }, + }, + want: []string{"CREATING", "ACTIVE", "UPDATING", "DELETING", "ERROR"}, + }, + { + name: "empty input slice", + args: args[TestEnum]{ + inputSlice: []TestEnum{}, + }, + want: []string{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := EnumSliceToStringSlice(tt.args.inputSlice); !reflect.DeepEqual(got, tt.want) { + t.Errorf("EnumSliceToStringSlice() = %v, want %v", got, tt.want) + } + }) + } +}