navidrome/utils/gg/gg.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

29 lines
698 B
Go
Raw Normal View History

2023-02-16 02:18:53 +01:00
// Package gg implements simple "extensions" to Go language. Based on https://github.com/icza/gog
package gg
2023-03-27 03:26:55 +02:00
// If returns v if it is a non-zero value, orElse otherwise.
2023-02-16 02:18:53 +01:00
//
// 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.
2023-03-27 03:26:55 +02:00
func If[T comparable](v T, orElse T) T {
2023-02-16 02:18:53 +01:00
var zero T
if v != zero {
return v
}
return orElse
}
2023-03-27 03:26:55 +02:00
// P returns a pointer to the input value
func P[T any](v T) *T {
return &v
}
// V returns the value of the input pointer, or a zero value if the input pointer is nil.
func V[T any](p *T) T {
if p == nil {
var zero T
return zero
}
return *p
}