forked from sbstjn/appsync-resolvers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate.go
73 lines (61 loc) · 1.56 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
package resolvers
import (
"context"
"errors"
"fmt"
"reflect"
)
type validateFunc func(h reflect.Type) error
type validateList []validateFunc
func (v validateList) run(handler reflect.Type) error {
for _, validator := range v {
if err := validator(handler); err != nil {
return err
}
}
return nil
}
var validators = validateList{
func(h reflect.Type) error {
if kind := h.Kind(); kind != reflect.Func {
return fmt.Errorf("Resolver is not a function, got %s", kind)
}
return nil
},
func(h reflect.Type) error {
if num := h.NumIn(); num > 2 {
return fmt.Errorf("Resolver must not have more than two arguments, got %v", num)
}
return nil
},
func(h reflect.Type) error {
if h.NumIn() == 2 && !h.In(0).Implements(reflect.TypeOf((*context.Context)(nil)).Elem()) {
return errors.New("Resolver takes two arguments, but the first argument is not Context")
}
return nil
},
func(h reflect.Type) error {
if h.NumIn() > 0 && h.In(h.NumIn()-1).Kind() != reflect.Struct {
return errors.New("Resolver payload argument must be a struct")
}
return nil
},
func(h reflect.Type) error {
if num := h.NumOut(); num > 2 {
return fmt.Errorf("Resolver must not have more than two return values, got %v", num)
}
return nil
},
func(h reflect.Type) error {
if h.NumOut() < 1 {
return errors.New("Resolver must have at least one return value")
}
return nil
},
func(h reflect.Type) error {
if last := h.Out(h.NumOut() - 1); last.String() != "error" {
return errors.New("Last return value must be an error")
}
return nil
},
}