-
Notifications
You must be signed in to change notification settings - Fork 1
/
primitive.go
156 lines (112 loc) · 2.11 KB
/
primitive.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package convert
import (
"fmt"
"strconv"
)
func ToBoolP(any interface{}) *bool {
value := ToBool(any)
return &value
}
func ToIntP(any interface{}) *int {
value := ToInt(any)
return &value
}
func ToInt64P(any interface{}) *int64 {
value := ToInt64(any)
return &value
}
func ToFloat32P(any interface{}) *float32 {
value := float32(ToFloat(any))
return &value
}
func ToFloat64P(any float64) *float64 {
value := ToFloat(any)
return &value
}
func ToStringP(any string) *string {
value := ToString(any)
return &value
}
func ToString(any interface{}) (out string) {
switch any := any.(type) {
case int:
out = strconv.Itoa(any)
case int8:
out = strconv.FormatInt(int64(any), 10)
case int16:
out = strconv.FormatInt(int64(any), 10)
case int32:
out = strconv.FormatInt(int64(any), 10)
case int64:
out = strconv.FormatInt(any, 10)
case string:
out = any
case float32:
out = strconv.FormatFloat(float64(any), 'f', -1, 32)
case float64:
out = strconv.FormatFloat(any, 'f', -1, 64)
case bool:
out = strconv.FormatBool(any)
default:
if any == nil {
out = ""
} else {
out = fmt.Sprintf("%v", any)
}
}
return
}
func ToInt(any interface{}) (out int) {
return int(ToInt64(any))
}
func ToInt64(any interface{}) (out int64) {
switch any := any.(type) {
case int:
out = int64(any)
case int8:
out = int64(any)
case int16:
out = int64(any)
case int32:
out = int64(any)
case int64:
out = any
case float32:
out = int64(any)
case float64:
out = int64(any)
case string:
out, _ = strconv.ParseInt(any, 10, 64)
default:
out = ToInt64(0)
}
return
}
func ToFloat(any interface{}) (out float64) {
switch any := any.(type) {
case int:
out = float64(any)
case int8:
out = float64(any)
case int16:
out = float64(any)
case int32:
out = float64(any)
case int64:
out = float64(any)
case float32:
out = float64(any)
case float64:
out = any
case string:
out, _ = strconv.ParseFloat(any, 64)
default:
out = float64(0)
}
return
}
func ToBool(any interface{}) (out bool) {
val := ToString(any)
out, _ = strconv.ParseBool(val)
return
}