-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-req.go
146 lines (133 loc) · 3.09 KB
/
convert-req.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
package goa
import (
"encoding/json"
"fmt"
"log"
"reflect"
"github.com/lovego/goa/convert"
)
type todoReqFields struct {
Param bool
Query bool
Header bool
Body bool
Ctx bool
}
func newReqConvertFunc(typ reflect.Type, path string) (
func(*Context) (reflect.Value, error), bool,
) {
isPtr := false
if typ.Kind() == reflect.Ptr {
isPtr = true
typ = typ.Elem()
}
param, todo := validateReqFields(typ, path)
return func(ctx *Context) (reflect.Value, error) {
ptr := reflect.New(typ)
req := ptr.Elem()
var err error
convert.Traverse(req, true, func(value reflect.Value, f reflect.StructField) bool {
switch f.Name {
case "Param":
if todo.Param {
convertNilPtr(value)
err = param.Convert(value, ctx.params)
}
case "Query":
if todo.Query {
convertNilPtr(value)
err = convert.Query(value, ctx.Request.URL.Query())
}
case "Header":
if todo.Header {
convertNilPtr(value)
err = convert.Header(value, ctx.Request.Header)
}
case "Body":
if todo.Body {
err = convertReqBody(value, ctx)
}
case "Session":
if sess := ctx.Get("session"); sess != nil {
err = convert.Session(value, reflect.ValueOf(sess))
}
case "Ctx":
if todo.Ctx {
value.Set(reflect.ValueOf(ctx))
}
}
return err == nil
})
if err != nil {
return reflect.Value{}, err
}
if isPtr {
return ptr, nil
} else {
return req, nil
}
}, todo.Ctx
}
var typeContextPtr = reflect.TypeOf((*Context)(nil))
func validateReqFields(typ reflect.Type, path string) (
param convert.ParamConverter, todo todoReqFields,
) {
if typ.Kind() != reflect.Struct {
log.Panic("req parameter of handler func must be a struct or struct pointer.")
}
convert.TraverseType(typ, func(f reflect.StructField) {
switch f.Name {
case "Param":
param = convert.GetParamConverter(f.Type, path)
todo.Param = true
case "Query":
if !isEmptyStruct(f.Type) {
convert.ValidateQuery(f.Type)
todo.Query = true
}
case "Header":
if !isEmptyStruct(f.Type) {
convert.ValidateHeader(f.Type)
todo.Header = true
}
case "Ctx":
if f.Type != typeContextPtr {
log.Panic("Ctx field of req parameter must be of type '*goa.Context'.")
}
todo.Ctx = true
case "Body":
if !isEmptyStruct(f.Type) {
todo.Body = true
}
case "Session": // can be any type, don't need to validate here.
case "Title", "Desc": // just for doc, does not care here
default:
log.Panicf("Unknown field: req.%s.", f.Name)
}
})
return
}
func convertReqBody(value reflect.Value, ctx *Context) error {
body, err := ctx.RequestBody()
if err != nil {
return err
}
if len(body) == 0 {
return nil
}
if err := json.Unmarshal(body, value.Addr().Interface()); err != nil {
return fmt.Errorf("req.Body: %s", err.Error())
}
return nil
}
func isEmptyStruct(typ reflect.Type) bool {
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
return typ.Kind() == reflect.Struct && typ.NumField() == 0
}
func convertNilPtr(v reflect.Value) {
if v.Kind() == reflect.Ptr && v.IsNil() && v.CanSet() {
v.Set(reflect.New(v.Type().Elem()))
}
}