From 4cba78afd948470826c0bed5eda222f9824b8da7 Mon Sep 17 00:00:00 2001 From: eyalpost Date: Tue, 25 Apr 2017 23:42:35 +0300 Subject: [PATCH] small fixes --- context/param/conv.go | 25 +++++++++++++------------ context/param/parsers.go | 14 ++++++-------- parser.go | 9 ++++----- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/context/param/conv.go b/context/param/conv.go index 4938f45f..8e580c2e 100644 --- a/context/param/conv.go +++ b/context/param/conv.go @@ -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 { diff --git a/context/param/parsers.go b/context/param/parsers.go index 64cabe49..2b48e878 100644 --- a/context/param/parsers.go +++ b/context/param/parsers.go @@ -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 } diff --git a/parser.go b/parser.go index 17d9a5d2..66f51d0b 100644 --- a/parser.go +++ b/parser.go @@ -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]