-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
152 lines (128 loc) · 4.52 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
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
package van
import (
"context"
"fmt"
"reflect"
)
var (
typeVan = reflect.TypeOf((*Van)(nil))
typeError = reflect.TypeOf((*error)(nil)).Elem()
typeContext = reflect.TypeOf((*context.Context)(nil)).Elem()
)
func isStructPtr(t reflect.Type) bool {
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
}
func validateProviderSignature(t reflect.Type) error {
switch {
case t.Kind() != reflect.Func:
return fmt.Errorf("provider must be a function, got %s", t.String())
case t.NumIn() > maxArgs:
return fmt.Errorf("provider must have at most %d arguments, got %d", maxArgs, t.NumIn())
case t.NumOut() != 2:
return fmt.Errorf("provider must have two return values, got %d", t.NumOut())
case t.Out(0).Kind() != reflect.Interface:
return fmt.Errorf("provider's first return value must be an interface, got %s", t.Out(0).String())
case !t.Out(1).Implements(typeError):
return fmt.Errorf("provider's second return value must be an error, got %s", t.Out(1).String())
}
if err := validateDependencyArgs(t, 0); err != nil {
return err
}
return nil
}
func validateHandlerSignature(t reflect.Type) error {
switch {
case t.Kind() != reflect.Func:
return fmt.Errorf("handler must be a function, got %s", t.String())
case t.NumIn() < 2:
return fmt.Errorf("handler must have at least 2 arguments, got %s", fmt.Sprint(t.NumIn()))
case t.NumIn() > maxArgs:
return fmt.Errorf("handler must have at most %d arguments, got %d", maxArgs, t.NumIn())
case t.In(0) != typeContext:
return fmt.Errorf("handler's first argument must be context.Context, got %s", t.In(0).String())
case !isStructPtr(t.In(1)):
return fmt.Errorf("handler's second argument must be a struct pointer, got %s", t.In(1).String())
case t.NumOut() != 1:
return fmt.Errorf("handler must have one return value, got %s", fmt.Sprint(t.NumOut()))
case !t.Out(0).Implements(typeError):
return fmt.Errorf("handler's return type must be error, got %s", t.Out(0).String())
}
if err := validateDependencyArgs(t, 2); err != nil {
return err
}
return nil
}
func validateListenerSignature(t reflect.Type) error {
switch {
case t.Kind() != reflect.Func:
return fmt.Errorf("handler must be a function, got %s", t.String())
case t.NumIn() < 2:
return fmt.Errorf("handler must have at least 2 arguments, got %s", fmt.Sprint(t.NumIn()))
case t.NumIn() > maxArgs:
return fmt.Errorf("handler must have at most %d arguments, got %d", maxArgs, t.NumIn())
case t.In(0) != typeContext:
return fmt.Errorf("handler's first argument must be context.Context, got %s", t.In(0).String())
case t.In(1).Kind() != reflect.Struct:
return fmt.Errorf("handler's second argument must be a struct, got %s", t.In(1).String())
case t.NumOut() != 0:
return fmt.Errorf("event handler should not have any return values")
}
if err := validateDependencyArgs(t, 2); err != nil {
return err
}
return nil
}
func validateExecLambdaSignature(t reflect.Type) error {
switch {
case t.Kind() != reflect.Func:
return fmt.Errorf("function must be a function, got %s", t.String())
case t.NumIn() > maxArgs:
return fmt.Errorf("function must have at most %d arguments, got %d", maxArgs, t.NumIn())
case t.NumOut() != 1:
return fmt.Errorf("function must have one return value, got %s", fmt.Sprint(t.NumOut()))
case !t.Out(0).Implements(typeError):
return fmt.Errorf("return value must be an error, got %s", t.Out(0).String())
}
if err := validateDependencyArgs(t, 0); err != nil {
return err
}
return nil
}
func validateDependencyArgs(t reflect.Type, start int) error {
for i := start; i < t.NumIn(); i++ {
argType := t.In(i)
switch argType.Kind() {
case reflect.Interface:
continue
case reflect.Ptr:
if argType != typeVan {
return fmt.Errorf("argument %d must be an interface, struct or *van.Van, got %s", i, argType.String())
}
case reflect.Struct:
if err := validateDependencyStruct(argType); err != nil {
return fmt.Errorf("error in dependency struct argument %d: %w", i, err)
}
continue
default:
return fmt.Errorf("argument %d must be an interface, struct or *van.Van, got %s", i, argType.String())
}
}
return nil
}
func validateDependencyStruct(t reflect.Type) error {
for _, f := range reflect.VisibleFields(t) {
if !f.IsExported() {
return fmt.Errorf("field %s must be exported", f.Name)
}
if f.Type.Kind() != reflect.Interface {
return fmt.Errorf("field %s must be an interface, got %s", f.Name, f.Type.String())
}
}
return nil
}
func toError(v reflect.Value) error {
if v.IsNil() {
return nil
}
return v.Interface().(error)
}