-
Notifications
You must be signed in to change notification settings - Fork 0
/
optional.go
58 lines (48 loc) · 1.3 KB
/
optional.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package optional
// Value wraps an optional value of type T
type Value[T any] struct {
value *T
}
// Of instantiates a new optional value with value t
func Of[T any](t T) Value[T] {
return Value[T]{&t}
}
// OfNil instantiates a new optional value of type T with value set to nil
func OfNil[T any]() Value[T] {
return Value[T]{nil}
}
// Set sets the underlying optional value to the provided non-nil value
func (o *Value[T]) Set(value T) {
o.value = &value
}
// SetNil sets the underlying optional value to nil
func (o *Value[T]) SetNil() {
o.value = nil
}
// Get returns true if optional value is non-nil, along with the underlying value
func (o *Value[T]) Get() (ok bool, value T) {
if o.value != nil {
return true, *o.value
}
zeroValue := new(T)
return false, *zeroValue
}
// GetOr returns the underlying non-nil value or the provided fallback value
func (o *Value[T]) GetOr(value T) T {
if o.value == nil {
return value
}
return *o.value
}
// MustGet returns the underlying value or PANICS if value is nil
func (o *Value[T]) MustGet() (value T) {
return *o.value
}
// HasValue returns true if the underlying value is non-nil
func (o *Value[T]) HasValue() bool {
return o.value != nil
}
// IsNil returns true if the underlying value is nil
func (o *Value[T]) IsNil() bool {
return o.value == nil
}