forked from sashabaranov/go-fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_handler.go
More file actions
33 lines (28 loc) · 938 Bytes
/
validate_handler.go
File metadata and controls
33 lines (28 loc) · 938 Bytes
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
package fastapi
import (
"reflect"
"github.com/gin-gonic/gin"
)
func (router *Router) AddCall(path string, handler interface{}) {
validateHandler(handler)
router.routes[path] = handler
}
func validateHandler(handler interface{}) {
handlerType := reflect.TypeOf(handler)
ginCtxType := reflect.TypeOf(&gin.Context{})
errorInterface := reflect.TypeOf((*error)(nil)).Elem()
switch {
case handlerType.NumIn() != 2:
panic("Wrong number of arguments")
case handlerType.NumOut() != 2:
panic("Wrong number of return values")
case !handlerType.In(0).ConvertibleTo(ginCtxType):
panic("First argument should be *gin.Context!")
case handlerType.In(1).Kind() != reflect.Struct:
panic("Second argument must be a struct")
case !handlerType.Out(1).Implements(errorInterface):
panic("Second return value should be an error")
case handlerType.Out(0).Kind() != reflect.Struct:
panic("First return value must be a struct")
}
}