Skip to content
Snippets Groups Projects
Commit cde2c1e7 authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

Fix SliceToMap function

parent 7e3569b1
No related tags found
No related merge requests found
......@@ -193,12 +193,12 @@ func CloneSlice[T interface{ Clone() T }](s []T) []T {
return result
}
type Keyed[T any] interface {
type Keyed[T comparable] interface {
Key() T
}
func SliceToMap[T Keyed[any]](s []T) map[any]T {
res := make(map[any]T, len(s))
func SliceToMap[K comparable, T Keyed[K]](s []T) map[K]T {
res := make(map[K]T, len(s))
for _, elem := range s {
res[elem.Key()] = elem
}
......
......@@ -139,3 +139,18 @@ keyB2: val20
})
}
}
type KV struct {
K string
V string
}
func (kv *KV) Key() string {
return kv.K
}
func TestSliceToMap(t *testing.T) {
s := []*KV{{"a", "1"}, {"b", "2"}, {"c", "3"}}
m := SliceToMap(s)
assert.Equal(t, map[string]*KV{"a": {"a", "1"}, "b": {"b", "2"}, "c": {"c", "3"}}, m)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment