-
Notifications
You must be signed in to change notification settings - Fork 273
/
helpers.go
317 lines (250 loc) · 6.36 KB
/
helpers.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package funk
import (
"bytes"
"math/rand"
"reflect"
)
var numericZeros = []interface{}{
int(0),
int8(0),
int16(0),
int32(0),
int64(0),
uint(0),
uint8(0),
uint16(0),
uint32(0),
uint64(0),
float32(0),
float64(0),
}
// ToFloat64 converts any numeric value to float64.
func ToFloat64(x interface{}) (float64, bool) {
var xf float64
xok := true
switch xn := x.(type) {
case uint8:
xf = float64(xn)
case uint16:
xf = float64(xn)
case uint32:
xf = float64(xn)
case uint64:
xf = float64(xn)
case int:
xf = float64(xn)
case int8:
xf = float64(xn)
case int16:
xf = float64(xn)
case int32:
xf = float64(xn)
case int64:
xf = float64(xn)
case float32:
xf = float64(xn)
case float64:
xf = float64(xn)
default:
xok = false
}
return xf, xok
}
// PtrOf makes a copy of the given interface and returns a pointer.
func PtrOf(itf interface{}) interface{} {
t := reflect.TypeOf(itf)
cp := reflect.New(t)
cp.Elem().Set(reflect.ValueOf(itf))
// Avoid double pointers if itf is a pointer
if t.Kind() == reflect.Ptr {
return cp.Elem().Interface()
}
return cp.Interface()
}
// IsFunction returns if the argument is a function.
func IsFunction(in interface{}, num ...int) bool {
funcType := reflect.TypeOf(in)
result := funcType != nil && funcType.Kind() == reflect.Func
if len(num) >= 1 {
result = result && funcType.NumIn() == num[0]
}
if len(num) == 2 {
result = result && funcType.NumOut() == num[1]
}
return result
}
// IsPredicate returns if the argument is a predicate function.
func IsPredicate(in interface{}, inTypes ...reflect.Type) bool {
if len(inTypes) == 0 {
inTypes = append(inTypes, nil)
}
funcType := reflect.TypeOf(in)
result := funcType != nil && funcType.Kind() == reflect.Func
result = result && funcType.NumOut() == 1 && funcType.Out(0).Kind() == reflect.Bool
result = result && funcType.NumIn() == len(inTypes)
for i := 0; result && i < len(inTypes); i++ {
inType := inTypes[i]
result = inType == nil || inType.ConvertibleTo(funcType.In(i))
}
return result
}
// IsEqual returns if the two objects are equal
func IsEqual(expected interface{}, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
if exp, ok := expected.([]byte); ok {
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return true
}
return bytes.Equal(exp, act)
}
return reflect.DeepEqual(expected, actual)
}
// IsType returns if the two objects are in the same type
func IsType(expected interface{}, actual interface{}) bool {
return IsEqual(reflect.TypeOf(expected), reflect.TypeOf(actual))
}
// Equal returns if the two objects are equal
func Equal(expected interface{}, actual interface{}) bool {
return IsEqual(expected, actual)
}
// NotEqual returns if the two objects are not equal
func NotEqual(expected interface{}, actual interface{}) bool {
return !IsEqual(expected, actual)
}
// IsIteratee returns if the argument is an iteratee.
func IsIteratee(in interface{}) bool {
if in == nil {
return false
}
arrType := reflect.TypeOf(in)
kind := arrType.Kind()
return kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map
}
// IsCollection returns if the argument is a collection.
func IsCollection(in interface{}) bool {
arrType := reflect.TypeOf(in)
kind := arrType.Kind()
return kind == reflect.Array || kind == reflect.Slice
}
// SliceOf returns a slice which contains the element.
func SliceOf(in interface{}) interface{} {
value := reflect.ValueOf(in)
sliceType := reflect.SliceOf(reflect.TypeOf(in))
slice := reflect.New(sliceType)
sliceValue := reflect.MakeSlice(sliceType, 0, 0)
sliceValue = reflect.Append(sliceValue, value)
slice.Elem().Set(sliceValue)
return slice.Elem().Interface()
}
// Any returns true if any element of the iterable is not empty. If the iterable is empty, return False.
func Any(objs ...interface{}) bool {
if len(objs) == 0 {
return false
}
for _, obj := range objs {
if !IsEmpty(obj) {
return true
}
}
return false
}
// All returns true if all elements of the iterable are not empty (or if the iterable is empty)
func All(objs ...interface{}) bool {
if len(objs) == 0 {
return true
}
for _, obj := range objs {
if IsEmpty(obj) {
return false
}
}
return true
}
// IsEmpty returns if the object is considered as empty or not.
func IsEmpty(obj interface{}) bool {
if obj == nil || obj == "" || obj == false {
return true
}
for _, v := range numericZeros {
if obj == v {
return true
}
}
objValue := reflect.ValueOf(obj)
switch objValue.Kind() {
case reflect.Map:
fallthrough
case reflect.Slice, reflect.Chan:
return objValue.Len() == 0
case reflect.Struct:
return reflect.DeepEqual(obj, ZeroOf(obj))
case reflect.Ptr:
if objValue.IsNil() {
return true
}
obj = redirectValue(objValue).Interface()
return reflect.DeepEqual(obj, ZeroOf(obj))
}
return false
}
// IsZero returns if the object is considered as zero value
func IsZero(obj interface{}) bool {
if obj == nil || obj == "" || obj == false {
return true
}
for _, v := range numericZeros {
if obj == v {
return true
}
}
return reflect.DeepEqual(obj, ZeroOf(obj))
}
// NotEmpty returns if the object is considered as non-empty or not.
func NotEmpty(obj interface{}) bool {
return !IsEmpty(obj)
}
// ZeroOf returns a zero value of an element.
func ZeroOf(in interface{}) interface{} {
if in == nil {
return nil
}
return reflect.Zero(reflect.TypeOf(in)).Interface()
}
// RandomInt generates a random int, based on a min and max values
func RandomInt(min, max int) int {
return min + rand.Intn(max-min)
}
// Shard will shard a string name
func Shard(str string, width int, depth int, restOnly bool) []string {
var results []string
for i := 0; i < depth; i++ {
results = append(results, str[(width*i):(width*(i+1))])
}
if restOnly {
results = append(results, str[(width*depth):])
} else {
results = append(results, str)
}
return results
}
var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
// RandomString returns a random string with a fixed length
func RandomString(n int, allowedChars ...[]rune) string {
var letters []rune
if len(allowedChars) == 0 {
letters = defaultLetters
} else {
letters = allowedChars[0]
}
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}