Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.19.0
- **New:** Added new `EnumSliceToStringSlice ` util func

## v0.18.0
- **New:** Added duration utils

Expand Down
2 changes: 1 addition & 1 deletion core/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.18.0
v0.19.0
14 changes: 14 additions & 0 deletions core/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
49 changes: 49 additions & 0 deletions core/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -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)
}
})
}
}
Loading