2017-04-23 18:37:09 +00:00
|
|
|
package param
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
beecontext "github.com/astaxie/beego/context"
|
|
|
|
"github.com/astaxie/beego/logs"
|
|
|
|
)
|
|
|
|
|
2017-04-25 20:42:35 +00:00
|
|
|
func ConvertParams(methodParams []*MethodParam, methodType reflect.Type, ctx *beecontext.Context) (result []reflect.Value) {
|
|
|
|
result = make([]reflect.Value, 0, len(methodParams))
|
|
|
|
for i := 0; i < len(methodParams); i++ {
|
|
|
|
reflectValue := convertParam(methodParams[i], methodType.In(i), ctx)
|
|
|
|
result = append(result, reflectValue)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-23 18:37:09 +00:00
|
|
|
func convertParam(param *MethodParam, paramType reflect.Type, ctx *beecontext.Context) (result reflect.Value) {
|
2017-04-25 13:00:49 +00:00
|
|
|
paramValue := getParamValue(param, ctx)
|
|
|
|
if paramValue == "" {
|
|
|
|
if param.required {
|
|
|
|
ctx.Abort(400, fmt.Sprintf("Missing parameter %s", param.name))
|
|
|
|
} else {
|
|
|
|
paramValue = param.defValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 20:42:35 +00:00
|
|
|
reflectValue, err := parseValue(param, paramValue, paramType)
|
2017-04-25 13:00:49 +00:00
|
|
|
if err != nil {
|
|
|
|
logs.Debug(fmt.Sprintf("Error converting param %s to type %s. Value: %v, Error: %s", param.name, paramType, paramValue, err))
|
|
|
|
ctx.Abort(400, fmt.Sprintf("Invalid parameter %s. Can not convert %v to type %s", param.name, paramValue, paramType))
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflectValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func getParamValue(param *MethodParam, ctx *beecontext.Context) string {
|
2017-04-23 18:37:09 +00:00
|
|
|
switch param.location {
|
|
|
|
case body:
|
2017-04-25 13:00:49 +00:00
|
|
|
return string(ctx.Input.RequestBody)
|
2017-04-23 18:37:09 +00:00
|
|
|
case header:
|
2017-04-25 13:00:49 +00:00
|
|
|
return ctx.Input.Header(param.name)
|
|
|
|
// if strValue == "" && strings.Contains(param.name, "_") { //magically handle X-Headers?
|
|
|
|
// strValue = ctx.Input.Header(strings.Replace(param.name, "_", "-", -1))
|
|
|
|
// }
|
|
|
|
case path:
|
|
|
|
return ctx.Input.Query(":" + param.name)
|
2017-04-23 18:37:09 +00:00
|
|
|
default:
|
2017-04-25 13:00:49 +00:00
|
|
|
return ctx.Input.Query(param.name)
|
2017-04-23 18:37:09 +00:00
|
|
|
}
|
2017-04-25 13:00:49 +00:00
|
|
|
}
|
2017-04-23 18:37:09 +00:00
|
|
|
|
2017-04-25 20:42:35 +00:00
|
|
|
func parseValue(param *MethodParam, paramValue string, paramType reflect.Type) (result reflect.Value, err error) {
|
2017-04-25 13:00:49 +00:00
|
|
|
if paramValue == "" {
|
|
|
|
return reflect.Zero(paramType), nil
|
2017-04-23 18:37:09 +00:00
|
|
|
} else {
|
2017-04-25 20:42:35 +00:00
|
|
|
parser := getParser(param, paramType)
|
|
|
|
value, err := parser.parse(paramValue, paramType)
|
2017-04-23 18:37:09 +00:00
|
|
|
if err != nil {
|
2017-04-25 13:00:49 +00:00
|
|
|
return result, err
|
2017-04-23 18:37:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 13:00:49 +00:00
|
|
|
return safeConvert(reflect.ValueOf(value), paramType)
|
2017-04-23 18:37:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func safeConvert(value reflect.Value, t reflect.Type) (result reflect.Value, err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
var ok bool
|
|
|
|
err, ok = r.(error)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("%v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
result = value.Convert(t)
|
|
|
|
return
|
|
|
|
}
|