-
Notifications
You must be signed in to change notification settings - Fork 8
/
result.go
369 lines (317 loc) · 10.3 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package gust
import (
"errors"
"fmt"
)
// Ret wraps a result.
func Ret[T any](some T, err error) Result[T] {
if err != nil {
return Err[T](err)
}
return Ok(some)
}
// Ok wraps a successful result.
func Ok[T any](ok T) Result[T] {
return Result[T]{inner: EnumOk[T, error](ok)}
}
// Err wraps a failure result.
func Err[T any](err any) Result[T] {
return Result[T]{inner: EnumErr[T, error](toError(err))}
}
// Result can be used to improve `func()(T,error)`,
// represents either success (T) or failure (error).
type Result[T any] struct {
inner EnumResult[T, error]
}
// IsValid returns true if the object is initialized.
func (r *Result[T]) IsValid() bool {
return r != nil && r.inner.IsValid()
}
// IsErr returns true if the result is error.
func (r Result[T]) IsErr() bool {
return r.inner.IsErr()
}
// IsOk returns true if the result is Ok.
func (r Result[T]) IsOk() bool {
return !r.IsErr()
}
// String returns the string representation.
func (r Result[T]) String() string {
return r.inner.String()
}
// Split returns the tuple (T, error).
func (r Result[T]) Split() (T, error) {
return r.inner.Split()
}
// EnumResult returns the inner EnumResult[T, error].
func (r Result[T]) EnumResult() EnumResult[T, error] {
return r.inner
}
// Errable converts from `Result[T]` to `Errable[error]`.
func (r Result[T]) Errable() Errable[error] {
if r.IsErr() {
return ToErrable[error](r.inner.safeGetE())
}
return NonErrable[error]()
}
// IsOkAnd returns true if the result is Ok and the value inside it matches a predicate.
func (r Result[T]) IsOkAnd(f func(T) bool) bool {
return r.inner.IsOkAnd(f)
}
// IsErrAnd returns true if the result is error and the value inside it matches a predicate.
func (r Result[T]) IsErrAnd(f func(error) bool) bool {
return r.inner.IsErrAnd(f)
}
// Ok converts from `Result[T]` to `Option[T]`.
func (r Result[T]) Ok() Option[T] {
return r.inner.Ok()
}
// XOk converts from `Result[T]` to `Option[any]`.
func (r Result[T]) XOk() Option[any] {
return r.inner.XOk()
}
// Err returns error.
func (r Result[T]) Err() error {
if r.IsErr() {
return r.inner.safeGetE()
}
return nil
}
// ErrVal returns error inner value.
func (r Result[T]) ErrVal() any {
if r.IsOk() {
return nil
}
e := r.inner.safeGetE()
if ev, ok := any(e).(*ErrBox); ok {
if ev != nil {
return ev.val
}
return nil
}
return e
}
// ToX converts from `Result[T]` to Result[any].
func (r Result[T]) ToX() Result[any] {
return Result[any]{inner: r.inner.ToXOk()}
}
// Map maps a Result[T] to Result[T] by applying a function to a contained Ok value, leaving an error untouched.
// This function can be used to compose the results of two functions.
func (r Result[T]) Map(f func(T) T) Result[T] {
if r.IsOk() {
return Ok[T](f(r.inner.safeGetT()))
}
return Err[T](r.inner.safeGetE())
}
// XMap maps a Result[T] to Result[any] by applying a function to a contained Ok value, leaving an error untouched.
// This function can be used to compose the results of two functions.
func (r Result[T]) XMap(f func(T) any) Result[any] {
if r.IsOk() {
return Ok[any](f(r.inner.safeGetT()))
}
return Err[any](r.inner.safeGetE())
}
// MapOr returns the provided default (if error), or applies a function to the contained value (if no error),
// Arguments passed to map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use MapOrElse, which is lazily evaluated.
func (r Result[T]) MapOr(defaultOk T, f func(T) T) T {
if r.IsOk() {
return f(r.inner.safeGetT())
}
return defaultOk
}
// XMapOr returns the provided default (if error), or applies a function to the contained value (if no error),
// Arguments passed to map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use MapOrElse, which is lazily evaluated.
func (r Result[T]) XMapOr(defaultOk any, f func(T) any) any {
if r.IsOk() {
return f(r.inner.safeGetT())
}
return defaultOk
}
// MapOrElse maps a Result[T] to T by applying fallback function default to a contained error, or function f to a contained Ok value.
// This function can be used to unpack a successful result while handling an error.
func (r Result[T]) MapOrElse(defaultFn func(error) T, f func(T) T) T {
if r.IsOk() {
return f(r.inner.safeGetT())
}
return defaultFn(r.inner.safeGetE())
}
// XMapOrElse maps a Result[T] to `any` by applying fallback function default to a contained error, or function f to a contained Ok value.
// This function can be used to unpack a successful result while handling an error.
func (r Result[T]) XMapOrElse(defaultFn func(error) any, f func(T) any) any {
if r.IsOk() {
return f(r.inner.safeGetT())
}
return defaultFn(r.inner.safeGetE())
}
// MapErr maps a Result[T] to Result[T] by applying a function to a contained error, leaving an Ok value untouched.
// This function can be used to pass through a successful result while handling an error.
func (r Result[T]) MapErr(op func(error) (newErr any)) Result[T] {
if r.IsErr() {
return Err[T](op(r.inner.safeGetE()))
}
return r
}
// Inspect calls the provided closure with a reference to the contained value (if no error).
func (r Result[T]) Inspect(f func(T)) Result[T] {
if r.IsOk() {
f(r.inner.safeGetT())
}
return r
}
// InspectErr calls the provided closure with a reference to the contained error (if error).
func (r Result[T]) InspectErr(f func(error)) Result[T] {
if r.IsErr() {
f(r.inner.safeGetE())
}
return r
}
// Expect returns the contained Ok value.
// Panics if the value is an error, with a panic message including the
// passed message, and the content of the error.
func (r Result[T]) Expect(msg string) T {
return r.inner.Expect(msg)
}
// Unwrap returns the contained Ok value.
// Because this function may panic, its use is generally discouraged.
// Instead, prefer to use pattern matching and handle the error case explicitly, or call UnwrapOr or UnwrapOrElse.
func (r Result[T]) Unwrap() T {
return r.inner.Unwrap()
}
// UnwrapOrDefault returns the contained T or a non-nil-pointer zero T.
func (r Result[T]) UnwrapOrDefault() T {
return r.inner.UnwrapOrDefault()
}
// ExpectErr returns the contained error.
// Panics if the value is not an error, with a panic message including the
// passed message, and the content of the [`Ok`].
func (r Result[T]) ExpectErr(msg string) error {
return r.inner.ExpectErr(msg)
}
// UnwrapErr returns the contained error.
// Panics if the value is not an error, with a custom panic message provided
// by the [`Ok`]'s value.
func (r Result[T]) UnwrapErr() error {
if r.IsErr() {
return r.inner.safeGetE()
}
panic(ToErrBox(fmt.Sprintf("called `Result.UnwrapErr()` on an `ok` value: %v", r.inner.safeGetT())))
}
// And returns res if the result is Ok, otherwise returns the error of self.
func (r Result[T]) And(res Result[T]) Result[T] {
if r.IsErr() {
return r
}
return res
}
// XAnd returns res if the result is Ok, otherwise returns the error of self.
func (r Result[T]) XAnd(res Result[any]) Result[any] {
if r.IsErr() {
return Err[any](r.inner.safeGetE())
}
return res
}
// AndThen calls op if the result is Ok, otherwise returns the error of self.
// This function can be used for control flow based on Result values.
func (r Result[T]) AndThen(op func(T) Result[T]) Result[T] {
if r.IsErr() {
return r
}
return op(r.inner.safeGetT())
}
// XAndThen calls op if the result is Ok, otherwise returns the error of self.
// This function can be used for control flow based on Result values.
func (r Result[T]) XAndThen(op func(T) Result[any]) Result[any] {
if r.IsErr() {
return Err[any](r.Err())
}
return op(r.inner.safeGetT())
}
// Or returns res if the result is Err, otherwise returns the Ok value of r.
// Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use OrElse, which is lazily evaluated.
func (r Result[T]) Or(res Result[T]) Result[T] {
if r.IsErr() {
return res
}
return r
}
// OrElse calls op if the result is Err, otherwise returns the Ok value of self.
// This function can be used for control flow based on result values.
func (r Result[T]) OrElse(op func(error) Result[T]) Result[T] {
if r.IsErr() {
return op(r.inner.safeGetE())
}
return r
}
// UnwrapOr returns the contained Ok value or a provided default.
// Arguments passed to UnwrapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use UnwrapOrElse, which is lazily evaluated.
func (r Result[T]) UnwrapOr(defaultOk T) T {
return r.inner.UnwrapOr(defaultOk)
}
// UnwrapOrElse returns the contained Ok value or computes it from a closure.
func (r Result[T]) UnwrapOrElse(defaultFn func(error) T) T {
return r.inner.UnwrapOrElse(defaultFn)
}
// ContainsErr returns true if the result is an error containing the given value.
func (r Result[T]) ContainsErr(err any) bool {
if r.IsOk() {
return false
}
return errors.Is(r.inner.safeGetE(), toError(err))
}
// AsPtr returns its pointer or nil.
func (r Result[T]) AsPtr() *T {
return r.inner.AsPtr()
}
func (r Result[T]) MarshalJSON() ([]byte, error) {
return r.inner.MarshalJSON()
}
func (r *Result[T]) UnmarshalJSON(b []byte) error {
return r.inner.UnmarshalJSON(b)
}
var (
_ Iterable[any] = Result[any]{}
_ DeIterable[any] = Result[any]{}
)
func (r Result[T]) Next() Option[T] {
return r.inner.Next()
}
func (r Result[T]) NextBack() Option[T] {
return r.inner.NextBack()
}
func (r Result[T]) Remaining() uint {
return r.inner.Remaining()
}
// CtrlFlow returns the `CtrlFlow[error, T]`.
func (r Result[T]) CtrlFlow() CtrlFlow[error, T] {
if r.IsErr() {
return Break[error, T](r.inner.safeGetE())
}
return Continue[error, T](r.inner.safeGetT())
}
// UnwrapOrThrow returns the contained T or panic returns error (panicValue[*any]).
// NOTE:
//
// If there is an error, that panic should be caught with CatchResult[U]
func (r Result[T]) UnwrapOrThrow() T {
return r.inner.UnwrapOrThrow()
}
// CatchResult catches panic caused by Result[T].UnwrapOrThrow() and sets error to *Result[U]
// Example:
//
// ```go
// func example() (result Result[string]) {
// defer CatchResult(&result)
// Err[int]("int error").UnwrapOrThrow()
// return Ok[string]("ok")
// }
// ```
func CatchResult[U any](result *Result[U]) {
switch p := recover().(type) {
case nil:
case panicValue[*error]:
result.inner.t = None[U]()
result.inner.e = p.value
default:
panic(p)
}
}