Replace gg.If with cmp.Or

This commit is contained in:
Deluan 2024-06-05 22:48:00 -04:00
parent 11bef060a3
commit b4ef1b1e38
5 changed files with 6 additions and 47 deletions

View file

@ -1,18 +1,6 @@
// Package gg implements simple "extensions" to Go language. Based on https://github.com/icza/gog
package gg
// If returns v if it is a non-zero value, orElse otherwise.
//
// This is similar to elvis operator (?:) in Groovy and other languages.
// Note: Different from the real elvis operator, the orElse expression will always get evaluated.
func If[T comparable](v T, orElse T) T {
var zero T
if v != zero {
return v
}
return orElse
}
// P returns a pointer to the input value
func P[T any](v T) *T {
return &v

View file

@ -16,33 +16,6 @@ func TestGG(t *testing.T) {
}
var _ = Describe("GG", func() {
Describe("If", func() {
DescribeTable("string",
func(v, orElse, expected string) {
Expect(gg.If(v, orElse)).To(Equal(expected))
},
Entry("zero value", "", "default", "default"),
Entry("non-zero value", "anything", "default", "anything"),
)
DescribeTable("numeric",
func(v, orElse, expected int) {
Expect(gg.If(v, orElse)).To(Equal(expected))
},
Entry("zero value", 0, 2, 2),
Entry("non-zero value", -1, 2, -1),
)
type testStruct struct {
field1 int
}
DescribeTable("struct",
func(v, orElse, expected testStruct) {
Expect(gg.If(v, orElse)).To(Equal(expected))
},
Entry("zero value", testStruct{}, testStruct{123}, testStruct{123}),
Entry("non-zero value", testStruct{456}, testStruct{123}, testStruct{456}),
)
})
Describe("P", func() {
It("returns a pointer to the input value", func() {
v := 123