-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
72 lines (63 loc) · 1.65 KB
/
utils.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
package httpwrap
import (
"fmt"
"reflect"
)
var _errorType = reflect.TypeOf((*error)(nil)).Elem()
func isEmptyInterface(t reflect.Type) bool {
return t.String() == "interface {}"
}
func isError(t reflect.Type) bool {
return t.Implements(_errorType)
}
func typesOf(fn any) ([]reflect.Type, []reflect.Type) {
val := reflect.ValueOf(fn)
fnType := val.Type()
inTypes, outTypes := []reflect.Type{}, []reflect.Type{}
for i := 0; i < fnType.NumIn(); i++ {
inTypes = append(inTypes, fnType.In(i))
}
for i := 0; i < fnType.NumOut(); i++ {
outTypes = append(outTypes, fnType.Out(i))
}
return inTypes, outTypes
}
func validateBefore(in, _ []reflect.Type) error {
for i, t := range in {
if isEmptyInterface(t) {
return fmt.Errorf("before input #%d must not be empty interface", i)
}
}
if err := areTypesUnique(in); err != nil {
return fmt.Errorf("before input types must be unique: %v", err)
}
return nil
}
func validateMain(in, _ []reflect.Type) error {
for i, t := range in {
if isEmptyInterface(t) {
return fmt.Errorf("main input #%d must not be empty interface", i)
}
}
if err := areTypesUnique(in); err != nil {
return fmt.Errorf("main input types must be unique: %v", err)
}
// TODO: Assert check that main returns a non-error output?
return nil
}
func validateAfter(in, _ []reflect.Type) error {
if err := areTypesUnique(in); err != nil {
return fmt.Errorf("after input types must be unique: %v", err)
}
return nil
}
func areTypesUnique(ts []reflect.Type) error {
m := map[reflect.Type]int{}
for i, t := range ts {
if j, found := m[t]; found {
return fmt.Errorf("types %d and %d are equal", j, i)
}
m[t] = i
}
return nil
}