-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
45 lines (37 loc) · 1.21 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
package result
type Result interface {
Value() interface{}
IsOkay() bool
IsError() bool
Or(Result) Result
And(Result) Result
AndThen(res) Result
OrElse(res) Result
}
type res func(interface{}) Result
type okay struct {
value interface{}
}
func (o *okay) Value() interface{} { return o.value }
func (o *okay) IsOkay() bool { return true }
func (o *okay) IsError() bool { return !o.IsOkay() }
func (o *okay) Or(result Result) Result { return o }
func (o *okay) And(result Result) Result { return result }
func (o *okay) AndThen(fn res) Result { return fn(o.value) }
func (o *okay) OrElse(fn res) Result { return o }
type err struct {
value interface{}
}
func (e *err) Value() interface{} { return e.value }
func (e *err) IsOkay() bool { return !e.IsError() }
func (e *err) IsError() bool { return true }
func (e *err) Or(result Result) Result { return result }
func (e *err) And(result Result) Result { return e }
func (e *err) AndThen(fn res) Result { return e }
func (e *err) OrElse(fn res) Result { return fn(e.value) }
func Ok(value interface{}) Result {
return &okay{value: value}
}
func Err(value interface{}) Result {
return &err{value: value}
}