-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
102 lines (81 loc) · 2.06 KB
/
value.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
package cfg
import (
"fmt"
"strconv"
"strings"
"time"
)
// Value是字符串数据的别名
type Value string
// 返回String值
func (v Value) String() string { return string(v) }
// 返回解析Float64数值
func (v Value) Float64() (float64, error) {
return strconv.ParseFloat(v.String(), 64)
}
// 返回解析Float64数值,或指定默认值
func (v Value) Float64OrDefault(defaultValue float64) float64 {
if i, e := v.Float64(); nil != e {
return defaultValue
} else {
return i
}
}
// 返回解析Int64数值
func (v Value) Int64() (int64, error) {
return strconv.ParseInt(v.String(), 10, 64)
}
// 返回解析Int64数值,或指定默认值
func (v Value) Int64OrDefault(defaultValue int64) int64 {
if i, e := v.Int64(); nil != e {
return defaultValue
} else {
return i
}
}
// 返回时差Duration值
func (v Value) Duration() (time.Duration, error) {
return time.ParseDuration(v.String())
}
// 返回时差Duration值,或指定默认值
func (v Value) DurationOrDefault(defaultValue time.Duration) time.Duration {
if i, e := v.Duration(); nil != e {
return defaultValue
} else {
return i
}
}
// 返回Boolean值
func (v Value) Bool() bool {
s := strings.ToLower(v.String())
return "true" == s || "1" == s
}
// 将Any类型,转换成String
func Value2String(value interface{}) string {
return ToString(value)
}
// 将Any类型,转换成String
func ToString(values interface{}) string {
switch values.(type) {
case string:
return values.(string)
case int:
return strconv.FormatInt(int64(values.(int)), 10)
case uint:
return strconv.FormatInt(int64(values.(int)), 10)
case int32:
return strconv.FormatInt(int64(values.(int32)), 10)
case uint32:
return strconv.FormatInt(int64(values.(int32)), 10)
case int64:
return strconv.FormatInt(values.(int64), 10)
case uint64:
return strconv.FormatInt(values.(int64), 10)
case float32:
return strconv.FormatFloat(float64(values.(float32)), 'E', -1, 32)
case float64:
return strconv.FormatFloat(values.(float64), 'E', -1, 64)
default:
return fmt.Sprintf("%v", values)
}
}