From 69a572a40b27c1f9aa48f07a30261dbcf093428d Mon Sep 17 00:00:00 2001 From: Ruben Hoenle Date: Wed, 12 Nov 2025 15:01:13 +0100 Subject: [PATCH 1/2] feat(core): new util func to convert enum value slices to string slices --- CHANGELOG.md | 7 ++++-- core/CHANGELOG.md | 3 +++ core/VERSION | 2 +- core/utils/utils.go | 14 ++++++++++++ core/utils/utils_test.go | 49 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 200d471a7..71dce2d87 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 `TestEnumSliceToStringSlice` 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..43eb430f6 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,3 +1,6 @@ +## v0.19.0 +- **New:** Added new `TestEnumSliceToStringSlice` 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) + } + }) + } +} From 509a7ce83610ffe8499e7773be40f81f67fbb472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ruben=20H=C3=B6nle?= Date: Wed, 12 Nov 2025 15:12:58 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com> --- CHANGELOG.md | 2 +- core/CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71dce2d87..84d20f08e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## Release (2025-xx-xx) - `core`: - [v0.19.0](core/CHANGELOG.md#v0190) - - **New:** Added new `TestEnumSliceToStringSlice` util func + - **New:** Added new `EnumSliceToStringSlice ` util func - [v0.18.0](core/CHANGELOG.md#v0180) - **New:** Added duration utils - `stackitmarketplace`: diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 43eb430f6..3bea787af 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,5 +1,5 @@ ## v0.19.0 -- **New:** Added new `TestEnumSliceToStringSlice` util func +- **New:** Added new `EnumSliceToStringSlice ` util func ## v0.18.0 - **New:** Added duration utils