From 057f12e602cda3282bd3aa53590a4bd120789f3e Mon Sep 17 00:00:00 2001 From: Piotr Icikowski Date: Tue, 28 May 2024 00:19:20 +0200 Subject: [PATCH] feat(pairs): add `Pairs`-related methods --- maps.go | 9 ----- pairs.go | 28 ++++++++++++++++ pairs_test.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 pairs.go create mode 100644 pairs_test.go diff --git a/maps.go b/maps.go index f4533c0..cddd4a4 100644 --- a/maps.go +++ b/maps.go @@ -1,14 +1,5 @@ package generics -// Pair represents key-value pair -type Pair[K comparable, V any] struct { - Key K - 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 func MapPairs[K comparable, V any](src map[K]V) Pairs[K, V] { dst := make([]Pair[K, V], len(src)) diff --git a/pairs.go b/pairs.go new file mode 100644 index 0000000..9f3b558 --- /dev/null +++ b/pairs.go @@ -0,0 +1,28 @@ +package generics + +// Pair represents key-value pair. +type Pair[K comparable, V any] struct { + Key K + Value V +} + +// Pairs represents a collection of key-value pairs. +type Pairs[K comparable, V any] []Pair[K, V] + +// Keys returns list of key-value pairs' keys. +func (p Pairs[K, V]) Keys() []K { + dst := make([]K, len(p)) + for i, pair := range p { + dst[i] = pair.Key + } + return dst +} + +// Values returns list of key-value pairs' values. +func (p Pairs[K, V]) Values() []V { + dst := make([]V, len(p)) + for i, pair := range p { + dst[i] = pair.Value + } + return dst +} diff --git a/pairs_test.go b/pairs_test.go new file mode 100644 index 0000000..1f914fc --- /dev/null +++ b/pairs_test.go @@ -0,0 +1,93 @@ +package generics + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPairsKeys(t *testing.T) { + tests := map[string]struct { + src Pairs[string, int] + dst []string + }{ + "nil pairs": { + src: nil, + dst: []string{}, + }, + "empty pairs": { + src: Pairs[string, int]{}, + dst: []string{}, + }, + "filled pairs": { + src: Pairs[string, int]{ + { + Key: "foo", + Value: 1, + }, + { + Key: "bar", + Value: 2, + }, + { + Key: "baz", + Value: 3, + }, + }, + dst: []string{ + "foo", + "bar", + "baz", + }, + }, + } + + for name, tc := range tests { + name, tc := name, tc + t.Run(name, func(t *testing.T) { + dst := tc.src.Keys() + require.ElementsMatch(t, tc.dst, dst) + }) + } +} + +func TestPairsValues(t *testing.T) { + tests := map[string]struct { + src Pairs[string, int] + dst []int + }{ + "nil pairs": { + src: nil, + dst: []int{}, + }, + "empty pairs": { + src: Pairs[string, int]{}, + dst: []int{}, + }, + "filled pairs": { + src: Pairs[string, int]{ + { + Key: "foo", + Value: 1, + }, + { + Key: "bar", + Value: 2, + }, + { + Key: "baz", + Value: 3, + }, + }, + dst: []int{1, 2, 3}, + }, + } + + for name, tc := range tests { + name, tc := name, tc + t.Run(name, func(t *testing.T) { + dst := tc.src.Values() + require.ElementsMatch(t, tc.dst, dst) + }) + } +}