push
github
4 of 14 new or added lines in 1 file covered. (28.57%)
2275 of 3117 relevant lines covered (72.99%)
4.13 hits per line
1 |
package optional
|
|
2 |
|
|
3 |
var empty = &optional{v: nil} |
|
4 |
|
|
5 |
// optional struct
|
|
6 |
type optional struct { |
|
7 |
v interface{}
|
|
8 |
} |
|
9 |
|
|
10 |
func Of(data interface{}) *optional { |
5✔ |
11 |
return &optional{v: data}
|
5✔ |
12 |
} |
5✔ |
13 |
|
|
14 |
func OfNillable(data interface{}) *optional { |
× |
15 |
if data == nil { |
× |
16 |
return empty
|
× |
17 |
} |
× |
18 |
|
|
19 |
return &optional{v: data}
|
× |
20 |
} |
|
21 |
|
|
22 |
func (o *optional) Map(fn func(v interface{}) interface{}) *optional { |
× |
23 |
if o.v == nil { |
× |
24 |
return empty
|
× |
25 |
} |
× |
26 |
|
|
27 |
return OfNillable(fn(o.v))
|
× |
28 |
} |
|
29 |
|
|
NEW
|
func (o *optional) Get() interface{} { |
× |
NEW
|
if o.v == nil { |
× |
NEW
|
panic("nil value") |
× |
33 |
} |
|
34 |
|
|
NEW
|
return o.v
|
× |
36 |
} |
|
37 |
|
|
NEW
|
func (o *optional) OrElse(v interface{}) interface{} { |
× |
NEW
|
if o.v == nil { |
× |
NEW
|
return v
|
× |
NEW
|
} |
× |
42 |
|
|
NEW
|
return o.v
|
× |
44 |
} |
|
45 |
|
|
|
func (o *optional) OrElseGet(v interface{}) interface{} { |
5✔ |
|
if o.v == nil { |
10✔ |
|
return v
|
5✔ |
|
} |
5✔ |
50 |
|
|
NEW
|
return o.v
|
× |
52 |
} |