feat: add UnwarpOr and UnwarpOrDefault to pointer pkg (#116)

This commit is contained in:
Ta authored and GitHub committed 2023-07-23 13:09:50 +08:00
1 parent 154ec56780
commit 5caa14c838
5 files changed
+203

No files matched your search

+20
View File
@@ -18,6 +18,26 @@ func Unwrap[T any](p *T) T {
return *p
}
// UnwarpOr returns the value from the pointer or fallback if the pointer is nil.
// Play: https://go.dev/play/p/mmNaLC38W8C
func UnwarpOr[T any](p *T, fallback T) T {
if p == nil {
return fallback
}
return *p
}
// UnwarpOrDefault returns the value from the pointer or the default value if the pointer is nil.
// Play: https://go.dev/play/p/ZnGIHf8_o4E
func UnwarpOrDefault[T any](p *T) T {
var v T
if p == nil {
return v
}
return *p
}
// ExtractPointer returns the underlying value by the given interface type
// Play: https://go.dev/play/p/D7HFjeWU2ZP
func ExtractPointer(value any) any {