forked from EverythingMe/vertex
/
route.go
58 lines (43 loc) · 1.27 KB
/
route.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
package vertex
import (
"reflect"
"github.com/dvirsky/go-pylog/logging"
gorilla "github.com/gorilla/schema"
"github.com/EverythingMe/vertex/schema"
)
// A routing map for an API
type Routes []Route
// Route represents a single route (path) in the API and its handler and optional extra middleware
type Route struct {
Path string
Description string
Handler RequestHandler
Methods MethodFlag
Security SecurityScheme
Middleware []Middleware
Test Tester
Returns interface{}
Renderer Renderer
requestInfo schema.RequestInfo
}
func (r *Route) parseInfo(path string) error {
ri, err := schema.NewRequestInfo(reflect.TypeOf(r.Handler), path, r.Description, r.Returns)
if err != nil {
return err
}
// search for custom unmarshallers in the request info
for _, param := range ri.Params {
if param.Type.Kind() == reflect.Struct {
logging.Debug("Checking unmarshaller for %s", param.Type)
val := reflect.Zero(param.Type).Interface()
if unm, ok := val.(Unmarshaler); ok {
logging.Info("Registering unmarshaller for %#v", val)
schemaDecoder.RegisterConverter(val, gorilla.Converter(func(s string) reflect.Value {
return reflect.ValueOf(unm.UnmarshalRequestData(s))
}))
}
}
}
r.requestInfo = ri
return nil
}