mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 23:00:50 +00:00
small fixes
This commit is contained in:
parent
cbd831042a
commit
4cba78afd9
@ -8,6 +8,15 @@ import (
|
||||
"github.com/astaxie/beego/logs"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func convertParam(param *MethodParam, paramType reflect.Type, ctx *beecontext.Context) (result reflect.Value) {
|
||||
paramValue := getParamValue(param, ctx)
|
||||
if paramValue == "" {
|
||||
@ -18,7 +27,7 @@ func convertParam(param *MethodParam, paramType reflect.Type, ctx *beecontext.Co
|
||||
}
|
||||
}
|
||||
|
||||
reflectValue, err := parseValue(paramValue, paramType)
|
||||
reflectValue, err := parseValue(param, paramValue, paramType)
|
||||
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))
|
||||
@ -43,11 +52,12 @@ func getParamValue(param *MethodParam, ctx *beecontext.Context) string {
|
||||
}
|
||||
}
|
||||
|
||||
func parseValue(paramValue string, paramType reflect.Type) (result reflect.Value, err error) {
|
||||
func parseValue(param *MethodParam, paramValue string, paramType reflect.Type) (result reflect.Value, err error) {
|
||||
if paramValue == "" {
|
||||
return reflect.Zero(paramType), nil
|
||||
} else {
|
||||
value, err := parse(paramValue, paramType)
|
||||
parser := getParser(param, paramType)
|
||||
value, err := parser.parse(paramValue, paramType)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
@ -56,15 +66,6 @@ func parseValue(paramValue string, paramType reflect.Type) (result reflect.Value
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func safeConvert(value reflect.Value, t reflect.Type) (result reflect.Value, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
|
@ -12,12 +12,7 @@ type paramParser interface {
|
||||
parse(value string, toType reflect.Type) (interface{}, error)
|
||||
}
|
||||
|
||||
func parse(value string, t reflect.Type) (interface{}, error) {
|
||||
parser := getParser(t)
|
||||
return parser.parse(value, t)
|
||||
}
|
||||
|
||||
func getParser(t reflect.Type) paramParser {
|
||||
func getParser(param *MethodParam, t reflect.Type) paramParser {
|
||||
switch t.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
@ -26,7 +21,10 @@ func getParser(t reflect.Type) paramParser {
|
||||
if t.Elem().Kind() == reflect.Uint8 { //treat []byte as string
|
||||
return stringParser{}
|
||||
}
|
||||
elemParser := getParser(t.Elem())
|
||||
if param.location == body {
|
||||
return jsonParser{}
|
||||
}
|
||||
elemParser := getParser(param, t.Elem())
|
||||
if elemParser == (jsonParser{}) {
|
||||
return elemParser
|
||||
}
|
||||
@ -38,7 +36,7 @@ func getParser(t reflect.Type) paramParser {
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return floatParser{}
|
||||
case reflect.Ptr:
|
||||
elemParser := getParser(t.Elem())
|
||||
elemParser := getParser(param, t.Elem())
|
||||
if elemParser == (jsonParser{}) {
|
||||
return elemParser
|
||||
}
|
||||
|
@ -199,12 +199,11 @@ func parseComment(lines []*ast.Comment) (pc *parsedComment, err error) {
|
||||
logs.Error("Invalid @Param format. Needs at least 4 parameters")
|
||||
}
|
||||
p := parsedParam{}
|
||||
names := strings.Split(pv[0], "=")
|
||||
funcParamName := names[0]
|
||||
names := strings.SplitN(pv[0], "=>", 2)
|
||||
p.name = names[0]
|
||||
funcParamName := p.name
|
||||
if len(names) > 1 {
|
||||
p.name = names[1]
|
||||
} else {
|
||||
p.name = funcParamName
|
||||
funcParamName = names[1]
|
||||
}
|
||||
p.location = pv[1]
|
||||
p.datatype = pv[2]
|
||||
|
Loading…
Reference in New Issue
Block a user