From bd54b61746285faf3df487278b6ad7b0106a9c07 Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Fri, 19 Jul 2024 15:14:28 -0500 Subject: [PATCH] types/opt: add (Value[T]).GetOr(def T) T method Updates #12736 Signed-off-by: Nick Khyl --- types/opt/value.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/types/opt/value.go b/types/opt/value.go index 1781731a4..54fab7a53 100644 --- a/types/opt/value.go +++ b/types/opt/value.go @@ -64,6 +64,14 @@ func (o Value[T]) Get() T { return o.value } +// GetOr returns the value of o or def if a value hasn't been set. +func (o Value[T]) GetOr(def T) T { + if o.set { + return o.value + } + return def +} + // Get returns the value and a flag indicating whether the value is set. func (o Value[T]) GetOk() (v T, ok bool) { return o.value, o.set