feat(types): add Pairs type

This commit is contained in:
Piotr Icikowski 2024-05-21 22:39:52 +02:00
parent 8bd63075f4
commit 658d8a7fd6
Signed by: Piotr Icikowski
GPG Key ID: 3931CA47A91F7666
2 changed files with 7 additions and 4 deletions

View File

@ -6,8 +6,11 @@ type Pair[K comparable, V any] struct {
Value V Value V
} }
// Pairs represents collection of key-value pairs
type Pairs[K comparable, V any] []Pair[K, V]
// MapPairs returns list of map's key-value pairs // MapPairs returns list of map's key-value pairs
func MapPairs[K comparable, V any](src map[K]V) []Pair[K, V] { func MapPairs[K comparable, V any](src map[K]V) Pairs[K, V] {
dst := make([]Pair[K, V], len(src)) dst := make([]Pair[K, V], len(src))
idx := 0 idx := 0
for k, v := range src { for k, v := range src {

View File

@ -13,11 +13,11 @@ func TestMapPairs(t *testing.T) {
}{ }{
"nil map": { "nil map": {
src: nil, src: nil,
dst: []Pair[string, int]{}, dst: Pairs[string, int]{},
}, },
"empty map": { "empty map": {
src: map[string]int{}, src: map[string]int{},
dst: []Pair[string, int]{}, dst: Pairs[string, int]{},
}, },
"filled map": { "filled map": {
src: map[string]int{ src: map[string]int{
@ -25,7 +25,7 @@ func TestMapPairs(t *testing.T) {
"bar": 2, "bar": 2,
"baz": 3, "baz": 3,
}, },
dst: []Pair[string, int]{ dst: Pairs[string, int]{
{ {
Key: "foo", Key: "foo",
Value: 1, Value: 1,