-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
150 lines (120 loc) · 3.13 KB
/
result.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package result
import "fmt"
// Result is a type that represents either a value of type T or an error.
type Result[T any] struct {
value T
err error
}
// Ok creates a new Result[T] with the given value.
func Ok[T any](value T) Result[T] {
return Result[T]{value: value}
}
// Err creates a new Result[T] with the given error.
func Err[T any](err error) Result[T] {
return Result[T]{err: err}
}
// IsOk returns true if the result is ok.
func (r *Result[T]) IsOk() bool {
return r.err == nil
}
// IsErr returns true if the result is an error.
func (r *Result[T]) IsErr() bool {
return r.err != nil
}
// IsOkAnd returns true if the result is ok and the value is equal to the given
// value.
func (r *Result[T]) IsOkAnd(f func(T) bool) bool {
return r.IsOk() && f(r.value)
}
// IsErrAnd returns true if the result is an error and the error satisfies the
// given predicate.
func (r *Result[T]) IsErrAnd(f func(error) bool) bool {
return r.IsErr() && f(r.err)
}
// Ok returns the value if the result is ok, otherwise panics.
func (r *Result[T]) Ok() *T {
if r.IsErr() {
panic(r.err)
}
return &r.value
}
// Err returns the error if the result is an error, otherwise panics.
func (r *Result[T]) Err() *error {
if r.IsOk() {
panic("result is ok")
}
return &r.err
}
// Map maps the value if the result is ok, otherwise returns the error.
// FIXME
func (r *Result[T]) Map(f func(*T)) error {
if r.IsErr() {
return r.err
}
f(&r.value)
return nil
}
// MapOr maps the value if the result is ok, otherwise returns the given value.
func (r *Result[T]) MapOr(f func(T) T, or T) T {
if r.IsErr() {
return or
}
return f(r.value)
}
// MapOrElse maps the value if the result is ok, otherwise returns the value
// returned by the given function.
func (r *Result[T]) MapOrElse(f func(T) T, or func() T) T {
if r.IsErr() {
return or()
}
return f(r.value)
}
// MapErr maps the error if the result is an error, otherwise returns the value.
func (r *Result[T]) MapErr(f func(error) error) *Result[T] {
if r.IsOk() {
return r
}
return &Result[T]{err: f(r.err)}
}
// Inspect calls the given function with the value if the result is ok, otherwise
// returns the error.
func (r *Result[T]) Inspect(f func(T)) *Result[T] {
if r.IsErr() {
return r
}
f(r.value)
return r
}
// InspectErr calls the given function with the error if the result is an error,
// otherwise returns the value.
func (r *Result[T]) InspectErr(f func(error)) *Result[T] {
if r.IsOk() {
return r
}
f(r.err)
return r
}
// Expect returns the value if the result is ok, otherwise panics with the given
// message.
func (r *Result[T]) Expect(msg string) T {
if r.IsErr() {
panic(msg)
}
return r.value
}
// Unwrap returns the value if the result is ok, otherwise panics.
func (r *Result[T]) Unwrap() T {
if r.IsErr() {
msg := fmt.Sprintf("called `Result[T].Unwrap()` on an `Err` value: %v", r.err)
panic(msg)
}
return r.value
}
// UnwrapErr returns the error if the result is an error, otherwise panics.
func (r *Result[T]) UnwrapErr() error {
if r.IsOk() {
msg := fmt.Sprintf("called `Result[T].UnwrapErr()` on an `Ok` value: %v", r.value)
panic(msg)
}
return r.err
}