-
Notifications
You must be signed in to change notification settings - Fork 39
/
operation_chainable_helper.go
326 lines (261 loc) · 8.32 KB
/
operation_chainable_helper.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
318
319
320
321
322
323
324
325
326
package gubrak
import (
"errors"
"fmt"
"reflect"
)
func inspectFunc(err *error, data interface{}) (reflect.Value, reflect.Type) {
var dataValue reflect.Value
var dataValueType reflect.Type
dataValue = reflect.ValueOf(data)
if dataValue.Kind() == reflect.Ptr {
dataValue = dataValue.Elem()
}
if dataValue.Kind() != reflect.Func {
*err = errors.New("callback should be function")
return dataValue, dataValueType
}
dataValueType = dataValue.Type()
return dataValue, dataValueType
}
func inspectData(data interface{}) (reflect.Value, reflect.Type, reflect.Kind, int) {
var dataValue reflect.Value
var dataValueType reflect.Type
var dataValueKind reflect.Kind
dataValueLen := 0
if data != nil {
dataValue = reflect.ValueOf(data)
dataValueType = dataValue.Type()
dataValueKind = dataValue.Kind()
if dataValueKind == reflect.Ptr {
dataValue = dataValue.Elem()
}
if dataValueKind == reflect.Slice || dataValueKind == reflect.Array {
dataValueLen = dataValue.Len()
} else if dataValueKind == reflect.Map {
dataValueLen = len(dataValue.MapKeys())
}
}
return dataValue, dataValueType, dataValueKind, dataValueLen
}
func makeSlice(valueType reflect.Type, args ...int) reflect.Value {
sliceLen := 0
sliceCap := 0
if len(args) > 0 {
sliceLen = args[0]
if len(args) > 1 {
sliceCap = args[1]
}
}
if valueType.Kind() == reflect.Array {
sliceUnaddresable := reflect.MakeSlice(reflect.SliceOf(valueType.Elem()), 0, 0)
sliceAddressable := reflect.New(sliceUnaddresable.Type())
sliceAddressable.Elem().Set(sliceUnaddresable)
return sliceAddressable.Elem()
}
return reflect.MakeSlice(valueType, sliceLen, sliceCap)
}
func validateFuncInputForSliceLoop(err *error, funcType reflect.Type, data reflect.Value) int {
funcTypeNumIn := funcType.NumIn()
if funcTypeNumIn == 0 || funcTypeNumIn >= 3 {
*err = errors.New("callback must only have one or two parameters")
return funcTypeNumIn
}
if funcType.In(0).Kind() != data.Type().Elem().Kind() {
*err = errors.New("callback 1st parameter's data type should be same with slice element data type")
return funcTypeNumIn
}
if funcTypeNumIn == 2 {
if funcType.In(1).Kind() != reflect.Int {
*err = errors.New("callback 2nd parameter's data type should be int")
return funcTypeNumIn
}
}
return funcTypeNumIn
}
func validateFuncInputForSliceLoopWithoutIndex(err *error, funcType reflect.Type, data reflect.Value) {
if funcType.NumIn() != 1 {
*err = errors.New("callback must only have one parameters")
return
}
if funcType.In(0).Kind() != data.Type().Elem().Kind() {
*err = errors.New("callback parameter's data type should be same with slice data type")
}
}
func validateFuncInputForCollectionLoop(err *error, funcType reflect.Type, data reflect.Value) int {
funcTypeNumIn := funcType.NumIn()
if funcTypeNumIn == 0 || funcTypeNumIn >= 3 {
*err = errors.New("callback must only have one or two parameters")
return funcTypeNumIn
}
if funcType.In(0).Kind() != data.Type().Elem().Kind() {
*err = errors.New("callback 1st parameter's data type should be same with map value data type")
return funcTypeNumIn
}
if funcTypeNumIn == 2 {
if funcType.In(1).Kind() != data.Type().Key().Kind() {
*err = errors.New("callback 2nd parameter's data type should be same with map key type")
return funcTypeNumIn
}
}
return funcTypeNumIn
}
func validateFuncOutputNone(err *error, funcType reflect.Type) {
callbackTypeNumOut := funcType.NumOut()
if callbackTypeNumOut != 0 {
*err = errors.New("callback should not have return value")
}
}
func validateFuncOutputOneVarDynamic(err *error, funcType reflect.Type) int {
callbackTypeNumOut := funcType.NumOut()
if callbackTypeNumOut != 1 {
*err = errors.New("callback return value should only be 1 variable")
return callbackTypeNumOut
}
return callbackTypeNumOut
}
func validateFuncOutputOneVarBool(err *error, callbackType reflect.Type, isMust bool) int {
isOptional := !isMust
message := "callback return value should be one variable with bool type"
if isOptional {
message = "callback return value data type should be bool, ... or no return value at all"
}
callbackTypeNumOut := callbackType.NumOut()
if callbackTypeNumOut == 1 {
if callbackType.Out(0).Kind() != reflect.Bool {
*err = errors.New(message)
return callbackTypeNumOut
}
} else {
if isOptional {
if callbackTypeNumOut > 1 {
*err = errors.New(message)
return callbackTypeNumOut
}
} else {
*err = errors.New(message)
return callbackTypeNumOut
}
}
return callbackTypeNumOut
}
func forEachSlice(slice reflect.Value, sliceLen int, eachCallback func(reflect.Value, int)) {
forEachSliceStoppable(slice, sliceLen, func(each reflect.Value, i int) bool {
eachDataValue := slice.Index(i)
eachCallback(eachDataValue, i)
return true
})
}
func forEachSliceStoppable(slice reflect.Value, sliceLen int, eachCallback func(reflect.Value, int) bool) {
for i := 0; i < sliceLen; i++ {
eachDataValue := slice.Index(i)
shouldContinue := eachCallback(eachDataValue, i)
if !shouldContinue {
return
}
}
}
func forEachCollection(collection reflect.Value, keys []reflect.Value, eachCallback func(reflect.Value, reflect.Value, int)) {
forEachCollectionStoppable(collection, keys, func(value, key reflect.Value, i int) bool {
eachCallback(value, key, i)
return true
})
}
func forEachCollectionStoppable(collection reflect.Value, keys []reflect.Value, eachCallback func(reflect.Value, reflect.Value, int) bool) {
for i, key := range keys {
shouldContinue := eachCallback(collection.MapIndex(key), key, i)
if !shouldContinue {
return
}
}
}
func callFuncSliceLoop(funcToCall, param reflect.Value, i int, numIn int) []reflect.Value {
if numIn == 1 {
return funcToCall.Call([]reflect.Value{param})
}
return funcToCall.Call([]reflect.Value{param, reflect.ValueOf(i)})
}
func callFuncCollectionLoop(funcToCall, value, key reflect.Value, numIn int) []reflect.Value {
if numIn == 1 {
return funcToCall.Call([]reflect.Value{value})
}
return funcToCall.Call([]reflect.Value{value, key})
}
func isSlice(err *error, label string, dataValue ...reflect.Value) bool {
if len(dataValue) == 0 {
*err = fmt.Errorf("%s cannot be empty", label)
return false
} else if len(dataValue) == 1 {
if dataValue[0].Kind() == reflect.Slice || dataValue[0].Kind() == reflect.Array {
return true
}
*err = fmt.Errorf("%s must be slice", label)
return false
} else {
res := dataValue[0].Kind() == reflect.Slice || dataValue[0].Kind() == reflect.Array
for i, each := range dataValue {
if i > 0 {
res = res || (each.Kind() == reflect.Slice) || (each.Kind() == reflect.Array)
}
}
return res
}
}
func isNonNilData(err *error, label string, data interface{}) bool {
if data == nil {
*err = fmt.Errorf("%s cannot be nil", label)
return false
}
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() {
*err = fmt.Errorf("%s cannot be nil", label)
return false
}
}
return true
}
func isZeroOrPositiveNumber(err *error, label string, size int) bool {
if size < 0 {
*err = fmt.Errorf("%s must not be negative number", label)
return false
} else if size == 0 {
return true
}
return true
}
func isPositiveNumber(err *error, label string, size int) bool {
if size < 0 {
*err = fmt.Errorf("%s must be positive number", label)
return false
} else if size == 0 {
*err = fmt.Errorf("%s must be positive number", label)
return false
}
return true
}
func isLeftShouldBeGreaterOrEqualThanRight(err *error, labelLeft string, valueLeft int, labelRight string, valueRight int) bool {
if valueLeft < valueRight {
*err = fmt.Errorf("%s should be greater than %s", labelLeft, labelRight)
return false
}
return true
}
func isTypeEqual(err *error, labelLeft string, typeLeft reflect.Type, labelRight string, typeRight reflect.Type) bool {
if typeLeft != typeRight {
*err = fmt.Errorf("type of %s should be same with type of %s", labelLeft, labelRight)
return false
}
return true
}
func catch(err *error) {
if r := recover(); r != nil {
*err = fmt.Errorf("%v", r)
}
}
func catchWithCustomErrorMessage(err *error, callback func(string) string) {
if r := recover(); r != nil {
*err = errors.New(callback(fmt.Sprintf("%v", r)))
}
}