CloudMigrations: wire ngalert to cloud migration service and add slicesext.Map helper (#94254)
* CloudMigrations: add slicesext.Map function to simplify dto creation * CloudMigrations: wire ngalert to cloud migration service
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package slicesext
|
||||
|
||||
func Map[T any, U any](xs []T, f func(T) U) []U {
|
||||
out := make([]U, 0, len(xs))
|
||||
|
||||
for _, x := range xs {
|
||||
out = append(out, f(x))
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package slicesext_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/cloudmigration/slicesext"
|
||||
)
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("mapping a nil slice does nothing and returns an empty slice", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
require.Empty(t, slicesext.Map[any, any](nil, nil))
|
||||
})
|
||||
|
||||
t.Run("mapping a non-nil slice with a nil function panics", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
require.Panics(t, func() { slicesext.Map[int, any]([]int{1, 2, 3}, nil) })
|
||||
})
|
||||
|
||||
t.Run("mapping a non-nil slice with a non-nil function returns the mapped slice", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
original := []int{1, 2, 3}
|
||||
expected := []string{"1", "2", "3"}
|
||||
fn := func(i int) string { return strconv.Itoa(i) }
|
||||
|
||||
require.ElementsMatch(t, expected, slicesext.Map(original, fn))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user