mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 20:00:55 +00:00
delete strcut map
I think if user should set field in controller, there's no need to have thie feature
This commit is contained in:
parent
309f2e199b
commit
9e41d93184
86
router.go
86
router.go
@ -8,14 +8,9 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
sc *Controller = &Controller{}
|
|
||||||
)
|
|
||||||
|
|
||||||
type controllerInfo struct {
|
type controllerInfo struct {
|
||||||
pattern string
|
pattern string
|
||||||
regex *regexp.Regexp
|
regex *regexp.Regexp
|
||||||
@ -189,85 +184,6 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func StructMap(vc reflect.Value, r *http.Request) error {
|
|
||||||
for k, t := range r.Form {
|
|
||||||
v := t[0]
|
|
||||||
names := strings.Split(k, ".")
|
|
||||||
var value reflect.Value = vc
|
|
||||||
for i, name := range names {
|
|
||||||
name = strings.Title(name)
|
|
||||||
if i == 0 {
|
|
||||||
if reflect.ValueOf(sc).Elem().FieldByName(name).IsValid() {
|
|
||||||
Trace("Controller's property should not be changed by mapper.")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if value.Kind() != reflect.Struct {
|
|
||||||
Trace(fmt.Sprintf("arg error, value kind is %v", value.Kind()))
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if i != len(names)-1 {
|
|
||||||
value = value.FieldByName(name)
|
|
||||||
if !value.IsValid() {
|
|
||||||
Trace(fmt.Sprintf("(%v value is not valid %v)", name, value))
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tv := value.FieldByName(name)
|
|
||||||
if !tv.IsValid() {
|
|
||||||
Trace(fmt.Sprintf("struct %v has no field named %v", value, name))
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if !tv.CanSet() {
|
|
||||||
Trace("can not set " + k)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
var l interface{}
|
|
||||||
switch k := tv.Kind(); k {
|
|
||||||
case reflect.String:
|
|
||||||
l = v
|
|
||||||
case reflect.Bool:
|
|
||||||
l = (v == "true")
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
|
||||||
x, err := strconv.Atoi(v)
|
|
||||||
if err != nil {
|
|
||||||
Trace("arg " + v + " as int: " + err.Error())
|
|
||||||
break
|
|
||||||
}
|
|
||||||
l = x
|
|
||||||
case reflect.Int64:
|
|
||||||
x, err := strconv.ParseInt(v, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
Trace("arg " + v + " as int: " + err.Error())
|
|
||||||
break
|
|
||||||
}
|
|
||||||
l = x
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
x, err := strconv.ParseFloat(v, 64)
|
|
||||||
if err != nil {
|
|
||||||
Trace("arg " + v + " as float64: " + err.Error())
|
|
||||||
break
|
|
||||||
}
|
|
||||||
l = x
|
|
||||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
||||||
x, err := strconv.ParseUint(v, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
Trace("arg " + v + " as int: " + err.Error())
|
|
||||||
break
|
|
||||||
}
|
|
||||||
l = x
|
|
||||||
case reflect.Struct:
|
|
||||||
Trace("can not set an struct")
|
|
||||||
}
|
|
||||||
|
|
||||||
tv.Set(reflect.ValueOf(l))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AutoRoute
|
// AutoRoute
|
||||||
func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -450,8 +366,6 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
//Invoke the request handler
|
//Invoke the request handler
|
||||||
vc := reflect.New(runrouter.controllerType)
|
vc := reflect.New(runrouter.controllerType)
|
||||||
|
|
||||||
StructMap(vc.Elem(), r)
|
|
||||||
|
|
||||||
//call the controller init function
|
//call the controller init function
|
||||||
init := vc.MethodByName("Init")
|
init := vc.MethodByName("Init")
|
||||||
in := make([]reflect.Value, 2)
|
in := make([]reflect.Value, 2)
|
||||||
|
Loading…
Reference in New Issue
Block a user