-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhey_func.go
More file actions
268 lines (247 loc) · 6.16 KB
/
hey_func.go
File metadata and controls
268 lines (247 loc) · 6.16 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// This is where some of the most commonly used functions are stored.
package hey
import (
"maps"
"strings"
"unsafe"
"github.com/cd365/hey/v7/cst"
)
// AnyAny Convert any type of slice to []any.
func AnyAny[T any](slice []T) []any {
result := make([]any, len(slice))
for i, v := range slice {
result[i] = v
}
return result
}
// DiscardDuplicate Slice member deduplication.
func DiscardDuplicate[T comparable](discard func(tmp T) bool, dynamic ...T) (result []T) {
length := len(dynamic)
mp := make(map[T]*struct{}, length)
ok := false
result = make([]T, 0, length)
for i := range length {
if _, ok = mp[dynamic[i]]; ok {
continue
}
if discard != nil {
if discard(dynamic[i]) {
continue
}
}
mp[dynamic[i]] = nil
result = append(result, dynamic[i])
}
return result
}
// MergeSlice Combine multiple slices of the same type into one slice.
func MergeSlice[V any](values ...[]V) []V {
length := 0
for _, v := range values {
length += len(v)
}
result := make([]V, 0, length)
for _, v := range values {
result = append(result, v...)
}
return result
}
// MergeMap Combine multiple maps of the same type into one map.
func MergeMap[K comparable, V any](values ...map[K]V) map[K]V {
length := len(values)
result := make(map[K]V, 8)
for i := range length {
maps.Copy(result, values[i])
}
return result
}
// MapToSlice Create a slice using a custom function and map.
func MapToSlice[K comparable, V any, W any](values map[K]V, fx func(k K, v V) W) []W {
if fx == nil {
return nil
}
length := len(values)
result := make([]W, 0, length)
for index, value := range values {
result = append(result, fx(index, value))
}
return result
}
// MapToMap Create a map based on another map.
func MapToMap[K comparable, V any, X comparable, Y any](values map[K]V, fx func(k K, v V) (X, Y)) map[X]Y {
if fx == nil {
return nil
}
result := make(map[X]Y, len(values))
for key, value := range values {
k, v := fx(key, value)
result[k] = v
}
return result
}
// SliceToSlice Create a slice from another slice.
func SliceToSlice[V any, W any](values []V, fx func(k int, v V) W) []W {
if fx == nil {
return nil
}
result := make([]W, len(values))
for index, value := range values {
result[index] = fx(index, value)
}
return result
}
// SliceToMap Create a map using a custom function and slice.
func SliceToMap[V any, K comparable, W any](values []V, fx func(v V) (K, W)) map[K]W {
if fx == nil {
return nil
}
length := len(values)
result := make(map[K]W, length)
for i := range length {
k, v := fx(values[i])
result[k] = v
}
return result
}
// SliceDiscard Delete some elements from a slice; the deletion criteria are determined by `discard`.
func SliceDiscard[V any](values []V, discard func(k int, v V) bool) []V {
if values == nil || discard == nil {
return values
}
result := make([]V, 0, len(values))
for index, value := range values {
if !discard(index, value) {
result = append(result, value)
}
}
return result
}
// MapDiscard Remove some elements from the map; the deletion criteria are determined by `discard`.
func MapDiscard[K comparable, V any](values map[K]V, discard func(k K, v V) bool) map[K]V {
if values == nil || discard == nil {
return values
}
result := make(map[K]V, len(values))
for index, value := range values {
if !discard(index, value) {
result[index] = value
}
}
return result
}
// JoinString Concatenate multiple strings in sequence.
func JoinString(elems ...string) string {
builder := poolGetStringBuilder()
defer poolPutStringBuilder(builder)
length := len(elems)
for i := range length {
builder.WriteString(elems[i])
}
return builder.String()
}
// LastNotEmptyString Get last not empty string, return empty string if it does not exist.
func LastNotEmptyString(sss []string) string {
for i := len(sss) - 1; i >= 0; i-- {
if sss[i] != cst.Empty {
return sss[i]
}
}
return cst.Empty
}
// SQLBlockComment SQL statement block comment.
func SQLBlockComment(comment string) string {
comment = strings.TrimSpace(comment)
if comment == cst.Empty {
return ""
}
return JoinString("/*", comment, "*/")
}
// InValues Build column IN ( values[0].attributeN, values[1].attributeN, values[2].attributeN ... )
func InValues[T any](values []T, fx func(tmp T) any) []any {
if fx == nil {
return nil
}
length := len(values)
if length == 0 {
return nil
}
num := 0
result := make([]any, 0, length)
for _, value := range values {
elem := fx(value)
if elem != nil {
num++
result = append(result, elem)
}
}
return result
}
// InGroupValues Build ( column1, column2, column3 ... ) IN ( ( values[0].attribute1, values[0].attribute2, values[0].attribute3 ... ), ( values[1].attribute1, values[1].attribute2, values[1].attribute3 ... ) ... )
func InGroupValues[T any](values []T, fx func(tmp T) []any) [][]any {
if fx == nil {
return nil
}
length := len(values)
if length == 0 {
return nil
}
num := 0
result := make([][]any, 0, length)
for _, value := range values {
elem := fx(value)
if elem != nil {
num++
result = append(result, elem)
}
}
return result
}
// NamingPascal Naming pascal case.
func NamingPascal(value string) string {
if value == cst.Empty {
return cst.Empty
}
length := len(value)
result := make([]byte, 0, length)
next2upper := true
for i := range length {
if value[i] == '_' {
next2upper = true
continue
}
if next2upper && value[i] >= 'a' && value[i] <= 'z' {
result = append(result, value[i]-32)
} else {
result = append(result, value[i])
}
next2upper = false
}
return string(result[:])
}
// NamingCamel Naming camel case.
func NamingCamel(value string) string {
if value == cst.Empty {
return cst.Empty
}
value = NamingPascal(value)
return JoinString(strings.ToLower(value[0:1]), value[1:])
}
// NamingUnderline Naming underline case.
func NamingUnderline(value string) string {
if value == cst.Empty {
return cst.Empty
}
length := len(value)
result := make([]byte, 0, length)
for i := range length {
if value[i] >= 'A' && value[i] <= 'Z' {
if i > 0 {
result = append(result, '_')
}
result = append(result, value[i]+32)
} else {
result = append(result, value[i])
}
}
return *(*string)(unsafe.Pointer(&result))
}