delete strcut map
I think if user should set field in controller, there's no need to have thie feature
Showing
1 changed file
with
0 additions
and
86 deletions
| ... | @@ -8,14 +8,9 @@ import ( | ... | @@ -8,14 +8,9 @@ import ( |
| 8 | "reflect" | 8 | "reflect" |
| 9 | "regexp" | 9 | "regexp" |
| 10 | "runtime" | 10 | "runtime" |
| 11 | "strconv" | ||
| 12 | "strings" | 11 | "strings" |
| 13 | ) | 12 | ) |
| 14 | 13 | ||
| 15 | var ( | ||
| 16 | sc *Controller = &Controller{} | ||
| 17 | ) | ||
| 18 | |||
| 19 | type controllerInfo struct { | 14 | type controllerInfo struct { |
| 20 | pattern string | 15 | pattern string |
| 21 | regex *regexp.Regexp | 16 | regex *regexp.Regexp |
| ... | @@ -189,85 +184,6 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu | ... | @@ -189,85 +184,6 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu |
| 189 | }) | 184 | }) |
| 190 | } | 185 | } |
| 191 | 186 | ||
| 192 | func StructMap(vc reflect.Value, r *http.Request) error { | ||
| 193 | for k, t := range r.Form { | ||
| 194 | v := t[0] | ||
| 195 | names := strings.Split(k, ".") | ||
| 196 | var value reflect.Value = vc | ||
| 197 | for i, name := range names { | ||
| 198 | name = strings.Title(name) | ||
| 199 | if i == 0 { | ||
| 200 | if reflect.ValueOf(sc).Elem().FieldByName(name).IsValid() { | ||
| 201 | Trace("Controller's property should not be changed by mapper.") | ||
| 202 | break | ||
| 203 | } | ||
| 204 | } | ||
| 205 | if value.Kind() != reflect.Struct { | ||
| 206 | Trace(fmt.Sprintf("arg error, value kind is %v", value.Kind())) | ||
| 207 | break | ||
| 208 | } | ||
| 209 | |||
| 210 | if i != len(names)-1 { | ||
| 211 | value = value.FieldByName(name) | ||
| 212 | if !value.IsValid() { | ||
| 213 | Trace(fmt.Sprintf("(%v value is not valid %v)", name, value)) | ||
| 214 | break | ||
| 215 | } | ||
| 216 | } else { | ||
| 217 | tv := value.FieldByName(name) | ||
| 218 | if !tv.IsValid() { | ||
| 219 | Trace(fmt.Sprintf("struct %v has no field named %v", value, name)) | ||
| 220 | break | ||
| 221 | } | ||
| 222 | if !tv.CanSet() { | ||
| 223 | Trace("can not set " + k) | ||
| 224 | break | ||
| 225 | } | ||
| 226 | var l interface{} | ||
| 227 | switch k := tv.Kind(); k { | ||
| 228 | case reflect.String: | ||
| 229 | l = v | ||
| 230 | case reflect.Bool: | ||
| 231 | l = (v == "true") | ||
| 232 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32: | ||
| 233 | x, err := strconv.Atoi(v) | ||
| 234 | if err != nil { | ||
| 235 | Trace("arg " + v + " as int: " + err.Error()) | ||
| 236 | break | ||
| 237 | } | ||
| 238 | l = x | ||
| 239 | case reflect.Int64: | ||
| 240 | x, err := strconv.ParseInt(v, 10, 64) | ||
| 241 | if err != nil { | ||
| 242 | Trace("arg " + v + " as int: " + err.Error()) | ||
| 243 | break | ||
| 244 | } | ||
| 245 | l = x | ||
| 246 | case reflect.Float32, reflect.Float64: | ||
| 247 | x, err := strconv.ParseFloat(v, 64) | ||
| 248 | if err != nil { | ||
| 249 | Trace("arg " + v + " as float64: " + err.Error()) | ||
| 250 | break | ||
| 251 | } | ||
| 252 | l = x | ||
| 253 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||
| 254 | x, err := strconv.ParseUint(v, 10, 64) | ||
| 255 | if err != nil { | ||
| 256 | Trace("arg " + v + " as int: " + err.Error()) | ||
| 257 | break | ||
| 258 | } | ||
| 259 | l = x | ||
| 260 | case reflect.Struct: | ||
| 261 | Trace("can not set an struct") | ||
| 262 | } | ||
| 263 | |||
| 264 | tv.Set(reflect.ValueOf(l)) | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | return nil | ||
| 269 | } | ||
| 270 | |||
| 271 | // AutoRoute | 187 | // AutoRoute |
| 272 | func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | 188 | func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 273 | defer func() { | 189 | defer func() { |
| ... | @@ -450,8 +366,6 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -450,8 +366,6 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 450 | //Invoke the request handler | 366 | //Invoke the request handler |
| 451 | vc := reflect.New(runrouter.controllerType) | 367 | vc := reflect.New(runrouter.controllerType) |
| 452 | 368 | ||
| 453 | StructMap(vc.Elem(), r) | ||
| 454 | |||
| 455 | //call the controller init function | 369 | //call the controller init function |
| 456 | init := vc.MethodByName("Init") | 370 | init := vc.MethodByName("Init") |
| 457 | in := make([]reflect.Value, 2) | 371 | in := make([]reflect.Value, 2) | ... | ... |
-
Please register or sign in to post a comment