Some refactorings
This commit is contained in:
+20
-2
@@ -1,14 +1,32 @@
|
||||
// Package gg implements simple "extensions" to Go language. Based on https://github.com/icza/gog
|
||||
package gg
|
||||
|
||||
// IfZero returns v if it is a non-zero value, orElse otherwise.
|
||||
// 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 IfZero[T comparable](v T, orElse T) T {
|
||||
func If[T comparable](v T, orElse T) T {
|
||||
var zero T
|
||||
if v != zero {
|
||||
return v
|
||||
}
|
||||
return orElse
|
||||
}
|
||||
|
||||
// FirstOr is a generic helper function that returns the first non-zero value from
|
||||
// a list of comparable values, or a default value if all the values are zero.
|
||||
func FirstOr[T comparable](or T, values ...T) T {
|
||||
// Initialize a zero value of the same type as the input values.
|
||||
var zero T
|
||||
|
||||
// Loop through each input value and check if it is non-zero. If a non-zero value
|
||||
// is found, return it immediately.
|
||||
for _, v := range values {
|
||||
if v != zero {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
// If all the input values are zero, return the default value.
|
||||
return or
|
||||
}
|
||||
|
||||
+41
-25
@@ -15,29 +15,45 @@ func TestGG(t *testing.T) {
|
||||
RunSpecs(t, "GG Suite")
|
||||
}
|
||||
|
||||
var _ = Describe("IfZero", func() {
|
||||
DescribeTable("string",
|
||||
func(v, orElse, expected string) {
|
||||
Expect(gg.IfZero(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.IfZero(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.IfZero(v, orElse)).To(Equal(expected))
|
||||
},
|
||||
Entry("zero value", testStruct{}, testStruct{123}, testStruct{123}),
|
||||
Entry("non-zero value", testStruct{456}, testStruct{123}, testStruct{456}),
|
||||
)
|
||||
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("FirstOr", func() {
|
||||
Context("when given a list of strings", func() {
|
||||
It("returns the first non-empty value", func() {
|
||||
Expect(gg.FirstOr("default", "foo", "bar", "baz")).To(Equal("foo"))
|
||||
Expect(gg.FirstOr("default", "", "", "qux")).To(Equal("qux"))
|
||||
})
|
||||
|
||||
It("returns the default value if all values are empty", func() {
|
||||
Expect(gg.FirstOr("default", "", "", "")).To(Equal("default"))
|
||||
Expect(gg.FirstOr("", "", "", "")).To(Equal(""))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user