mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 04:51:30 +00:00
Merge pull request #59 from lunny/master
Add a basic map for url params to controller properties.
This commit is contained in:
commit
73f0f4f31f
87
router.go
87
router.go
@ -7,9 +7,14 @@ 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
|
||||||
@ -183,6 +188,85 @@ 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() {
|
||||||
@ -347,10 +431,13 @@ 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)
|
||||||
ct := &Context{ResponseWriter: w, Request: r, Params: params}
|
ct := &Context{ResponseWriter: w, Request: r, Params: params}
|
||||||
|
|
||||||
in[0] = reflect.ValueOf(ct)
|
in[0] = reflect.ValueOf(ct)
|
||||||
in[1] = reflect.ValueOf(runrouter.controllerType.Name())
|
in[1] = reflect.ValueOf(runrouter.controllerType.Name())
|
||||||
init.Call(in)
|
init.Call(in)
|
||||||
|
Loading…
Reference in New Issue
Block a user