-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptional.go
123 lines (99 loc) · 2.64 KB
/
optional.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
package tl
// Optional defines a value that can be optional. So, if might be defined or not.
type Optional[T any] struct {
value T
hasValue bool
}
// MakeOptional returns an Optional with a value `v`.
func MakeOptional[T any](v T) (opt Optional[T]) {
opt.Set(v)
return opt
}
// NewOptional returns an Optional with a value `v` if `v` is not nil.
func NewOptional[T any](v *T) (opt Optional[T]) {
if v != nil {
opt.Set(*v)
}
return opt
}
// None returns an empty Optional.
func None[T any]() (opt Optional[T]) {
return opt
}
// From sets the value `v` to the Optional.
func (opt Optional[T]) From(v T) Optional[T] {
opt.Set(v)
return opt
}
// Ptr returns a pointer to the value or nil if there's no value.
func (opt Optional[T]) Ptr() *T {
if opt.hasValue {
return &opt.value
}
return nil
}
// Get returns the value. The function doesn't check whether the value is set or not.
func (opt Optional[T]) Get() T {
return opt.value
}
// Or sets the value `v` to the Optional only if no value is already present.
func (opt Optional[T]) Or(v T) Optional[T] {
if !opt.hasValue {
opt.Set(v)
}
return opt
}
// HasValue returns true if Optional contains a valid value.
func (opt Optional[T]) HasValue() bool {
return opt.hasValue
}
// Set sets a value `v` to the Optional.
func (opt *Optional[T]) Set(v T) {
opt.value = v
opt.hasValue = true
}
// Reset sets the Optional as invalid.
func (opt *Optional[T]) Reset() {
opt.hasValue = false
}
// OptionalPtr is like Optional but instead of holding a copy of the value, it holds a pointer.
type OptionalPtr[T any] struct {
value *T
}
// MakeOptionalPtr returns an OptionalPtr using the value `v`.
func MakeOptionalPtr[T any](v *T) (opt OptionalPtr[T]) {
opt.Set(v)
return opt
}
// Ptr returns a pointer to the value.
func (opt OptionalPtr[T]) Ptr() *T {
return opt.value
}
// Get returns a pointer to the value, because the value is a pointer already.
func (opt OptionalPtr[T]) Get() *T {
return opt.value
}
// GetValue returns the value inside the Optional's pointer. This function might panic
// if the value is not defined (aka value == nil).
func (opt *OptionalPtr[T]) GetValue() T {
return *opt.value
}
// Or sets the value `v` to the Optional only if not value is already present.
func (opt OptionalPtr[T]) Or(v *T) OptionalPtr[T] {
if !opt.HasValue() {
opt.Set(v)
}
return opt
}
// HasValue returns true if a value is present.
func (opt OptionalPtr[T]) HasValue() bool {
return opt.value != nil
}
// Set sets the value `v` to the OptionalPtr.
func (opt *OptionalPtr[T]) Set(v *T) {
opt.value = v
}
// Reset resets the OptionalPtr's value.
func (opt *OptionalPtr[T]) Reset() {
opt.value = nil
}