-
Notifications
You must be signed in to change notification settings - Fork 1
/
opt.go
75 lines (63 loc) · 1.96 KB
/
opt.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package opt
import (
"errors"
)
// ErrEmpty is a constant error value used to signify when an Opt is empty.
var ErrEmpty = errors.New("empty")
// Opt is a light wrapper around a value or an error.
// Opts should always be passed by value. If you see a pointer to an Opt anywhere something is wrong.
//
// The zero Opt structure holds the default value for T and no error, and is *not* considered empty.
type Opt[T any] struct {
val T
err error
}
// Map applies a function to the value of the Opt, unless the Opt is empty.
// If you need to change the type inside the Opt you will have to use Map.
func (o Opt[T]) Map(f func(T) T) Opt[T] {
if o.Empty() {
return o
}
return Of(f(o.val))
}
// Must returns the value wrapped by this opt or panics if there isn't one.
// It should be considered a last resort to use this function. It is almost always
// better to use Or, OnErr, or Return instead.
func (o Opt[T]) Must() T {
if o.err != nil {
panic(o.err)
}
return o.val
}
// Error returns the error, if any, held by this opt.
func (o Opt[T]) Error() error {
return o.err
}
// OnErr calls a function if the opt is an error or returns the value directly.
// The function returns a default value that will be returned from OnErr.
func (o Opt[T]) OnErr(errFn func(err error) T) T {
if o.err != nil && o.err != ErrEmpty {
return errFn(o.err)
}
return o.val
}
// Return unpacks the opt into a standard (value, error) pair.
func (o Opt[T]) Return() (T, error) {
return o.val, o.err
}
// Empty returns true if the option holds ErrEmpty or any other error.
func (o Opt[T]) Empty() bool {
return o.err != nil
}
// Ok returns true if there is no error associated with this opt.
// It is guaranteed to be valid to call Opt.Must() if Opt.Ok() returns true.
func (o Opt[T]) Ok() bool {
return o.err == nil
}
// Or returns the value held by this opt or an alternate value if it is empty or an error.
func (o Opt[T]) Or(altValue T) T {
if o.Empty() {
return altValue
}
return o.val
}