Compare commits

..

No commits in common. "v1.2.0-rc.2" and "v1.2.0-rc.1" have entirely different histories.

9 changed files with 20 additions and 149 deletions

View File

@ -1,15 +1,3 @@
## [1.2.0-rc.2](https://git.ext.icikowski.pl/go/generics/compare/v1.2.0-rc.1...v1.2.0-rc.2) (2024-05-27)
### Features
* **pairs:** add `Pairs`-related methods ([057f12e](https://git.ext.icikowski.pl/go/generics/commit/057f12e602cda3282bd3aa53590a4bd120789f3e))
### Refactoring
* **comments:** beautify comments ([73328b0](https://git.ext.icikowski.pl/go/generics/commit/73328b0ef77efe3bc873b643e1464a84faf64e54))
## [1.2.0-rc.1](https://git.ext.icikowski.pl/go/generics/compare/v1.1.1-rc.9...v1.2.0-rc.1) (2024-05-21) ## [1.2.0-rc.1](https://git.ext.icikowski.pl/go/generics/compare/v1.1.1-rc.9...v1.2.0-rc.1) (2024-05-21)

View File

@ -1,5 +0,0 @@
# `generics`
[![Go Report Card](https://goreportcard.com/badge/pkg.icikowski.pl/generics)](https://goreportcard.com/report/pkg.icikowski.pl/generics)
Collection of helpers and functions using generics.

17
maps.go
View File

@ -1,6 +1,15 @@
package generics package generics
// MapPairs returns list of map's key-value pairs. // 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] { 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
@ -14,7 +23,7 @@ func MapPairs[K comparable, V any](src map[K]V) Pairs[K, V] {
return dst return dst
} }
// MapKeys returns list of map's keys. // MapKeys returns list of map's keys
func MapKeys[K comparable, V any](src map[K]V) []K { func MapKeys[K comparable, V any](src map[K]V) []K {
dst := make([]K, len(src)) dst := make([]K, len(src))
idx := 0 idx := 0
@ -25,7 +34,7 @@ func MapKeys[K comparable, V any](src map[K]V) []K {
return dst return dst
} }
// MapValues returns list of map's values. // MapValues returns list of map's values
func MapValues[K comparable, V any](src map[K]V) []V { func MapValues[K comparable, V any](src map[K]V) []V {
dst := make([]V, len(src)) dst := make([]V, len(src))
idx := 0 idx := 0
@ -36,7 +45,7 @@ func MapValues[K comparable, V any](src map[K]V) []V {
return dst return dst
} }
// InvertMap returns map with keys and values swapped. // InvertMap returns map with keys and values swapped
func InvertMap[K, V comparable](src map[K]V) map[V]K { func InvertMap[K, V comparable](src map[K]V) map[V]K {
if src == nil { if src == nil {
return nil return nil

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "generics", "name": "generics",
"version": "1.2.0-rc.2", "version": "1.2.0-rc.1",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "generics", "name": "generics",
"version": "1.2.0-rc.2", "version": "1.2.0-rc.1",
"devDependencies": { "devDependencies": {
"@saithodev/semantic-release-gitea": "^2.1.0", "@saithodev/semantic-release-gitea": "^2.1.0",
"@semantic-release/changelog": "^6.0.3", "@semantic-release/changelog": "^6.0.3",

View File

@ -1,4 +1,4 @@
/* /*
Collection of helpers and functions using generics. Generics-related functions
*/ */
package generics package generics

View File

@ -1,7 +1,7 @@
{ {
"private": true, "private": true,
"name": "generics", "name": "generics",
"version": "1.2.0-rc.2", "version": "1.2.0-rc.1",
"scripts": { "scripts": {
"release": "./node_modules/.bin/semantic-release" "release": "./node_modules/.bin/semantic-release"
}, },

View File

@ -1,28 +0,0 @@
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
}

View File

@ -1,93 +0,0 @@
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)
})
}
}

6
ptr.go
View File

@ -1,11 +1,11 @@
package generics package generics
// Ptr returns pointer for given value. // Ptr returns pointer for given value
func Ptr[T any](val T) *T { func Ptr[T any](val T) *T {
return &val return &val
} }
// Val returns value of given pointer or default value if pointer is nil. // Val returns value of given pointer or default value if pointer is nil
func Val[T any](ptr *T) T { func Val[T any](ptr *T) T {
var val T var val T
if ptr != nil { if ptr != nil {
@ -14,7 +14,7 @@ func Val[T any](ptr *T) T {
return val return val
} }
// Fallback returns value of given pointer or fallback value if pointer is nil. // Fallback returns value of given pointer or fallback value if pointer is nil
func Fallback[T any](ptr *T, fallback T) T { func Fallback[T any](ptr *T, fallback T) T {
if ptr != nil { if ptr != nil {
return *ptr return *ptr