-
Notifications
You must be signed in to change notification settings - Fork 39
/
operation_is.go
297 lines (257 loc) · 6.55 KB
/
operation_is.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
package gubrak
import (
"math"
"reflect"
"time"
)
func typeIs(data interface{}, types ...reflect.Kind) bool {
valueOfData := reflect.ValueOf(data)
for _, tipe := range types {
if tipe == valueOfData.Kind() {
return true
}
}
return false
}
// IsSlice is alias of IsSlice()
func IsSlice(data interface{}) bool {
return typeIs(data, reflect.Slice)
}
// IsArray is alias of IsArray()
func IsArray(data interface{}) bool {
return typeIs(data, reflect.Array)
}
// IsSliceOrArray will return true when type of the data is array/slice
func IsSliceOrArray(data interface{}) bool {
return IsSlice(data) || IsArray(data)
}
var IsArrayOrSlice = IsSliceOrArray
// IsBool will return true when type of the data is boolean
func IsBool(data interface{}) bool {
return typeIs(data,
reflect.Bool,
)
}
// IsChannel will return true when type of the data is channel
func IsChannel(data interface{}) bool {
return typeIs(data, reflect.Chan)
}
// IsDate will return true when type of the data is time.Time
func IsDate(data interface{}) bool {
if _, ok := data.(time.Time); ok {
return true
}
return false
}
// IsString will return true when type of the data is string
func IsString(data interface{}) bool {
return typeIs(data, reflect.String)
}
// IsEmptyString will return true when type of the data is string and it's empty
func IsEmptyString(data interface{}) bool {
if data == nil {
return true
}
if value, ok := data.(string); ok {
return value == ""
}
return false
}
// IsFloat will return true when type of the data is floating number
func IsFloat(data interface{}) bool {
return typeIs(data,
reflect.Float32,
reflect.Float64,
)
}
// IsFunction will return true when type of the data is closure/function
func IsFunction(data interface{}) bool {
return typeIs(data, reflect.Func)
}
// IsInt will return true when type of the data is numeric integer
func IsInt(data interface{}) bool {
return typeIs(data,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
)
}
// IsMap will return true when type of the data is hash map
func IsMap(data interface{}) bool {
return typeIs(data, reflect.Map)
}
// IsNil will return true when type of the data is nil
func IsNil(data interface{}) bool {
if data == nil {
return true
}
valueOfData := reflect.ValueOf(data)
switch valueOfData.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer, reflect.Struct:
if valueOfData.IsNil() {
return true
}
}
return false
}
// IsNumeric will return true when type of the data is numeric (float, uint, int)
func IsNumeric(data interface{}) bool {
return typeIs(data,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Float32,
reflect.Float64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
)
}
// IsPointer will return true when type of the data is pointer
func IsPointer(data interface{}) bool {
return typeIs(data, reflect.Ptr)
}
// IsStructObject will return true when type of the data is object from struct
func IsStructObject(data interface{}) bool {
return typeIs(data, reflect.Struct)
}
// IsTrue will return true when type of the data is bool, and the value is true
func IsTrue(data interface{}) bool {
if data == nil {
return false
}
if value, ok := data.(bool); ok {
return value == true
}
return false
}
// IsUint will return true when type of the data is uint
func IsUint(data interface{}) bool {
return typeIs(data,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
)
}
// IsZeroNumber will return true when type of the data is numeric and it's has 0 value
func IsZeroNumber(data interface{}) bool {
if data == nil {
return true
}
if value, ok := data.(float32); ok {
return value == 0
}
if value, ok := data.(float64); ok {
return value == 0
}
if value, ok := data.(int); ok {
return value == 0
}
if value, ok := data.(int8); ok {
return value == 0
}
if value, ok := data.(int16); ok {
return value == 0
}
if value, ok := data.(int32); ok {
return value == 0
}
if value, ok := data.(int64); ok {
return value == 0
}
if value, ok := data.(uint); ok {
return value == 0
}
if value, ok := data.(uint8); ok {
return value == 0
}
if value, ok := data.(uint16); ok {
return value == 0
}
if value, ok := data.(uint32); ok {
return value == 0
}
if value, ok := data.(uint64); ok {
return value == 0
}
if value, ok := data.(uintptr); ok {
return value == 0
}
if value, ok := data.(complex64); ok {
value128 := complex128(value)
return math.Float64bits(real(value128)) == 0 && math.Float64bits(imag(value128)) == 0
}
if value, ok := data.(complex128); ok {
return math.Float64bits(real(value)) == 0 && math.Float64bits(imag(value)) == 0
}
return false
}
// IsZeroValue reports whether value is the zero value for its type.
func IsZeroValue(data interface{}) bool {
if data == nil {
return true
} else if value, ok := data.(string); ok {
return value == ""
} else if value, ok := data.(bool); ok {
return value == false
} else if value, ok := data.(float32); ok {
return value == 0
} else if value, ok := data.(float64); ok {
return value == 0
} else if value, ok := data.(int); ok {
return value == 0
} else if value, ok := data.(int8); ok {
return value == 0
} else if value, ok := data.(int16); ok {
return value == 0
} else if value, ok := data.(int32); ok {
return value == 0
} else if value, ok := data.(int64); ok {
return value == 0
} else if value, ok := data.(uint); ok {
return value == 0
} else if value, ok := data.(uint8); ok {
return value == 0
} else if value, ok := data.(uint16); ok {
return value == 0
} else if value, ok := data.(uint32); ok {
return value == 0
} else if value, ok := data.(uint64); ok {
return value == 0
} else if value, ok := data.(uintptr); ok {
return value == 0
} else if value, ok := data.(complex64); ok {
value128 := complex128(value)
return math.Float64bits(real(value128)) == 0 && math.Float64bits(imag(value128)) == 0
} else if value, ok := data.(complex128); ok {
return math.Float64bits(real(value)) == 0 && math.Float64bits(imag(value)) == 0
} else {
if IsStructObject(data) {
if IsNil(data) {
return true
}
valueOfData := reflect.ValueOf(data)
for i := 0; i < valueOfData.NumField(); i++ {
if !IsZeroValue(valueOfData.Field(i).Interface()) {
return false
}
}
} else {
if IsNil(data) {
return true
}
}
}
return true
}
var IsEmpty = IsZeroValue