-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper_parser.go
233 lines (214 loc) · 4.16 KB
/
helper_parser.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package t
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
// ParseFloat64 ...
func ParseFloat64(o interface{}) (res float64) {
switch value := o.(type) {
case float32:
return float64(value)
case float64:
return value
default:
var str = ParseString(o)
var strUpper = strings.ToUpper(str)
if strUpper == "TRUE" {
str = "1"
} else if strUpper == "FALSE" {
str = "0"
}
res, _ = strconv.ParseFloat(str, 64)
}
return
}
// ParseFloat32 ...
func ParseFloat32(o interface{}) float32 {
return float32(ParseFloat64(o))
}
// ParseInt64 ...
func ParseInt64(o interface{}) (res int64) {
switch val := o.(type) {
case int64:
return val
case int:
return int64(val)
case int32:
return int64(val)
case int16:
return int64(val)
case int8:
return int64(val)
case uint64:
return int64(val)
case uint:
return int64(val)
case uint32:
return int64(val)
case uint16:
return int64(val)
case uint8:
return int64(val)
default:
return int64(ParseFloat64(o))
}
}
// ParseInt32 ...
func ParseInt32(o interface{}) (res int32) {
res = int32(ParseFloat64(o))
return
}
// ParseInt16 ...
func ParseInt16(o interface{}) (res int16) {
res = int16(ParseFloat64(o))
return
}
// ParseInt8 ...
func ParseInt8(o interface{}) (res int8) {
res = int8(ParseFloat64(o))
return
}
// ParseInt ...
func ParseInt(o interface{}) int {
return int(ParseInt64(o))
}
// ParseUint64 ...
func ParseUint64(o interface{}) uint64 {
return uint64(ParseInt64(o))
}
// ParseUint32 ...
func ParseUint32(o interface{}) uint32 {
return uint32(ParseInt64(o))
}
// ParseUint16 ...
func ParseUint16(o interface{}) uint16 {
return uint16(ParseInt64(o))
}
// ParseUint8 ...
func ParseUint8(o interface{}) uint8 {
return uint8(ParseInt64(o))
}
// ParseUint ...
func ParseUint(o interface{}) uint {
return uint(ParseInt64(o))
}
// ParseBool ...
func ParseBool(o interface{}) bool {
var str = ParseString(o)
str = strings.ToUpper(str)
switch str {
case "", "0", "FALSE", "<NIL>":
return false
default:
return true
}
}
// ParseByte ...
func ParseByte(i interface{}) byte {
if v, ok := i.(byte); ok {
return v
}
return ParseUint8(i)
}
// ParseBytes ...
func ParseBytes(o interface{}) []byte {
if o == nil {
return nil
}
switch value := o.(type) {
case []byte:
return value
default:
tmp := ParseString(o)
return []byte(tmp)
}
}
// ParseRune ...
func ParseRune(o interface{}) rune {
if v, ok := o.(rune); ok {
return v
}
return rune(ParseInt32(o))
}
// ParseRunes ...
func ParseRunes(o interface{}) []rune {
if v, ok := o.([]rune); ok {
return v
}
return []rune(ParseString(o))
}
// ParseString 任意对象转换为字符串
func ParseString(o interface{}) string {
if o == nil {
return ""
}
switch value := o.(type) {
case string:
return value
case []byte:
return string(value)
case int:
return strconv.Itoa(value)
case int8:
return strconv.Itoa(int(value))
case int16:
return strconv.Itoa(int(value))
case int32:
return strconv.Itoa(int(value))
case int64:
return strconv.FormatInt(value, 10)
case uint:
return strconv.FormatUint(uint64(value), 10)
case uint8:
return strconv.FormatUint(uint64(value), 10)
case uint16:
return strconv.FormatUint(uint64(value), 10)
case uint32:
return strconv.FormatUint(uint64(value), 10)
case uint64:
return strconv.FormatUint(value, 10)
case float32:
return strconv.FormatFloat(float64(value), 'f', -1, 32)
case float64:
return strconv.FormatFloat(value, 'f', -1, 64)
case bool:
return strconv.FormatBool(value)
case *time.Time:
if value == nil {
return ""
}
return value.String()
default:
if value == nil {
return ""
}
// 如果是复杂类型,则转换为json
rv := reflect.ValueOf(value)
kind := rv.Kind()
switch kind {
case reflect.Chan,
reflect.Map,
reflect.Slice,
reflect.Func,
reflect.Ptr,
reflect.Interface,
reflect.UnsafePointer:
if rv.IsNil() {
return ""
}
}
if kind == reflect.Ptr {
return ParseString(rv.Elem().Interface())
}
// 如果不能转换为json的, 则原样输出为字符串
if js, err := json.Marshal(value); err != nil {
return fmt.Sprint(value)
} else {
return string(js)
}
}
}