|
| 1 | +package revel |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// Controller registry and types. |
| 9 | +type ControllerType struct { |
| 10 | + Namespace string // The namespace of the controller |
| 11 | + ModuleSource *Module // The module for the controller |
| 12 | + Type reflect.Type |
| 13 | + Methods []*MethodType |
| 14 | + ControllerIndexes [][]int // FieldByIndex to all embedded *Controllers |
| 15 | + ControllerEvents *ControllerTypeEvents |
| 16 | +} |
| 17 | +type ControllerTypeEvents struct { |
| 18 | + Before, After, Finally, Panic []*ControllerFieldPath |
| 19 | +} |
| 20 | +// The controller field path provides the caller the ability to invoke the call |
| 21 | +// directly |
| 22 | +type ControllerFieldPath struct { |
| 23 | + IsPointer bool |
| 24 | + FieldIndexPath[]int |
| 25 | + FunctionCall reflect.Value |
| 26 | +} |
| 27 | + |
| 28 | +type MethodType struct { |
| 29 | + Name string |
| 30 | + Args []*MethodArg |
| 31 | + RenderArgNames map[int][]string |
| 32 | + lowerName string |
| 33 | + Index int |
| 34 | +} |
| 35 | + |
| 36 | +type MethodArg struct { |
| 37 | + Name string |
| 38 | + Type reflect.Type |
| 39 | +} |
| 40 | + |
| 41 | +// Adds the controller to the controllers map using its namespace, also adds it to the module list of controllers. |
| 42 | +// If the controller is in the main application it is added without its namespace as well. |
| 43 | +func AddControllerType(moduleSource *Module,controllerType reflect.Type,methods []*MethodType) (newControllerType *ControllerType) { |
| 44 | + if moduleSource==nil { |
| 45 | + moduleSource = appModule |
| 46 | + } |
| 47 | + |
| 48 | + newControllerType = &ControllerType{ModuleSource:moduleSource,Type:controllerType,Methods:methods,ControllerIndexes:findControllers(controllerType)} |
| 49 | + newControllerType.ControllerEvents = NewControllerTypeEvents(newControllerType) |
| 50 | + newControllerType.Namespace = moduleSource.Namespace() |
| 51 | + controllerName := newControllerType.Name() |
| 52 | + |
| 53 | + // Store the first controller only in the controllers map with the unmapped namespace. |
| 54 | + if _, found := controllers[controllerName]; !found { |
| 55 | + controllers[controllerName] = newControllerType |
| 56 | + newControllerType.ModuleSource.AddController(newControllerType) |
| 57 | + if newControllerType.ModuleSource == appModule { |
| 58 | + // Add the controller mapping into the global namespace |
| 59 | + controllers[newControllerType.ShortName()] = newControllerType |
| 60 | + } |
| 61 | + } else { |
| 62 | + controllerLog.Errorf("Error, attempt to register duplicate controller as %s",controllerName) |
| 63 | + } |
| 64 | + controllerLog.Debugf("Registered controller: %s", controllerName) |
| 65 | + |
| 66 | + return |
| 67 | +} |
| 68 | +// Method searches for a given exported method (case insensitive) |
| 69 | +func (ct *ControllerType) Method(name string) *MethodType { |
| 70 | + lowerName := strings.ToLower(name) |
| 71 | + for _, method := range ct.Methods { |
| 72 | + if method.lowerName == lowerName { |
| 73 | + return method |
| 74 | + } |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// The controller name with the namespace |
| 80 | +func (ct *ControllerType) Name() (string) { |
| 81 | + return ct.Namespace + ct.ShortName() |
| 82 | +} |
| 83 | + |
| 84 | +// The controller name without the namespace |
| 85 | +func (ct *ControllerType) ShortName() (string) { |
| 86 | + return strings.ToLower(ct.Type.Name()) |
| 87 | +} |
| 88 | + |
| 89 | +func NewControllerTypeEvents(c *ControllerType) (ce *ControllerTypeEvents) { |
| 90 | + ce = &ControllerTypeEvents{} |
| 91 | + // Parse the methods for the controller type, assign any control methods |
| 92 | + checkType := c.Type |
| 93 | + ce.check(checkType, []int{}) |
| 94 | + return |
| 95 | +} |
| 96 | + |
| 97 | +// Add in before after panic and finally, recursive call |
| 98 | +// Befores are ordered in revers, everything else is in order of first encountered |
| 99 | +func (cte *ControllerTypeEvents) check(theType reflect.Type, fieldPath []int) { |
| 100 | + typeChecker := func(checkType reflect.Type) { |
| 101 | + for index := 0; index < checkType.NumMethod(); index++ { |
| 102 | + m := checkType.Method(index) |
| 103 | + // Must be two arguments, the second returns the controller type |
| 104 | + // Go cannot differentiate between promoted methods and |
| 105 | + // embedded methods, this allows the embedded method to be run |
| 106 | + // https://github.com/golang/go/issues/21162 |
| 107 | + if m.Type.NumOut() == 2 && m.Type.Out(1) == checkType { |
| 108 | + if checkType.Kind() == reflect.Ptr { |
| 109 | + controllerLog.Debug("Found controller type event method pointer","name", checkType.Elem().Name(),"methodname", m.Name) |
| 110 | + } else { |
| 111 | + controllerLog.Debug("Found controller type event method","name", checkType.Elem().Name(),"methodname", m.Name) |
| 112 | + } |
| 113 | + controllerFieldPath := newFieldPath(checkType.Kind() == reflect.Ptr, m.Func, fieldPath) |
| 114 | + switch strings.ToLower(m.Name) { |
| 115 | + case "before": |
| 116 | + cte.Before = append([]*ControllerFieldPath{controllerFieldPath}, cte.Before...) |
| 117 | + case "after": |
| 118 | + cte.After = append(cte.After, controllerFieldPath) |
| 119 | + case "panic": |
| 120 | + cte.Panic = append(cte.Panic, controllerFieldPath) |
| 121 | + case "finally": |
| 122 | + cte.Finally = append(cte.Finally, controllerFieldPath) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Check methods of both types |
| 129 | + typeChecker(theType) |
| 130 | + typeChecker(reflect.PtrTo(theType)) |
| 131 | + |
| 132 | + // Check for any sub controllers, ignore any pointers to controllers revel.Controller |
| 133 | + for i := 0; i < theType.NumField(); i++ { |
| 134 | + v := theType.Field(i) |
| 135 | + |
| 136 | + switch v.Type.Kind() { |
| 137 | + case reflect.Struct: |
| 138 | + cte.check(v.Type,append(fieldPath, i)) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | +func newFieldPath(isPointer bool, value reflect.Value, fieldPath []int) *ControllerFieldPath{ |
| 143 | + return &ControllerFieldPath{IsPointer:isPointer,FunctionCall:value,FieldIndexPath:fieldPath} |
| 144 | +} |
| 145 | + |
| 146 | +func (fieldPath *ControllerFieldPath) Invoke(value reflect.Value, input []reflect.Value) (result []reflect.Value) { |
| 147 | + for _,index := range fieldPath.FieldIndexPath { |
| 148 | + // You can only fetch fields from non pointers |
| 149 | + if value.Type().Kind() == reflect.Ptr { |
| 150 | + value = value.Elem().Field(index) |
| 151 | + } else { |
| 152 | + value = value.Field(index) |
| 153 | + } |
| 154 | + } |
| 155 | + if fieldPath.IsPointer && value.Type().Kind() != reflect.Ptr { |
| 156 | + value = value.Addr() |
| 157 | + } else if !fieldPath.IsPointer && value.Type().Kind() == reflect.Ptr { |
| 158 | + value = value.Elem() |
| 159 | + } |
| 160 | + |
| 161 | + return fieldPath.FunctionCall.Call(append([]reflect.Value{value}, input...)) |
| 162 | +} |
0 commit comments