-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
140 lines (132 loc) · 2.33 KB
/
interface.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
package utils
import (
"encoding/json"
"strconv"
)
// interface => int64
func Interface2Int64(value interface{}) (s int64) {
switch t := value.(type) {
case nil:
s = 0
case int8:
s = int64(value.(int8))
case uint8:
s = int64(value.(uint8))
case int:
s = int64(value.(int))
case uint:
s = int64(value.(uint))
case int64:
s = value.(int64)
case uint64:
s = int64(value.(uint64))
case int32:
s = int64(value.(int32))
case uint32:
s = int64(value.(uint32))
case float64:
s = int64(value.(float64))
case float32:
s = int64(value.(float32))
case bool:
if t {
s = 1
} else {
s = 0
}
case string:
s = String2Int64(t, 0)
case json.Number:
s, _ = t.Int64()
case json.Delim:
s = String2Int64(t.String(), 0)
default:
s = 0
}
return s
}
// interface => string
func Interface2String(value interface{}) (s string) {
switch t := value.(type) {
case nil:
s = ""
case int8:
s = strconv.Itoa(int(value.(int8)))
case uint8:
s = strconv.Itoa(int(value.(uint8)))
case int:
s = strconv.Itoa(t)
case uint:
s = strconv.Itoa(int(value.(uint)))
case int64:
s = strconv.FormatInt(value.(int64), 10)
case uint64:
s = strconv.FormatUint(value.(uint64), 10)
case int32:
s = strconv.FormatInt(int64(value.(int32)), 10)
case uint32:
s = strconv.FormatInt(int64(value.(uint32)), 10)
case float64:
s = strconv.FormatFloat(t, 'f', 0, 64)
case float32:
s = strconv.FormatFloat(float64(t), 'f', 0, 64)
case bool:
if t {
s = "true"
} else {
s = "false"
}
case string:
s = t
case json.Number:
s = t.String()
case json.Delim:
s = t.String()
default:
s = ""
}
return s
}
// interface => int
func Interface2Int(value interface{}) (s int) {
switch t := value.(type) {
case nil:
s = 0
case int8:
s = int(value.(int8))
case uint8:
s = int(value.(uint8))
case int:
s = value.(int)
case uint:
s = int(value.(uint))
case int64:
s = int(value.(int64))
case uint64:
s = int(value.(uint64))
case int32:
s = int(value.(int32))
case uint32:
s = int(value.(uint32))
case float64:
s = int(value.(float64))
case float32:
s = int(value.(float32))
case bool:
if t {
s = 1
} else {
s = 0
}
case string:
s = String2Int(t, 0)
case json.Number:
i64, _ := t.Int64()
s = int(i64)
case json.Delim:
s = String2Int(t.String(), 0)
default:
s = 0
}
return s
}