types/opt: add (Value[T]).GetOr(def T) T method

Updates #12736

Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
Nick Khyl 2024-07-19 15:14:28 -05:00 committed by Nick Khyl
parent 20562a4fb9
commit bd54b61746

View File

@ -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