-
Notifications
You must be signed in to change notification settings - Fork 0
/
maybe.go
71 lines (56 loc) · 1.05 KB
/
maybe.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
package maybe
import (
"github.com/branchgrove/kaeru"
)
// Contains some or none value based on ok
type Maybe[T any] struct {
value T
ok bool
}
// Ensures compatability with kaeru
func (o *Maybe[T]) ParseAny(v any) error {
if v == nil {
o.ok = false
return nil
}
err := kaeru.Parse(v, &o.value)
if err != nil {
return err
}
o.ok = true
return nil
}
// Default value for kaeru parsing
func (o *Maybe[T]) SetDefault() {
*o = None[T]()
}
// Create a new Maybe containing a value
func Some[T any](v T) Maybe[T] {
return Maybe[T]{
value: v,
ok: true,
}
}
// Create a new Maybe without a value
func None[T any]() Maybe[T] {
return Maybe[T]{
ok: false,
}
}
// Returns the value or panics if it is None
func (o Maybe[T]) Unwrap() T {
if o.ok == false {
panic("Tried to unwrap None value")
}
return o.value
}
// Returns the value and ok, if ok is true there is a value
func (o Maybe[T]) Get() (T, bool) {
return o.value, o.ok
}
func (o Maybe[T]) IsNone() bool {
return !o.ok
}
func (o Maybe[T]) IsSome() bool {
return o.ok
}