/
util.go
199 lines (181 loc) · 3.54 KB
/
util.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
package util
import (
"encoding/json"
"fmt"
"html"
"reflect"
"sort"
"strconv"
"strings"
"unsafe"
)
func GetSortedKey(m interface{}) (keys []string) {
switch t := m.(type) {
case map[string]string:
keys = make([]string, len(t))
index := 0
for k := range t {
keys[index] = k
index++
}
if len(t) < 2 {
return keys
}
sort.Strings(keys)
case map[string]interface{}:
keys = make([]string, len(t))
index := 0
for k := range t {
keys[index] = k
index++
}
if len(t) < 2 {
return keys
}
sort.Strings(keys)
default:
panic(fmt.Sprintf("不支持的类型 %T, %+v", m, m))
}
return
}
func CopyMap(data map[string]interface{}) map[string]interface{} {
if data == nil {
return nil
}
m := make(map[string]interface{}, len(data))
for k, v := range data {
m[k] = v
}
return m
}
func UnsafeStrToBytes(s string) (b []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return b
}
func UnsafeBytesToStr(z []byte) string {
return *(*string)(unsafe.Pointer(&z))
}
func InterfaceToStr(s interface{}, escaped ...bool) (d string) {
switch a := s.(type) {
case string:
d = a
case int:
d = strconv.FormatInt(int64(a), 10)
case int32:
d = strconv.FormatInt(int64(a), 10)
case int64:
d = strconv.FormatInt(a, 10)
case float64:
d = strconv.FormatFloat(a, 'f', -1, 64)
case interface{ MarshalJSON() ([]byte, error) }:
bs, _ := a.MarshalJSON()
d = string(bs)
case interface{ String() string }:
d = a.String()
default:
bs, _ := json.Marshal(a)
d = string(bs)
}
if len(escaped) == 1 && escaped[0] {
d = Escape(d)
}
return
}
func Escape(src string) string {
return html.EscapeString(src)
}
//
var styleEscaper = strings.NewReplacer(
`&`, "&",
// 单引号在style中是合法的.
//`'`, "'", // "'" is shorter than "'" and apos was not in HTML until HTML5.
`<`, "<",
`>`, ">",
`"`, """, // """ is shorter than """.
)
// EscapeStyle 和 html.EscapeString() 不同, EscapeStyle 不会再替换单引号 ', 因为单引号在style中是合法的.
func EscapeStyle(src string) string {
return styleEscaper.Replace(src)
}
// 字符串false,0 会被认定为false
func InterfaceToBool(s interface{}) (d bool) {
if s == nil {
return false
}
switch a := s.(type) {
case bool:
return a
case int:
return a != 0
case int8:
return a != 0
case int16:
return a != 0
case int32:
return a != 0
case int64:
return a != 0
case float64:
return a != 0
case float32:
return a != 0
case string:
return a != "" && a != "false" && a != "0"
default:
return true
}
}
func ForInterface(s interface{}, cb func(index int, v interface{}) error) error {
switch a := s.(type) {
case []interface{}:
for i := range a {
if err := cb(i, a[i]); err != nil {
return err
}
}
case []map[string]interface{}:
for i := range a {
if err := cb(i, a[i]); err != nil {
return err
}
}
case []int:
for i := range a {
if err := cb(i, a[i]); err != nil {
return err
}
}
case []int64:
for i := range a {
if err := cb(i, a[i]); err != nil {
return err
}
}
case []int32:
for i := range a {
if err := cb(i, a[i]); err != nil {
return err
}
}
case []string:
for i := range a {
if err := cb(i, a[i]); err != nil {
return err
}
}
case []float64:
for i := range a {
if err := cb(i, a[i]); err != nil {
return err
}
}
}
return nil
}
func Interface2Slice(s interface{}) (d []interface{}) {
return
}