1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-22 12:30:18 +00:00
This commit is contained in:
eyalpost
2017-04-21 15:26:41 +03:00
parent 405c170d45
commit 9aedb4d05a
7 changed files with 264 additions and 8 deletions

101
param/methodparams.go Normal file
View File

@ -0,0 +1,101 @@
package param
import (
"fmt"
"reflect"
beecontext "github.com/astaxie/beego/context"
)
//Keeps param information to be auto passed to controller methods
type MethodParam struct {
name string
parser paramParser
location paramLocation
required bool
defValue interface{}
}
type paramLocation byte
const (
param paramLocation = iota
body
header
)
type MethodParamOption func(*MethodParam)
func Bool(name string, opts ...MethodParamOption) *MethodParam {
return newParam(name, boolParser{}, opts)
}
func String(name string, opts ...MethodParamOption) *MethodParam {
return newParam(name, stringParser{}, opts)
}
func Int(name string, opts ...MethodParamOption) *MethodParam {
return newParam(name, intParser{}, opts)
}
func newParam(name string, parser paramParser, opts []MethodParamOption) (param *MethodParam) {
param = &MethodParam{name: name, parser: parser}
for _, option := range opts {
option(param)
}
return
}
func ConvertParams(methodParams []*MethodParam, methodType 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 value interface{}
switch p.location {
case body:
strValue = string(ctx.Input.RequestBody)
case header:
strValue = ctx.Input.Header(p.name)
default:
strValue = ctx.Input.Query(p.name)
}
if strValue == "" {
if p.required {
ctx.Abort(400, "Missing argument "+p.name)
} else if p.defValue != nil {
value = p.defValue
} else {
value = p.parser.zeroValue()
}
} else {
var err error
value, err = p.parser.parse(strValue)
if err != nil {
//TODO: handle err
}
}
reflectValue, err := safeConvert(reflect.ValueOf(value), methodType.In(i))
if err != nil {
//TODO: handle err
}
result = append(result, reflectValue)
i++
}
return
}
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
}

15
param/options.go Normal file
View File

@ -0,0 +1,15 @@
package param
var InHeader MethodParamOption = func(p *MethodParam) {
p.location = header
}
var IsRequired MethodParamOption = func(p *MethodParam) {
p.required = true
}
func Default(defValue interface{}) MethodParamOption {
return func(p *MethodParam) {
p.defValue = defValue
}
}

41
param/parsers.go Normal file
View File

@ -0,0 +1,41 @@
package param
import "strconv"
type paramParser interface {
parse(value string) (interface{}, error)
zeroValue() interface{}
}
type boolParser struct {
}
func (p boolParser) parse(value string) (interface{}, error) {
return strconv.ParseBool(value)
}
func (p boolParser) zeroValue() interface{} {
return false
}
type stringParser struct {
}
func (p stringParser) parse(value string) (interface{}, error) {
return value, nil
}
func (p stringParser) zeroValue() interface{} {
return ""
}
type intParser struct {
}
func (p intParser) parse(value string) (interface{}, error) {
return strconv.Atoi(value)
}
func (p intParser) zeroValue() interface{} {
return 0
}