mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
add some comments but I just released that we may not need controller's input field and we can bind directly via the targetStruct binder, next step is to implement that behavior
Former-commit-id: e2ed23e7c4f52237cf87148d9a85d01e89d479be
This commit is contained in:
parent
689b671bf9
commit
257f1318c9
|
@ -9,6 +9,15 @@ import (
|
||||||
"github.com/kataras/iris/core/router/macro"
|
"github.com/kataras/iris/core/router/macro"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// BaseController is the controller interface,
|
||||||
|
// which the main request `C` will implement automatically.
|
||||||
|
// End-dev doesn't need to have any knowledge of this if she/he doesn't want to implement
|
||||||
|
// a new Controller type.
|
||||||
|
// Controller looks the whole flow as one handler, so `ctx.Next`
|
||||||
|
// inside `BeginRequest` is not be respected.
|
||||||
|
// Alternative way to check if a middleware was procceed successfully
|
||||||
|
// and called its `ctx.Next` is the `ctx.Proceed(handler) bool`.
|
||||||
|
// You have to navigate to the `context/context#Proceed` function's documentation.
|
||||||
type BaseController interface {
|
type BaseController interface {
|
||||||
BeginRequest(context.Context)
|
BeginRequest(context.Context)
|
||||||
EndRequest(context.Context)
|
EndRequest(context.Context)
|
||||||
|
@ -59,8 +68,9 @@ func (c *C) BeginRequest(ctx context.Context) { c.Ctx = ctx }
|
||||||
// EndRequest does nothing, is here to complete the `BaseController` interface.
|
// EndRequest does nothing, is here to complete the `BaseController` interface.
|
||||||
func (c *C) EndRequest(ctx context.Context) {}
|
func (c *C) EndRequest(ctx context.Context) {}
|
||||||
|
|
||||||
|
// ControllerActivator returns a new controller type info description.
|
||||||
|
// Its functionality can be overriden by the end-dev.
|
||||||
type ControllerActivator struct {
|
type ControllerActivator struct {
|
||||||
Engine *Engine
|
|
||||||
// the router is used on the `Activate` and can be used by end-dev on the `OnActivate`
|
// the router is used on the `Activate` and can be used by end-dev on the `OnActivate`
|
||||||
// to register any custom controller's functions as handlers but we will need it here
|
// to register any custom controller's functions as handlers but we will need it here
|
||||||
// in order to not create a new type like `ActivationPayload` for the `OnActivate`.
|
// in order to not create a new type like `ActivationPayload` for the `OnActivate`.
|
||||||
|
@ -88,6 +98,14 @@ type ControllerActivator struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newControllerActivator(router router.Party, controller BaseController, bindValues ...reflect.Value) *ControllerActivator {
|
func newControllerActivator(router router.Party, controller BaseController, bindValues ...reflect.Value) *ControllerActivator {
|
||||||
|
var (
|
||||||
|
val = reflect.ValueOf(controller)
|
||||||
|
typ = val.Type()
|
||||||
|
|
||||||
|
// the full name of the controller, it's its type including the package path.
|
||||||
|
fullName = getNameOf(typ)
|
||||||
|
)
|
||||||
|
|
||||||
// the following will make sure that if
|
// the following will make sure that if
|
||||||
// the controller's has set-ed pointer struct fields by the end-dev
|
// the controller's has set-ed pointer struct fields by the end-dev
|
||||||
// we will include them to the bindings.
|
// we will include them to the bindings.
|
||||||
|
@ -95,31 +113,37 @@ func newControllerActivator(router router.Party, controller BaseController, bind
|
||||||
// the end-developer when declaring the controller,
|
// the end-developer when declaring the controller,
|
||||||
// activate listeners needs them in order to know if something set-ed already or not,
|
// activate listeners needs them in order to know if something set-ed already or not,
|
||||||
// look `BindTypeExists`.
|
// look `BindTypeExists`.
|
||||||
|
bindValues = append(lookupNonZeroFieldsValues(val), bindValues...)
|
||||||
var (
|
|
||||||
val = reflect.ValueOf(controller)
|
|
||||||
typ = val.Type()
|
|
||||||
|
|
||||||
fullName = getNameOf(typ)
|
|
||||||
)
|
|
||||||
|
|
||||||
c := &ControllerActivator{
|
c := &ControllerActivator{
|
||||||
|
// give access to the Router to the end-devs if they need it for some reason,
|
||||||
|
// i.e register done handlers.
|
||||||
Router: router,
|
Router: router,
|
||||||
Value: val,
|
Value: val,
|
||||||
Type: typ,
|
Type: typ,
|
||||||
FullName: fullName,
|
FullName: fullName,
|
||||||
|
// set some methods that end-dev cann't use accidentally
|
||||||
|
// to register a route via the `Handle`,
|
||||||
|
// all available exported and compatible methods
|
||||||
|
// are being appended to the slice at the `parseMethods`,
|
||||||
|
// if a new method is registered via `Handle` its function name
|
||||||
|
// is also appended to that slice.
|
||||||
reservedMethods: []string{
|
reservedMethods: []string{
|
||||||
"BeginRequest",
|
"BeginRequest",
|
||||||
"EndRequest",
|
"EndRequest",
|
||||||
"OnActivate",
|
"OnActivate",
|
||||||
},
|
},
|
||||||
input: append(lookupNonZeroFieldsValues(val), bindValues...),
|
// set the input as []reflect.Value in order to be able
|
||||||
|
// to check if a bind type is already exists, or even
|
||||||
|
// override the structBindings that are being generated later on.
|
||||||
|
input: bindValues,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.parseMethods()
|
c.parseMethods()
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checks if a method is already registered.
|
||||||
func (c *ControllerActivator) isReservedMethod(name string) bool {
|
func (c *ControllerActivator) isReservedMethod(name string) bool {
|
||||||
for _, s := range c.reservedMethods {
|
for _, s := range c.reservedMethods {
|
||||||
if s == name {
|
if s == name {
|
||||||
|
@ -130,8 +154,9 @@ func (c *ControllerActivator) isReservedMethod(name string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// register all available, exported methods to handlers if possible.
|
||||||
func (c *ControllerActivator) parseMethods() {
|
func (c *ControllerActivator) parseMethods() {
|
||||||
// register all available, exported methods to handlers if possible.
|
|
||||||
n := c.Type.NumMethod()
|
n := c.Type.NumMethod()
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
m := c.Type.Method(i)
|
m := c.Type.Method(i)
|
||||||
|
@ -184,49 +209,65 @@ func (c *ControllerActivator) activate() {
|
||||||
|
|
||||||
var emptyIn = []reflect.Value{}
|
var emptyIn = []reflect.Value{}
|
||||||
|
|
||||||
func (c *ControllerActivator) Handle(method, path, funcName string, middleware ...context.Handler) error {
|
// Handle registers a route based on a http method, the route's path
|
||||||
|
// and a function name that belongs to the controller, it accepts
|
||||||
|
// a forth, optionally, variadic parameter which is the before handlers.
|
||||||
|
//
|
||||||
|
// Just like `APIBuilder`, it returns the `*router.Route`, if failed
|
||||||
|
// then it logs the errors and it returns nil, you can check the errors
|
||||||
|
// programmatically by the `APIBuilder#GetReporter`.
|
||||||
|
func (c *ControllerActivator) Handle(method, path, funcName string, middleware ...context.Handler) *router.Route {
|
||||||
if method == "" || path == "" || funcName == "" ||
|
if method == "" || path == "" || funcName == "" ||
|
||||||
c.isReservedMethod(funcName) {
|
c.isReservedMethod(funcName) {
|
||||||
// isReservedMethod -> if it's already registered
|
// isReservedMethod -> if it's already registered
|
||||||
// by a previous Handle or analyze methods internally.
|
// by a previous Handle or analyze methods internally.
|
||||||
return errSkip
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get the method from the controller type.
|
||||||
m, ok := c.Type.MethodByName(funcName)
|
m, ok := c.Type.MethodByName(funcName)
|
||||||
if !ok {
|
if !ok {
|
||||||
err := fmt.Errorf("MVC: function '%s' doesn't exist inside the '%s' controller",
|
err := fmt.Errorf("MVC: function '%s' doesn't exist inside the '%s' controller",
|
||||||
funcName, c.FullName)
|
funcName, c.FullName)
|
||||||
c.Router.GetReporter().AddErr(err)
|
c.Router.GetReporter().AddErr(err)
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parse a route template which contains the parameters organised.
|
||||||
tmpl, err := macro.Parse(path, c.Router.Macros())
|
tmpl, err := macro.Parse(path, c.Router.Macros())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("MVC: fail to parse the path for '%s.%s': %v", c.FullName, funcName, err)
|
err = fmt.Errorf("MVC: fail to parse the path for '%s.%s': %v", c.FullName, funcName, err)
|
||||||
c.Router.GetReporter().AddErr(err)
|
c.Router.GetReporter().AddErr(err)
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// add this as a reserved method name in order to
|
// add this as a reserved method name in order to
|
||||||
// be sure that the same func will not be registered again, even if a custom .Handle later on.
|
// be sure that the same func will not be registered again, even if a custom .Handle later on.
|
||||||
c.reservedMethods = append(c.reservedMethods, funcName)
|
c.reservedMethods = append(c.reservedMethods, funcName)
|
||||||
|
|
||||||
// fmt.Printf("===============%s.%s==============\n", c.FullName, funcName)
|
// get the function's input.
|
||||||
|
funcIn := getInputArgsFromFunc(m.Type)
|
||||||
funcIn := getInputArgsFromFunc(m.Type) // except the receiver, which is the controller pointer itself.
|
|
||||||
|
|
||||||
|
// get the path parameters bindings from the template,
|
||||||
|
// use the function's input except the receiver which is the
|
||||||
|
// end-dev's controller pointer.
|
||||||
pathParams := getPathParamsForInput(tmpl.Params, funcIn[1:]...)
|
pathParams := getPathParamsForInput(tmpl.Params, funcIn[1:]...)
|
||||||
|
// get the function's input arguments' bindings.
|
||||||
funcBindings := newTargetFunc(m.Func, pathParams...)
|
funcBindings := newTargetFunc(m.Func, pathParams...)
|
||||||
|
|
||||||
elemTyp := indirectTyp(c.Type) // the element value, not the pointer.
|
// the element value, not the pointer.
|
||||||
|
elemTyp := indirectTyp(c.Type)
|
||||||
|
|
||||||
|
// we will make use of 'n' to make a slice of reflect.Value
|
||||||
|
// to pass into if the function has input arguments that
|
||||||
|
// are will being filled by the funcBindings.
|
||||||
n := len(funcIn)
|
n := len(funcIn)
|
||||||
|
|
||||||
handler := func(ctx context.Context) {
|
handler := func(ctx context.Context) {
|
||||||
|
|
||||||
// create a new controller instance of that type(>ptr).
|
// create a new controller instance of that type(>ptr).
|
||||||
ctrl := reflect.New(elemTyp)
|
ctrl := reflect.New(elemTyp)
|
||||||
b := ctrl.Interface().(BaseController) // the Interface(). is faster than MethodByName or pre-selected methods.
|
// the Interface(). is faster than MethodByName or pre-selected methods.
|
||||||
|
b := ctrl.Interface().(BaseController)
|
||||||
// init the request.
|
// init the request.
|
||||||
b.BeginRequest(ctx)
|
b.BeginRequest(ctx)
|
||||||
|
|
||||||
|
@ -268,16 +309,20 @@ func (c *ControllerActivator) Handle(method, path, funcName string, middleware .
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// end the request, don't check for stopped because this does the actual writing
|
if ctx.IsStopped() {
|
||||||
// if no response written already.
|
return
|
||||||
|
}
|
||||||
|
|
||||||
b.EndRequest(ctx)
|
b.EndRequest(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// register the handler now.
|
// register the handler now.
|
||||||
c.Router.Handle(method, path, append(middleware, handler)...).
|
route := c.Router.Handle(method, path, append(middleware, handler)...)
|
||||||
|
if route != nil {
|
||||||
// change the main handler's name in order to respect the controller's and give
|
// change the main handler's name in order to respect the controller's and give
|
||||||
// a proper debug message.
|
// a proper debug message.
|
||||||
MainHandlerName = fmt.Sprintf("%s.%s", c.FullName, funcName)
|
route.MainHandlerName = fmt.Sprintf("%s.%s", c.FullName, funcName)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return route
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user