1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-18 11:50:55 +00:00
Beego/context/param/parsers.go

150 lines
3.5 KiB
Go
Raw Normal View History

2017-04-21 12:26:41 +00:00
package param
2017-04-22 22:33:50 +00:00
import (
"encoding/json"
"reflect"
"strconv"
2017-04-23 18:37:09 +00:00
"strings"
2017-04-22 22:33:50 +00:00
"time"
)
2017-04-21 12:26:41 +00:00
type paramParser interface {
2017-04-22 22:33:50 +00:00
parse(value string, toType reflect.Type) (interface{}, error)
2017-04-21 12:26:41 +00:00
}
2017-04-25 20:42:35 +00:00
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:
return intParser{}
case reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 { //treat []byte as string
return stringParser{}
}
2017-05-12 06:28:46 +00:00
if param.in == body {
2017-04-25 20:42:35 +00:00
return jsonParser{}
}
elemParser := getParser(param, t.Elem())
if elemParser == (jsonParser{}) {
return elemParser
}
return sliceParser(elemParser)
case reflect.Bool:
return boolParser{}
case reflect.String:
return stringParser{}
case reflect.Float32, reflect.Float64:
return floatParser{}
case reflect.Ptr:
2017-04-25 20:42:35 +00:00
elemParser := getParser(param, t.Elem())
if elemParser == (jsonParser{}) {
return elemParser
}
return ptrParser(elemParser)
default:
if t.PkgPath() == "time" && t.Name() == "Time" {
return timeParser{}
}
return jsonParser{}
}
}
2017-04-23 18:37:09 +00:00
type parserFunc func(value string, toType reflect.Type) (interface{}, error)
func (f parserFunc) parse(value string, toType reflect.Type) (interface{}, error) {
return f(value, toType)
}
2017-04-21 12:26:41 +00:00
type boolParser struct {
}
2017-04-22 22:33:50 +00:00
func (p boolParser) parse(value string, toType reflect.Type) (interface{}, error) {
2017-04-21 12:26:41 +00:00
return strconv.ParseBool(value)
}
type stringParser struct {
}
2017-04-22 22:33:50 +00:00
func (p stringParser) parse(value string, toType reflect.Type) (interface{}, error) {
2017-04-21 12:26:41 +00:00
return value, nil
}
type intParser struct {
}
2017-04-22 22:33:50 +00:00
func (p intParser) parse(value string, toType reflect.Type) (interface{}, error) {
2017-04-21 12:26:41 +00:00
return strconv.Atoi(value)
}
2017-04-22 22:33:50 +00:00
type floatParser struct {
}
func (p floatParser) parse(value string, toType reflect.Type) (interface{}, error) {
if toType.Kind() == reflect.Float32 {
2017-04-23 18:37:09 +00:00
res, err := strconv.ParseFloat(value, 32)
if err != nil {
return nil, err
}
return float32(res), nil
2017-04-22 22:33:50 +00:00
}
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
2017-04-21 12:26:41 +00:00
}
2017-04-23 18:37:09 +00:00
func sliceParser(elemParser paramParser) paramParser {
return parserFunc(func(value string, toType reflect.Type) (interface{}, error) {
values := strings.Split(value, ",")
result := reflect.MakeSlice(toType, 0, len(values))
elemType := toType.Elem()
for _, v := range values {
parsedValue, err := elemParser.parse(v, elemType)
if err != nil {
return nil, err
}
result = reflect.Append(result, reflect.ValueOf(parsedValue))
}
return result.Interface(), nil
})
}
func ptrParser(elemParser paramParser) paramParser {
return parserFunc(func(value string, toType reflect.Type) (interface{}, error) {
parsedValue, err := elemParser.parse(value, toType.Elem())
if err != nil {
return nil, err
}
newValPtr := reflect.New(toType.Elem())
newVal := reflect.Indirect(newValPtr)
convertedVal, err := safeConvert(reflect.ValueOf(parsedValue), toType.Elem())
if err != nil {
return nil, err
}
newVal.Set(convertedVal)
return newValPtr.Interface(), nil
})
}