-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
49 lines (45 loc) · 967 Bytes
/
option.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
package vd
import (
xconv "github.com/goclub/conv"
"strconv"
)
// option int simulate int? (int or nil)
// vd.Int(18) equal OptionInt{valid: true, int: 18}
type OptionInt struct {
valid bool
int int
}
func (o OptionInt) Valid() bool {
return o.valid
}
func (o OptionInt) String() string {
if !o.valid {return ""}
return xconv.IntString(o.int)
}
func (o OptionInt) Unwrap() int {
if o.valid {return o.int}
return 0
}
func Int(i int) OptionInt {
return OptionInt{true, i}
}
// option Float simulate Float? (Float or nil)
// vd.Float(18.1) equal OptionFloat{valid: true, float: 18.1}
type OptionFloat struct {
valid bool
float float64
}
func (o OptionFloat) Valid() bool {
return o.valid
}
func (o OptionFloat) String() string {
if !o.valid {return ""}
return strconv.FormatFloat(o.float,'f', -1, 64)
}
func (o OptionFloat) Unwrap() float64 {
if o.valid {return o.float}
return 0
}
func Float(f float64) OptionFloat {
return OptionFloat{true, f}
}