-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidate.go
97 lines (87 loc) · 3.67 KB
/
validate.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
package gollection
import (
"fmt"
"reflect"
)
func (g *gollection) validateSlice(funcName string) (reflect.Value, error) {
sv := reflect.ValueOf(g.slice)
if sv.Kind() != reflect.Slice {
return reflect.Value{}, fmt.Errorf("gollection.%s called with non-slice value of type %T", funcName, g.slice)
}
return sv, nil
}
func (g *gollection) validateSliceOfSlice(funcName string) (reflect.Type, error) {
currentType := reflect.TypeOf(g.slice).Elem()
if currentType.Kind() != reflect.Slice {
return nil, fmt.Errorf("gollection.%s called with non-slice value of type %T", funcName, g.slice)
}
return currentType, nil
}
func (g *gollection) validateDistinctByFunc(f interface{}) (reflect.Value, reflect.Type, error) {
funcValue := reflect.ValueOf(f)
funcType := funcValue.Type()
if funcType.Kind() != reflect.Func || funcType.NumIn() != 1 || funcType.NumOut() != 1 {
return reflect.Value{}, nil, fmt.Errorf("gollection.DistinctBy called with invalid func. required func(in <T>) out <T> but supplied %v", funcType)
}
return funcValue, funcType, nil
}
func (g *gollection) validateFilterFunc(f interface{}) (reflect.Value, reflect.Type, error) {
funcValue := reflect.ValueOf(f)
funcType := funcValue.Type()
if funcType.Kind() != reflect.Func ||
funcType.NumIn() != 1 ||
funcType.NumOut() != 1 ||
funcType.Out(0).Kind() != reflect.Bool {
return reflect.Value{}, nil, fmt.Errorf("gollection.Filter called with invalid func. required func(in <T>) bool but supplied %v", funcType)
}
return funcValue, funcType, nil
}
func (g *gollection) validateFlatMapFunc(f interface{}) (reflect.Value, reflect.Type, error) {
funcValue := reflect.ValueOf(f)
funcType := funcValue.Type()
if funcType.Kind() != reflect.Func || funcType.NumIn() != 1 || funcType.NumOut() != 1 {
return reflect.Value{}, nil, fmt.Errorf("gollection.FlatMap called with invalid func. required func(in <T>) out <T> but supplied %v", funcType)
}
return funcValue, funcType, nil
}
func (g *gollection) validateFoldFunc(f interface{}) (reflect.Value, reflect.Type, error) {
funcValue := reflect.ValueOf(f)
funcType := funcValue.Type()
if funcType.Kind() != reflect.Func ||
funcType.NumIn() != 2 ||
funcType.NumOut() != 1 {
return reflect.Value{}, nil, fmt.Errorf("gollection.Fold called with invalid func. required func(in1, in2 <T>) out <T> but supplied %v", funcType)
}
return funcValue, funcType, nil
}
func (g *gollection) validateMapFunc(f interface{}) (reflect.Value, reflect.Type, error) {
funcValue := reflect.ValueOf(f)
funcType := funcValue.Type()
if funcType.Kind() != reflect.Func ||
funcType.NumIn() != 1 ||
funcType.NumOut() != 1 {
return reflect.Value{}, nil, fmt.Errorf("gollection.Map called with invalid func. required func(in <T>) out <T> but supplied %v", funcType)
}
return funcValue, funcType, nil
}
func (g *gollection) validateReduceFunc(f interface{}) (reflect.Value, reflect.Type, error) {
funcValue := reflect.ValueOf(f)
funcType := funcValue.Type()
if funcType.Kind() != reflect.Func ||
funcType.NumIn() != 2 ||
funcType.NumOut() != 1 {
return reflect.Value{}, nil, fmt.Errorf("gollection.Reduce called with invalid func. required func(in1, in2 <T>) out <T> but supplied %v", funcType)
}
return funcValue, funcType, nil
}
func (g *gollection) validateSortByFunc(f interface{}) (reflect.Value, reflect.Type, error) {
funcValue := reflect.ValueOf(f)
funcType := funcValue.Type()
if funcType.Kind() != reflect.Func ||
funcType.NumIn() != 2 ||
funcType.NumOut() != 1 ||
funcType.Out(0).Kind() != reflect.Bool {
return reflect.Value{}, nil, fmt.Errorf("gollection.SortBy called with invalid func. required func(in1, in2 <T>) bool but supplied %v", funcType)
}
return funcValue, funcType, nil
}