-
Notifications
You must be signed in to change notification settings - Fork 8
/
option.go
542 lines (480 loc) · 12.6 KB
/
option.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package gust
import (
"encoding/json"
"fmt"
"unsafe"
)
// BoolOpt wraps a value as an Option.
// NOTE:
//
// `ok=true` is wrapped as Some,
// and `ok=false` is wrapped as None.
func BoolOpt[T any](val T, ok bool) Option[T] {
if !ok {
return Option[T]{value: nil}
}
v := &val
return Option[T]{value: &v}
}
// AssertOpt wraps a value as an Option.
func AssertOpt[T any](i any) Option[T] {
val, ok := i.(T)
if !ok {
return Option[T]{value: nil}
}
v := &val
return Option[T]{value: &v}
}
// BoolAssertOpt wraps a value as an Option.
func BoolAssertOpt[T any](i any, ok bool) Option[T] {
if ok {
if val, ok2 := i.(T); ok2 {
v := &val
return Option[T]{value: &v}
}
}
return Option[T]{value: nil}
}
// PtrOpt wraps a pointer value.
// NOTE:
//
// `non-nil pointer` is wrapped as Some,
// and `nil pointer` is wrapped as None.
func PtrOpt[U any, T *U](ptr T) Option[T] {
if ptr == nil {
return Option[T]{value: nil}
}
v := &ptr
return Option[T]{value: &v}
}
// ElemOpt wraps a value from pointer.
// NOTE:
//
// `non-nil pointer` is wrapped as Some,
// and `nil pointer` is wrapped as None.
func ElemOpt[T any](ptr *T) Option[T] {
if ptr == nil {
return Option[T]{value: nil}
}
return Option[T]{value: &ptr}
}
// ZeroOpt wraps a value as an Option.
// NOTE:
//
// `non-zero T` is wrapped as Some,
// and `zero T` is wrapped as None.
func ZeroOpt[T comparable](val T) Option[T] {
var zero T
if zero == val {
return Option[T]{value: nil}
}
v := &val
return Option[T]{value: &v}
}
// Some wraps a non-none value.
// NOTE:
//
// Option[T].IsSome() returns true.
// and Option[T].IsNone() returns false.
func Some[T any](value T) Option[T] {
v := &value
return Option[T]{value: &v}
}
// None returns a none.
// NOTE:
//
// Option[T].IsNone() returns true,
// and Option[T].IsSome() returns false.
func None[T any]() Option[T] {
return Option[T]{value: nil}
}
// Option can be used to avoid `(T, bool)` and `if *U != nil`,
// represents an optional value:
//
// every [`Option`] is either [`Some`](which is non-none T), or [`None`](which is none).
type Option[T any] struct {
value **T
}
// String returns the string representation.
func (o Option[T]) String() string {
if o.IsNone() {
return "None"
}
return fmt.Sprintf("Some(%v)", o.UnwrapUnchecked())
}
// Split returns the tuple (T, bool).
func (o Option[T]) Split() (T, bool) {
if o.IsSome() {
return o.UnwrapUnchecked(), true
}
var t T
return t, false
}
// ToX converts to `Option[any]`.
func (o Option[T]) ToX() Option[any] {
if o.IsNone() {
return None[any]()
}
return Some[any](o.UnwrapUnchecked())
}
// IsSome returns `true` if the option has value.
func (o Option[T]) IsSome() bool {
return !o.IsNone()
}
// IsSomeAnd returns `true` if the option has value and the value inside it matches a predicate.
func (o Option[T]) IsSomeAnd(f func(T) bool) bool {
if o.IsSome() {
return f(o.UnwrapUnchecked())
}
return false
}
// IsNone returns `true` if the option is none.
func (o Option[T]) IsNone() bool {
return o.value == nil || *o.value == nil
}
// Expect returns the contained [`Some`] value.
// Panics if the value is none with a custom panic message provided by `msg`.
func (o Option[T]) Expect(msg string) T {
if o.IsNone() {
panic(ToErrBox(msg))
}
return o.UnwrapUnchecked()
}
// Unwrap returns the contained value.
// Panics if the value is none.
func (o Option[T]) Unwrap() T {
if o.IsSome() {
return o.UnwrapUnchecked()
}
var t T
panic(ToErrBox(fmt.Sprintf("call Option[%T].Unwrap() on none", t)))
}
// UnwrapOr returns the contained value or a provided fallback value.
func (o Option[T]) UnwrapOr(fallbackValue T) T {
if o.IsSome() {
return o.UnwrapUnchecked()
}
return fallbackValue
}
// UnwrapOrElse returns the contained value or computes it from a closure.
func (o Option[T]) UnwrapOrElse(defaultSome func() T) T {
if o.IsSome() {
return o.UnwrapUnchecked()
}
return defaultSome()
}
// UnwrapOrDefault returns the contained value or a non-nil-pointer zero value.
func (o Option[T]) UnwrapOrDefault() T {
if o.IsSome() {
return o.UnwrapUnchecked()
}
return defaultValue[T]()
}
// Take takes the value out of the option, leaving a [`None`] in its place.
func (o Option[T]) Take() Option[T] {
if o.IsNone() {
return None[T]()
}
v := *o.value
*o.value = nil
return Option[T]{value: &v}
}
// UnwrapUnchecked returns the contained value.
func (o Option[T]) UnwrapUnchecked() T {
return **o.value
}
// Map maps an `Option[T]` to `Option[T]` by applying a function to a contained value.
func (o Option[T]) Map(f func(T) T) Option[T] {
if o.IsSome() {
return Some[T](f(o.UnwrapUnchecked()))
}
return None[T]()
}
// XMap maps an `Option[T]` to `Option[any]` by applying a function to a contained value.
func (o Option[T]) XMap(f func(T) any) Option[any] {
if o.IsSome() {
return Some[any](f(o.UnwrapUnchecked()))
}
return None[any]()
}
// Inspect calls the provided closure with a reference to the contained value (if it has value).
func (o Option[T]) Inspect(f func(T)) Option[T] {
if o.IsSome() {
f(o.UnwrapUnchecked())
}
return o
}
// InspectNone calls the provided closure (if it is none).
func (o Option[T]) InspectNone(f func()) Option[T] {
if o.IsNone() {
f()
}
return o
}
// MapOr returns the provided default value (if none),
// or applies a function to the contained value (if any).
func (o Option[T]) MapOr(defaultSome T, f func(T) T) T {
if o.IsSome() {
return f(o.UnwrapUnchecked())
}
return defaultSome
}
// XMapOr returns the provided default value (if none),
// or applies a function to the contained value (if any).
func (o Option[T]) XMapOr(defaultSome any, f func(T) any) any {
if o.IsSome() {
return f(o.UnwrapUnchecked())
}
return defaultSome
}
// MapOrElse computes a default function value (if none), or
// applies a different function to the contained value (if any).
func (o Option[T]) MapOrElse(defaultFn func() T, f func(T) T) T {
if o.IsSome() {
return f(o.UnwrapUnchecked())
}
return defaultFn()
}
// XMapOrElse computes a default function value (if none), or
// applies a different function to the contained value (if any).
func (o Option[T]) XMapOrElse(defaultFn func() any, f func(T) any) any {
if o.IsSome() {
return f(o.UnwrapUnchecked())
}
return defaultFn()
}
// OkOr transforms the `Option[T]` into a [`Result[T]`], mapping [`Some(v)`] to
// [`Ok(v)`] and [`None`] to [`Err(err)`].
func (o Option[T]) OkOr(err any) Result[T] {
if o.IsSome() {
return Result[T]{inner: EnumResult[T, error]{t: o}}
}
return Err[T](err)
}
// XOkOr transforms the `Option[T]` into a [`Result[any]`], mapping [`Some(v)`] to
// [`Ok(v)`] and [`None`] to [`Err(err)`].
func (o Option[T]) XOkOr(err any) Result[any] {
if o.IsSome() {
return Ok[any](o.UnwrapUnchecked())
}
return Err[any](err)
}
// OkOrElse transforms the `Option[T]` into a [`Result[T]`], mapping [`Some(v)`] to
// [`Ok(v)`] and [`None`] to [`Err(errFn())`].
func (o Option[T]) OkOrElse(errFn func() any) Result[T] {
if o.IsSome() {
return Result[T]{inner: EnumResult[T, error]{t: o}}
}
return Err[T](errFn())
}
// XOkOrElse transforms the `Option[T]` into a [`Result[any]`], mapping [`Some(v)`] to
// [`Ok(v)`] and [`None`] to [`Err(errFn())`].
func (o Option[T]) XOkOrElse(errFn func() any) Result[any] {
if o.IsSome() {
return Ok[any](o.UnwrapUnchecked())
}
return Err[any](errFn())
}
// EnumOkOr transforms the `Option[T]` into a [`EnumResult[T,any]`], mapping [`Some(v)`] to
// [`EnumOk(v)`] and [`None`] to [`EnumErr(err)`].
func (o Option[T]) EnumOkOr(err any) EnumResult[T, any] {
if o.IsSome() {
return EnumResult[T, any]{t: o}
}
return EnumErr[T, any](err)
}
// XEnumOkOr transforms the `Option[T]` into a [`EnumResult[any,any]`], mapping [`Some(v)`] to
// [`EnumOk(v)`] and [`None`] to [`EnumErr(err)`].
func (o Option[T]) XEnumOkOr(err any) EnumResult[any, any] {
if o.IsSome() {
return EnumOk[any, any](o.UnwrapUnchecked())
}
return EnumErr[any, any](err)
}
// EnumOkOrElse transforms the `Option[T]` into a [`EnumResult[T,any]`], mapping [`Some(v)`] to
// [`EnumOk(v)`] and [`None`] to [`EnumErr(errFn())`].
func (o Option[T]) EnumOkOrElse(errFn func() any) EnumResult[T, any] {
if o.IsSome() {
return EnumResult[T, any]{t: o}
}
return EnumErr[T, any](errFn())
}
// XEnumOkOrElse transforms the `Option[T]` into a [`EnumResult[any,any]`], mapping [`Some(v)`] to
// [`EnumOk(v)`] and [`None`] to [`EnumErr(errFn())`].
func (o Option[T]) XEnumOkOrElse(errFn func() any) EnumResult[any, any] {
if o.IsSome() {
return EnumOk[any, any](o.UnwrapUnchecked())
}
return EnumErr[any, any](errFn())
}
// AndThen returns [`None`] if the option is [`None`], otherwise calls `f` with the
func (o Option[T]) AndThen(f func(T) Option[T]) Option[T] {
if o.IsNone() {
return o
}
return f(o.UnwrapUnchecked())
}
// XAndThen returns [`None`] if the option is [`None`], otherwise calls `f` with the
func (o Option[T]) XAndThen(f func(T) Option[any]) Option[any] {
if o.IsNone() {
return None[any]()
}
return f(o.UnwrapUnchecked())
}
// OrElse returns the option if it contains a value, otherwise calls `f` and returns the result.
func (o Option[T]) OrElse(f func() Option[T]) Option[T] {
if o.IsNone() {
return f()
}
return o
}
// Filter returns [`None`] if the option is [`None`], otherwise calls `predicate`
// with the wrapped value and returns.
func (o Option[T]) Filter(predicate func(T) bool) Option[T] {
if o.IsSome() {
if predicate(o.UnwrapUnchecked()) {
return o
}
}
return None[T]()
}
// And returns [`None`] if the option is [`None`], otherwise returns `optb`.
func (o Option[T]) And(optb Option[T]) Option[T] {
if o.IsSome() {
return optb
}
return o
}
// XAnd returns [`None`] if the option is [`None`], otherwise returns `optb`.
func (o Option[T]) XAnd(optb Option[any]) Option[any] {
if o.IsSome() {
return optb
}
return None[any]()
}
// Or returns the option if it contains a value, otherwise returns `optb`.
func (o Option[T]) Or(optb Option[T]) Option[T] {
if o.IsNone() {
return optb
}
return o
}
// Xor [`Some`] if exactly one of `self`, `optb` is [`Some`], otherwise returns [`None`].
func (o Option[T]) Xor(optb Option[T]) Option[T] {
if o.IsSome() && optb.IsNone() {
return o
}
if o.IsNone() && optb.IsSome() {
return optb
}
return None[T]()
}
// Insert inserts `value` into the option, then returns its pointer.
func (o *Option[T]) Insert(some T) *T {
v := &some
o.value = &v
return v
}
// GetOrInsert inserts `value` into the option if it is [`None`], then
// returns the contained value pointer.
func (o *Option[T]) GetOrInsert(some T) *T {
if o.IsNone() {
v := &some
o.value = &v
}
return *o.value
}
// GetOrInsertWith inserts a value computed from `f` into the option if it is [`None`],
// then returns the contained value.
func (o *Option[T]) GetOrInsertWith(f func() T) *T {
if o.IsNone() {
var v *T
if f == nil {
v = defaultValuePtr[T]()
} else {
var some = f()
v = &some
}
o.value = &v
}
return *o.value
}
// GetOrInsertDefault inserts default value into the option if it is [`None`], then
// returns the contained value pointer.
func (o *Option[T]) GetOrInsertDefault() *T {
if o.IsNone() {
v := defaultValuePtr[T]()
o.value = &v
}
return *o.value
}
// AsPtr returns its pointer or nil.
func (o *Option[T]) AsPtr() *T {
if o == nil || o.value == nil {
return nil
}
return *o.value
}
// Replace replaces the actual value in the option by the value given in parameter,
// returning the old value if present,
// leaving a [`Some`] in its place without deinitializing either one.
func (o *Option[T]) Replace(some T) (old Option[T]) {
old.value = o.value
v := &some
o.value = &v
return old
}
const null = "null"
func (o Option[T]) MarshalJSON() ([]byte, error) {
if o.IsNone() {
return []byte(null), nil
}
return json.Marshal(o.value)
}
func (o *Option[T]) UnmarshalJSON(b []byte) error {
o.value = nil
if *(*string)(unsafe.Pointer(&b)) == null {
return nil
}
var value = new(T)
err := json.Unmarshal(b, value)
if err == nil {
o.value = &value
}
return err
}
var (
_ Iterable[any] = Option[any]{}
_ DeIterable[any] = Option[any]{}
)
func (o Option[T]) Next() Option[T] {
if o.IsNone() {
return o
}
v := o.Unwrap()
*o.value = nil
return Some(v)
}
func (o Option[T]) NextBack() Option[T] {
return o.Next()
}
func (o Option[T]) Remaining() uint {
if o.IsNone() {
return 0
}
return 1
}
// CtrlFlow returns the `CtrlFlow[Void, T]`.
func (o Option[T]) CtrlFlow() CtrlFlow[Void, T] {
if o.IsNone() {
return Break[Void, T](nil)
}
return Continue[Void, T](o.Unwrap())
}
// ToErrable converts from `Option[T]` to `Errable[T]`.
func (o Option[T]) ToErrable() Errable[T] {
if o.IsSome() {
return ToErrable[T](o.UnwrapUnchecked())
}
return NonErrable[T]()
}