This commit is contained in:
Deluan 2023-05-22 11:59:33 -04:00 • committed by Deluan
commit ea231fe265
8 changed files with 142 additions and 90 deletions

View file

@ -13,20 +13,37 @@ func If[T comparable](v T, orElse T) T {
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.
// P returns a pointer to a value, or nil if the value is the zero value of the type.
func P[T comparable](t T) *T {
var zero T
if t == zero {
return nil
}
return &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 {
// V returns the value of a pointer, or the zero value of the type if the pointer is nil.
func V[T comparable](p *T) T {
var zero T
if p == nil {
return zero
}
return *p
}
// Coalesce returns the first non-zero value from listed arguments.
// Returns the zero value of the type parameter if no arguments are given or all are the zero value.
// Useful when you want to initialize a variable to the first non-zero value from a list of fallback values.
//
// For example:
//
// hostVal := Coalesce(hostName, os.Getenv("HOST"), "localhost")
func Coalesce[T comparable](values ...T) (v T) {
var zero T
for _, v = range values {
if v != zero {
return v
return
}
}
// If all the input values are zero, return the default value.
return or
return
}

View file

@ -43,16 +43,54 @@ var _ = Describe("GG", func() {
)
})
Describe("FirstOr", func() {
Describe("Coalesce", 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"))
Expect(gg.Coalesce("foo", "bar", "baz", "default")).To(Equal("foo"))
Expect(gg.Coalesce("", "", "qux", "default")).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(""))
Expect(gg.Coalesce("", "", "", "default")).To(Equal("default"))
Expect(gg.Coalesce("", "", "", "")).To(Equal(""))
})
})
})
Describe("P", func() {
Context("when given a non-zero value", func() {
It("should return a non-nil pointer to that value", func() {
value := 42
result := gg.P(value)
Expect(result).ToNot(BeNil())
Expect(*result).To(Equal(value))
})
})
Context("when given the zero value of a type", func() {
It("should return nil", func() {
var value string
result := gg.P(value)
Expect(result).To(BeNil())
})
})
})
Describe("V", func() {
Context("when given a non-nil pointer", func() {
It("should return the value of that pointer", func() {
value := 42
pointer := &value
result := gg.V(pointer)
Expect(result).To(Equal(value))
})
})
Context("when given a nil pointer", func() {
It("should return the zero value of the type", func() {
var pointer *string
result := gg.V(pointer)
Expect(result).To(Equal(""))
})
})
})