1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 11:11:33 +00:00

all types implemented

This commit is contained in:
eyalpost 2017-04-23 01:33:50 +03:00
parent 9aedb4d05a
commit 89e01d125c
5 changed files with 123 additions and 54 deletions

View File

@ -5,6 +5,7 @@ import (
"reflect" "reflect"
beecontext "github.com/astaxie/beego/context" beecontext "github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs"
) )
//Keeps param information to be auto passed to controller methods //Keeps param information to be auto passed to controller methods
@ -13,7 +14,7 @@ type MethodParam struct {
parser paramParser parser paramParser
location paramLocation location paramLocation
required bool required bool
defValue interface{} defValue string
} }
type paramLocation byte type paramLocation byte
@ -38,6 +39,18 @@ func Int(name string, opts ...MethodParamOption) *MethodParam {
return newParam(name, intParser{}, opts) return newParam(name, intParser{}, opts)
} }
func Float(name string, opts ...MethodParamOption) *MethodParam {
return newParam(name, floatParser{}, opts)
}
func Time(name string, opts ...MethodParamOption) *MethodParam {
return newParam(name, timeParser{}, opts)
}
func Json(name string, opts ...MethodParamOption) *MethodParam {
return newParam(name, jsonParser{}, opts)
}
func newParam(name string, parser paramParser, opts []MethodParamOption) (param *MethodParam) { func newParam(name string, parser paramParser, opts []MethodParamOption) (param *MethodParam) {
param = &MethodParam{name: name, parser: parser} param = &MethodParam{name: name, parser: parser}
for _, option := range opts { for _, option := range opts {
@ -46,42 +59,47 @@ func newParam(name string, parser paramParser, opts []MethodParamOption) (param
return return
} }
func ConvertParams(methodParams []*MethodParam, methodType reflect.Type, ctx *beecontext.Context) (result []reflect.Value) { func convertParam(param *MethodParam, paramType reflect.Type, ctx *beecontext.Context) (result reflect.Value) {
result = make([]reflect.Value, 0, len(methodParams))
i := 0
for _, p := range methodParams {
var strValue string var strValue string
var value interface{} var reflectValue reflect.Value
switch p.location { switch param.location {
case body: case body:
strValue = string(ctx.Input.RequestBody) strValue = string(ctx.Input.RequestBody)
case header: case header:
strValue = ctx.Input.Header(p.name) strValue = ctx.Input.Header(param.name)
default: default:
strValue = ctx.Input.Query(p.name) strValue = ctx.Input.Query(param.name)
} }
if strValue == "" { if strValue == "" {
if p.required { if param.required {
ctx.Abort(400, "Missing argument "+p.name) ctx.Abort(400, fmt.Sprintf("Missing parameter %s", param.name))
} else if p.defValue != nil {
value = p.defValue
} else { } else {
value = p.parser.zeroValue() strValue = param.defValue
} }
}
if strValue == "" {
reflectValue = reflect.Zero(paramType)
} else { } else {
var err error value, err := param.parser.parse(strValue, paramType)
value, err = p.parser.parse(strValue)
if err != nil { if err != nil {
//TODO: handle err logs.Debug(fmt.Sprintf("Error converting param %s to type %s. Value: %s, Parser: %s, Error: %s", param.name, paramType.Name(), strValue, reflect.TypeOf(param.parser).Name(), err))
ctx.Abort(400, fmt.Sprintf("Invalid parameter %s. Can not convert %s to type %s", param.name, strValue, paramType.Name()))
} }
}
reflectValue, err := safeConvert(reflect.ValueOf(value), methodType.In(i)) reflectValue, err = safeConvert(reflect.ValueOf(value), paramType)
if err != nil { if err != nil {
//TODO: handle err panic(err)
} }
}
return reflectValue
}
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) result = append(result, reflectValue)
i++
} }
return return
} }

View File

@ -1,15 +1,25 @@
package param package param
var InHeader MethodParamOption = func(p *MethodParam) { import (
p.location = header "fmt"
} )
var IsRequired MethodParamOption = func(p *MethodParam) { var IsRequired MethodParamOption = func(p *MethodParam) {
p.required = true p.required = true
} }
var InHeader MethodParamOption = func(p *MethodParam) {
p.location = header
}
var InBody MethodParamOption = func(p *MethodParam) {
p.location = body
}
func Default(defValue interface{}) MethodParamOption { func Default(defValue interface{}) MethodParamOption {
return func(p *MethodParam) { return func(p *MethodParam) {
p.defValue = defValue if defValue != nil {
p.defValue = fmt.Sprintf("%v", defValue)
}
} }
} }

View File

@ -1,41 +1,67 @@
package param package param
import "strconv" import (
"encoding/json"
"reflect"
"strconv"
"time"
)
type paramParser interface { type paramParser interface {
parse(value string) (interface{}, error) parse(value string, toType reflect.Type) (interface{}, error)
zeroValue() interface{}
} }
type boolParser struct { type boolParser struct {
} }
func (p boolParser) parse(value string) (interface{}, error) { func (p boolParser) parse(value string, toType reflect.Type) (interface{}, error) {
return strconv.ParseBool(value) return strconv.ParseBool(value)
} }
func (p boolParser) zeroValue() interface{} {
return false
}
type stringParser struct { type stringParser struct {
} }
func (p stringParser) parse(value string) (interface{}, error) { func (p stringParser) parse(value string, toType reflect.Type) (interface{}, error) {
return value, nil return value, nil
} }
func (p stringParser) zeroValue() interface{} {
return ""
}
type intParser struct { type intParser struct {
} }
func (p intParser) parse(value string) (interface{}, error) { func (p intParser) parse(value string, toType reflect.Type) (interface{}, error) {
return strconv.Atoi(value) return strconv.Atoi(value)
} }
func (p intParser) zeroValue() interface{} { type floatParser struct {
return 0 }
func (p floatParser) parse(value string, toType reflect.Type) (interface{}, error) {
if toType.Kind() == reflect.Float32 {
return strconv.ParseFloat(value, 32)
}
return strconv.ParseFloat(value, 64)
}
type timeParser struct {
}
func (p timeParser) parse(value string, toType reflect.Type) (result interface{}, err error) {
result, err = time.Parse(time.RFC3339, value)
if err != nil {
result, err = time.Parse("2006-01-02", value)
}
return
}
type jsonParser struct {
}
func (p jsonParser) parse(value string, toType reflect.Type) (interface{}, error) {
pResult := reflect.New(toType)
v := pResult.Interface()
err := json.Unmarshal([]byte(value), v)
if err != nil {
return nil, err
}
return pResult.Elem().Interface(), nil
} }

View File

@ -25,3 +25,12 @@ func (s StatusCode) Error() string {
func (s StatusCode) Render(ctx *beecontext.Context) { func (s StatusCode) Render(ctx *beecontext.Context) {
ctx.Output.SetStatus(int(s)) ctx.Output.SetStatus(int(s))
} }
type statusCodeWithRender struct {
statusCode int
rendererFunc
}
func (s statusCodeWithRender) Error() string {
return strconv.Itoa(s.statusCode)
}

View File

@ -21,20 +21,26 @@ func Json(value interface{}, encoding ...bool) Renderer {
}) })
} }
func Error(err error) Renderer { func errorRenderer(err error) Renderer {
return rendererFunc(func(ctx *beecontext.Context) { return rendererFunc(func(ctx *beecontext.Context) {
ctx.Output.SetStatus(500) ctx.Output.SetStatus(500)
ctx.WriteString(err.Error()) ctx.WriteString(err.Error())
}) })
} }
func Redirect(localurl string) statusCodeWithRender {
return statusCodeWithRender{302, func(ctx *beecontext.Context) {
ctx.Redirect(302, localurl)
}}
}
func RenderMethodResult(result interface{}, ctx *beecontext.Context) { func RenderMethodResult(result interface{}, ctx *beecontext.Context) {
if result != nil { if result != nil {
renderer, ok := result.(Renderer) renderer, ok := result.(Renderer)
if !ok { if !ok {
err, ok := result.(error) err, ok := result.(error)
if ok { if ok {
renderer = Error(err) renderer = errorRenderer(err)
} else { } else {
renderer = Json(result) renderer = Json(result)
} }