From 401aad9fa4ff24c5331000a5dec5bbbef4073590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 11 Mar 2024 17:05:10 +0100 Subject: [PATCH] v0.0.409 --- goextVersion.go | 4 +-- langext/coalesce.go | 74 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/goextVersion.go b/goextVersion.go index 5b277d8..12b2ba0 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.408" +const GoextVersion = "0.0.409" -const GoextVersionTimestamp = "2024-03-11T16:41:47+0100" +const GoextVersionTimestamp = "2024-03-11T17:05:10+0100" diff --git a/langext/coalesce.go b/langext/coalesce.go index aa7d7be..f1d649d 100644 --- a/langext/coalesce.go +++ b/langext/coalesce.go @@ -5,12 +5,76 @@ import ( "time" ) -func Coalesce[T any](v *T, def T) T { - if v == nil { - return def - } else { - return *v +func Coalesce[T any](v1 *T, def T) T { + if v1 != nil { + return *v1 } + + return def +} + +func CoalesceOpt[T any](v1 *T, v2 *T) *T { + if v1 != nil { + return v1 + } + + return v2 +} + +func Coalesce3[T any](v1 *T, v2 *T, def T) T { + if v1 != nil { + return *v1 + } + + if v2 != nil { + return *v2 + } + + return def +} + +func Coalesce3Opt[T any](v1 *T, v2 *T, v3 *T) *T { + if v1 != nil { + return v1 + } + + if v2 != nil { + return v2 + } + + return v3 +} + +func Coalesce4[T any](v1 *T, v2 *T, v3 *T, def T) T { + if v1 != nil { + return *v1 + } + + if v2 != nil { + return *v2 + } + + if v3 != nil { + return *v3 + } + + return def +} + +func Coalesce4Opt[T any](v1 *T, v2 *T, v3 *T, v4 *T) *T { + if v1 != nil { + return v1 + } + + if v2 != nil { + return v2 + } + + if v3 != nil { + return v3 + } + + return v4 } func CoalesceString(s *string, def string) string {