-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoption.go
64 lines (55 loc) · 1.15 KB
/
goption.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
package goption
import "reflect"
// Option is interface to be the type of function return
type Option interface {
Is_None() bool
Some(func(interface{})) Option
None(func()) Option
Get() interface{}
}
type vSome struct {
v interface{}
}
func (v *vSome) Is_None() bool {
return false
}
func (v *vSome) Some(f func(interface{})) Option {
f(v.v)
return v
}
func (v *vSome) None(func()) Option {
return v
}
func (v *vSome) Get() interface{} {
return v.v
}
//Some return a type vSome
func Some(v interface{}) Option {
return &vSome{v: v}
}
type vNone struct{}
func (v *vNone) Is_None() bool {
return true
}
func (v *vNone) Some(f func(interface{})) Option {
return v
}
func (v *vNone) None(f func()) Option {
f()
return v
}
func (v *vNone) Get() interface{} {
panic("none not support get you should use is_none to check")
}
//None return a type vNone
func None() Option {
return &vNone{}
}
//ToOption conver golang nil to option
// if function return nil then this function return None
func ToOption(v interface{}) Option {
if v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil()) {
return None()
}
return Some(v)
}