-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvert.go
180 lines (165 loc) · 3.26 KB
/
convert.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
package utils
import (
"encoding/base64"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
// MustJSONIndent 转 json 返回 []byte
func MustJSONIndent(v interface{}) []byte {
js, _ := json.MarshalIndent(v, "", " ")
return js
}
// MustJSONIndentString 转 json Indent 返回 string
func MustJSONIndentString(v interface{}) string {
return B2S(MustJSONIndent(v))
}
// MustJSON 转 json 返回 []byte
func MustJSON(v interface{}) []byte {
js, _ := json.Marshal(v)
return js
}
// MustJSONString 转 json 返回 string
func MustJSONString(v interface{}) string {
return B2S(MustJSON(v))
}
// MustString 强制转为字符串
func MustString(v interface{}, timeLayout ...string) string {
switch s := v.(type) {
default:
return fmt.Sprint(v)
case string:
return s
case []byte:
return string(s)
case error:
return s.Error()
case nil:
return ""
case bool:
return strconv.FormatBool(s)
case int:
return strconv.Itoa(s)
case int8:
return strconv.FormatInt(int64(s), 10)
case int16:
return strconv.FormatInt(int64(s), 10)
case int32:
return strconv.Itoa(int(s))
case int64:
return strconv.FormatInt(s, 10)
case uint:
return strconv.FormatUint(uint64(s), 10)
case uint8:
return strconv.FormatUint(uint64(s), 10)
case uint16:
return strconv.FormatUint(uint64(s), 10)
case uint32:
return strconv.FormatUint(uint64(s), 10)
case uint64:
return strconv.FormatUint(s, 10)
case float32:
return strconv.FormatFloat(float64(s), 'f', -1, 32)
case float64:
return strconv.FormatFloat(s, 'f', -1, 64)
case time.Time:
if len(timeLayout) > 0 {
return s.Format(timeLayout[0])
}
return s.Format("2006-01-02 15:04:05")
case reflect.Value:
return MustString(s.Interface(), timeLayout...)
case fmt.Stringer:
return s.String()
}
}
// MustInt 强制转为整数 (int)
func MustInt(v interface{}) int {
switch i := v.(type) {
default:
d, ok := i.(int)
if ok {
return d
}
return 0
case string:
v, err := strconv.Atoi(strings.TrimSpace(i))
if err == nil {
return v
}
return 0
case bool:
if i {
return 1
}
return 0
case nil:
return 0
case int:
return i
case int8:
return int(i)
case int16:
return int(i)
case int32:
return int(i)
case int64:
return int(i)
case uint:
return int(i)
case uint8:
return int(i)
case uint16:
return int(i)
case uint32:
return int(i)
case uint64:
return int(i)
case float32:
return int(i)
case float64:
return int(i)
}
}
// MustBool 强制转为 bool
func MustBool(v interface{}) bool {
switch t := v.(type) {
default:
if MustInt(v) != 0 {
return true
}
case bool:
return t
case string:
switch t {
case "1", "t", "T", "true", "TRUE", "True":
return true
}
}
return false
}
// B64Encode Base64 编码
func B64Encode(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
// B64Decode Base64 解码
func B64Decode(s string) []byte {
if b, err := base64.StdEncoding.DecodeString(s); err == nil {
return b
}
return nil
}
// B64UrlEncode Base64 解码, 安全 URL, 替换: "+/" 为 "-_"
func B64UrlEncode(b []byte) string {
return base64.URLEncoding.EncodeToString(b)
}
// B64UrlDecode Base64 解码
func B64UrlDecode(s string) []byte {
if b, err := base64.URLEncoding.DecodeString(s); err == nil {
return b
}
return nil
}