-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtry.go
301 lines (244 loc) · 5.79 KB
/
try.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
package monadgo
import (
"fmt"
"reflect"
)
// TryOrElse is a function type for Try.OrElse.
type TryOrElse func() Try
// Try represents scala-like Try.
type Try interface {
Any
// OK returns true if this is Success.
OK() bool
// Failed returns true if this is Failure.
Failed() bool
// Foreach applies f to Try's value if this is Success.
// f: func(T)
Foreach(f interface{})
// Fold applies z if this is a Failure,
// or f if this is a Success.
// If f is initially applied and last element in results is false or error,
// then z applied with this element value.
// z: func(A) X. A can be error or bool.
// f: func(B) X. B is the element type in Success.
// returns value with type X.
Fold(z, f interface{}) interface{}
// Map applies f to the value from this Success,
// or returns this if this is a Failure.
// f: func(T) X
Map(f interface{}) Try
// FlatMap returns the f applied to the value from this Success,
// or returns this if this is a Failure.
// f: func(T) Try
FlatMap(f interface{}) Try
// OrElse returns this if it's a Success,
// or z if this is a Failure.
OrElse(z TryOrElse) Try
// GetOrElse returns the value from this Success,
// or z if this is a Failure.
GetOrElse(z interface{}) interface{}
// ToOption returns None if this is a Failure,
// or a Some containing Success's value.
ToOption() Option
}
type traitTry struct {
ok bool
v reflect.Value
}
func (t *traitTry) Get() interface{} {
return t.v.Interface()
}
func (t *traitTry) rv() reflect.Value {
return t.v
}
func (t *traitTry) String() string {
if t.ok {
return fmt.Sprintf("Success(%v)", t.Get())
}
return fmt.Sprintf("Failure(%v)", t.Get())
}
func (t *traitTry) OK() bool {
return t.ok
}
func (t *traitTry) Failed() bool {
return !t.ok
}
func (t *traitTry) Foreach(f interface{}) {
if t.ok {
funcOf(f).call(t.v)
}
}
func (t *traitTry) Fold(z, f interface{}) interface{} {
ztyp := reflect.TypeOf(z)
if !t.ok {
if ztyp.Kind() == reflect.Func {
return funcOf(z).call(t.v).Interface()
}
return z
}
result := tryCBF(funcOf(f).call(t.v))
if result.OK() {
return result.Get()
}
return result.Fold(z, nil)
}
func (t *traitTry) Map(f interface{}) Try {
if t.ok {
return tryCBF(funcOf(f).call(t.v))
}
return t
}
func (t *traitTry) FlatMap(f interface{}) Try {
if t.ok {
return funcOf(f).call(t.v).Interface().(Try)
}
return t
}
func (t *traitTry) OrElse(z TryOrElse) Try {
if !t.ok {
return z()
}
return t
}
func (t *traitTry) GetOrElse(z interface{}) interface{} {
if !t.ok {
return checkAndInvoke(z)
}
return t.Get()
}
func (t *traitTry) ToOption() Option {
if !t.ok {
return None
}
return OptionOf(t.Get())
}
/*func tryFromX(x interface{}) Try {
return tryFromContainer(containerOf(x), !isErrorOrFalse(x))
}*/
// isErrorOrFalse checks x is an existing error or false.
func isErrorOrFalse(x interface{}) (bool, bool) {
switch v := x.(type) {
case error:
return v != nil, true
case bool:
return !v, true
default:
return false, x == nil
}
}
// ----------------------------------------------------------------------------
// FailureOf returns Failure of x if x is error or false, or returns Success of x.
func FailureOf(x interface{}) Try {
return tryCBF(x)
}
// ----------------------------------------------------------------------------
// SuccessOf returns Success of x.
func SuccessOf(x ...interface{}) Try {
return tryCBF(x...)
}
var successNull = &traitTry{ok: true, v: nullValue}
var successUnit = &traitTry{ok: true, v: unitValue}
func newTraitTry(ok bool, x interface{}) Try {
return &traitTry{
ok: ok,
v: reflect.ValueOf(x),
}
}
func tryCBF(x ...interface{}) (ret Try) {
len := len(x)
if len == 0 {
return successUnit
}
switch len {
case 1:
switch v := x[0].(type) {
case Try:
return v
case reflect.Value:
return tryCBF(v.Interface())
case bool:
return newTraitTry(v, v)
case error:
if v != nil {
return newTraitTry(false, v)
}
return successNull
case Tuple:
return tryNOf(v)
default:
if v == nil {
return successNull
}
if reflect.TypeOf(v).Kind() == reflect.Func {
defer func() {
if r := recover(); r != nil {
ret = newTraitTry(false, fmt.Errorf("%v", r))
}
}()
return tryCBF(funcOf(v).call(unitValue).Interface())
}
return newTraitTry(true, v)
}
case 2:
return try1Of(x[0], x[1])
case 3:
return try2Of(x[0], x[1], x[2])
default:
return tryNOf(TupleOf(x))
}
}
// TryOf returns a Try.
// The last argument must be bool or error type.
// Return Failure if errOrFalse is false or error existing,
// or Success of Null.
func TryOf(x ...interface{}) (ret Try) {
return tryCBF(x...)
}
// try1Of returns a Try.
// errOrFalse must be bool or error type.
// Return Failure if errOrFalse is false or error existing,
// or Success of x.
func try1Of(x, errOrFalse interface{}) Try {
yes, reduce := isErrorOrFalse(errOrFalse)
if yes {
return newTraitTry(false, errOrFalse)
}
if reduce {
if x == nil {
return successNull
}
return newTraitTry(true, x)
}
return newTraitTry(true, Tuple2Of(x, errOrFalse))
}
// try2Of returns a Try.
// errOrFalse must be bool or error type.
// Return Failure if errOrFalse is false or error existing,
// or Success of Tuple2(x1,x2).
func try2Of(x1, x2, errOrFalse interface{}) Try {
yes, reduce := isErrorOrFalse(errOrFalse)
if yes {
return newTraitTry(false, errOrFalse)
}
if reduce {
return newTraitTry(true, Tuple2Of(x1, x2))
}
return newTraitTry(true, Tuple3Of(x1, x2, errOrFalse))
}
func tryNOf(t Tuple) Try {
last := t.V(t.Dimension() - 1)
yes, reduce := isErrorOrFalse(last)
if yes {
return newTraitTry(false, last)
}
if !reduce {
return newTraitTry(true, t)
}
if t.Dimension() == 2 {
if t.V(0) == nil {
return successNull
}
return newTraitTry(true, t.V(0))
}
return newTraitTry(true, t.reduce())
}