SetAction sets the assigns the Controller type, sets the action and initializes the controller.
(controllerName, methodName string, typeOfController *ControllerType)
| 387 | |
| 388 | // SetAction sets the assigns the Controller type, sets the action and initializes the controller. |
| 389 | func (c *Controller) SetTypeAction(controllerName, methodName string, typeOfController *ControllerType) error { |
| 390 | // Look up the controller and method types. |
| 391 | if typeOfController == nil { |
| 392 | if c.Type = ControllerTypeByName(controllerName, anyModule); c.Type == nil { |
| 393 | return errors.New("revel/controller: failed to find controller " + controllerName) |
| 394 | } |
| 395 | } else { |
| 396 | c.Type = typeOfController |
| 397 | } |
| 398 | |
| 399 | // Note method name is case insensitive search |
| 400 | if c.MethodType = c.Type.Method(methodName); c.MethodType == nil { |
| 401 | return errors.New("revel/controller: failed to find action " + controllerName + "." + methodName) |
| 402 | } |
| 403 | |
| 404 | c.Name, c.MethodName = c.Type.Type.Name(), c.MethodType.Name |
| 405 | c.Action = c.Name + "." + c.MethodName |
| 406 | |
| 407 | // Update Logger with controller and namespace |
| 408 | if c.Log != nil { |
| 409 | c.Log = c.Log.New("action", c.Action, "namespace", c.Type.Namespace) |
| 410 | } |
| 411 | |
| 412 | if RevelConfig.Controller.Reuse { |
| 413 | if _, ok := RevelConfig.Controller.CachedMap[c.Name]; !ok { |
| 414 | // Create a new stack for this controller |
| 415 | localType := c.Type.Type |
| 416 | RevelConfig.Controller.CachedMap[c.Name] = utils.NewStackLock( |
| 417 | RevelConfig.Controller.CachedStackSize, |
| 418 | RevelConfig.Controller.CachedStackMaxSize, |
| 419 | func() interface{} { |
| 420 | return reflect.New(localType).Interface() |
| 421 | }) |
| 422 | } |
| 423 | // Instantiate the controller. |
| 424 | c.AppController = RevelConfig.Controller.CachedMap[c.Name].Pop() |
| 425 | } else { |
| 426 | c.AppController = reflect.New(c.Type.Type).Interface() |
| 427 | } |
| 428 | c.setAppControllerFields() |
| 429 | |
| 430 | return nil |
| 431 | } |
| 432 | |
| 433 | func ControllerTypeByName(controllerName string, moduleSource *Module) (c *ControllerType) { |
| 434 | var found bool |
no test coverage detected