2017-11-27 20:39:57 +01:00
|
|
|
package mvc2
|
|
|
|
|
|
|
|
import (
|
2017-12-04 04:06:03 +01:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2017-11-27 20:39:57 +01:00
|
|
|
|
2017-12-04 04:06:03 +01:00
|
|
|
"github.com/kataras/iris/context"
|
|
|
|
"github.com/kataras/iris/core/router"
|
|
|
|
"github.com/kataras/iris/core/router/macro"
|
2017-11-27 20:39:57 +01:00
|
|
|
)
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// 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.
|
2017-12-04 04:06:03 +01:00
|
|
|
type BaseController interface {
|
|
|
|
BeginRequest(context.Context)
|
|
|
|
EndRequest(context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
// C is the basic BaseController type that can be used as an embedded anonymous field
|
|
|
|
// to custom end-dev controllers.
|
|
|
|
//
|
|
|
|
// func(c *ExampleController) Get() string |
|
|
|
|
// (string, string) |
|
|
|
|
// (string, int) |
|
|
|
|
// int |
|
|
|
|
// (int, string |
|
|
|
|
// (string, error) |
|
|
|
|
// bool |
|
|
|
|
// (any, bool) |
|
|
|
|
// error |
|
|
|
|
// (int, error) |
|
|
|
|
// (customStruct, error) |
|
|
|
|
// customStruct |
|
|
|
|
// (customStruct, int) |
|
|
|
|
// (customStruct, string) |
|
|
|
|
// Result or (Result, error)
|
|
|
|
// where Get is an HTTP Method func.
|
|
|
|
//
|
|
|
|
// Look `core/router#APIBuilder#Controller` method too.
|
2017-11-27 20:39:57 +01:00
|
|
|
//
|
2017-12-04 04:06:03 +01:00
|
|
|
// It completes the `activator.BaseController` interface.
|
|
|
|
//
|
|
|
|
// Example at: https://github.com/kataras/iris/tree/master/_examples/mvc/overview/web/controllers.
|
|
|
|
// Example usage at: https://github.com/kataras/iris/blob/master/mvc/method_result_test.go#L17.
|
|
|
|
type C struct {
|
|
|
|
// The current context.Context.
|
|
|
|
//
|
|
|
|
// we have to name it for two reasons:
|
|
|
|
// 1: can't ignore these via reflection, it doesn't give an option to
|
|
|
|
// see if the functions is derived from another type.
|
|
|
|
// 2: end-developer may want to use some method functions
|
|
|
|
// or any fields that could be conflict with the context's.
|
|
|
|
Ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ BaseController = &C{}
|
|
|
|
|
2017-12-13 05:17:28 +01:00
|
|
|
// BeginRequest does nothing anymore, is here to complet ethe `BaseController` interface.
|
|
|
|
// BaseController is not required anymore, `Ctx` is binded automatically by the engine's
|
|
|
|
// wrapped Handler.
|
|
|
|
func (c *C) BeginRequest(ctx context.Context) {}
|
2017-12-04 04:06:03 +01:00
|
|
|
|
|
|
|
// EndRequest does nothing, is here to complete the `BaseController` interface.
|
|
|
|
func (c *C) EndRequest(ctx context.Context) {}
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// ControllerActivator returns a new controller type info description.
|
|
|
|
// Its functionality can be overriden by the end-dev.
|
2017-12-04 04:06:03 +01:00
|
|
|
type ControllerActivator struct {
|
|
|
|
// 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
|
|
|
|
// in order to not create a new type like `ActivationPayload` for the `OnActivate`.
|
|
|
|
Router router.Party
|
|
|
|
|
2017-12-11 05:51:52 +01:00
|
|
|
// initRef BaseController // the BaseController as it's passed from the end-dev.
|
|
|
|
Value reflect.Value // the BaseController's Value.
|
|
|
|
Type reflect.Type // raw type of the BaseController (initRef).
|
2017-12-04 04:06:03 +01:00
|
|
|
// FullName it's the last package path segment + "." + the Name.
|
|
|
|
// i.e: if login-example/user/controller.go, the FullName is "user.Controller".
|
|
|
|
FullName string
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
// the methods names that is already binded to a handler,
|
|
|
|
// the BeginRequest, EndRequest and OnActivate are reserved by the internal implementation.
|
|
|
|
reservedMethods []string
|
|
|
|
|
|
|
|
// input are always empty after the `activate`
|
|
|
|
// are used to build the bindings, and we need this field
|
|
|
|
// because we have 3 states (Engine.Input, OnActivate, Bind)
|
|
|
|
// that we can add or override binding values.
|
2017-12-13 05:17:28 +01:00
|
|
|
ValueStore // TODO: or ... this is dirty code I will have to re format it a bit tomorrow.
|
2017-12-04 04:06:03 +01:00
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
// the bindings that comes from input (and Engine) and can be binded to the controller's(initRef) fields.
|
|
|
|
bindings *targetStruct
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 05:17:28 +01:00
|
|
|
func newControllerActivator(router router.Party, controller interface{}, bindValues ...reflect.Value) *ControllerActivator {
|
2017-12-12 13:33:39 +01:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2017-12-11 05:51:52 +01:00
|
|
|
// the following will make sure that if
|
|
|
|
// the controller's has set-ed pointer struct fields by the end-dev
|
|
|
|
// we will include them to the bindings.
|
|
|
|
// set bindings to the non-zero pointer fields' values that may be set-ed by
|
|
|
|
// the end-developer when declaring the controller,
|
|
|
|
// activate listeners needs them in order to know if something set-ed already or not,
|
|
|
|
// look `BindTypeExists`.
|
2017-12-12 13:33:39 +01:00
|
|
|
bindValues = append(lookupNonZeroFieldsValues(val), bindValues...)
|
2017-12-11 05:51:52 +01:00
|
|
|
|
2017-12-04 04:06:03 +01:00
|
|
|
c := &ControllerActivator{
|
2017-12-12 13:33:39 +01:00
|
|
|
// give access to the Router to the end-devs if they need it for some reason,
|
|
|
|
// i.e register done handlers.
|
2017-12-11 05:51:52 +01:00
|
|
|
Router: router,
|
|
|
|
Value: val,
|
|
|
|
Type: typ,
|
|
|
|
FullName: fullName,
|
2017-12-12 13:33:39 +01:00
|
|
|
// 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.
|
2017-12-13 05:17:28 +01:00
|
|
|
//
|
|
|
|
// TODO: now that BaseController is totally optionally
|
|
|
|
// we have to check if BeginRequest and EndRequest should be here.
|
2017-12-10 06:00:51 +01:00
|
|
|
reservedMethods: []string{
|
|
|
|
"BeginRequest",
|
|
|
|
"EndRequest",
|
|
|
|
"OnActivate",
|
|
|
|
},
|
2017-12-12 13:33:39 +01:00
|
|
|
// 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.
|
2017-12-13 05:17:28 +01:00
|
|
|
ValueStore: bindValues,
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// checks if a method is already registered.
|
2017-12-10 06:00:51 +01:00
|
|
|
func (c *ControllerActivator) isReservedMethod(name string) bool {
|
|
|
|
for _, s := range c.reservedMethods {
|
2017-12-04 04:06:03 +01:00
|
|
|
if s == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// register all available, exported methods to handlers if possible.
|
2017-12-11 05:51:52 +01:00
|
|
|
func (c *ControllerActivator) parseMethods() {
|
|
|
|
n := c.Type.NumMethod()
|
2017-12-10 06:00:51 +01:00
|
|
|
for i := 0; i < n; i++ {
|
2017-12-11 05:51:52 +01:00
|
|
|
m := c.Type.Method(i)
|
2017-12-10 06:00:51 +01:00
|
|
|
|
2017-12-11 05:51:52 +01:00
|
|
|
httpMethod, httpPath, err := parseMethod(m, c.isReservedMethod)
|
|
|
|
if err != nil {
|
|
|
|
if err != errSkip {
|
|
|
|
err = fmt.Errorf("MVC: fail to parse the route path and HTTP method for '%s.%s': %v", c.FullName, m.Name, err)
|
|
|
|
c.Router.GetReporter().AddErr(err)
|
2017-12-10 06:00:51 +01:00
|
|
|
|
2017-12-11 05:51:52 +01:00
|
|
|
}
|
2017-12-10 06:00:51 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-12-11 05:51:52 +01:00
|
|
|
c.Handle(httpMethod, httpPath, m.Name)
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
2017-12-10 06:00:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetBindings will override any bindings with the new "values".
|
|
|
|
func (c *ControllerActivator) SetBindings(values ...reflect.Value) {
|
|
|
|
// set field index with matching binders, if any.
|
2017-12-13 05:17:28 +01:00
|
|
|
c.ValueStore = values
|
2017-12-11 05:51:52 +01:00
|
|
|
c.bindings = newTargetStruct(c.Value, values...)
|
2017-12-10 06:00:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ControllerActivator) activate() {
|
2017-12-13 05:17:28 +01:00
|
|
|
c.SetBindings(c.ValueStore...)
|
|
|
|
c.parseMethods()
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
var emptyIn = []reflect.Value{}
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// 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 {
|
2017-12-10 06:00:51 +01:00
|
|
|
if method == "" || path == "" || funcName == "" ||
|
|
|
|
c.isReservedMethod(funcName) {
|
2017-12-04 04:06:03 +01:00
|
|
|
// isReservedMethod -> if it's already registered
|
|
|
|
// by a previous Handle or analyze methods internally.
|
2017-12-12 13:33:39 +01:00
|
|
|
return nil
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// get the method from the controller type.
|
2017-12-10 06:00:51 +01:00
|
|
|
m, ok := c.Type.MethodByName(funcName)
|
2017-12-04 04:06:03 +01:00
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("MVC: function '%s' doesn't exist inside the '%s' controller",
|
|
|
|
funcName, c.FullName)
|
|
|
|
c.Router.GetReporter().AddErr(err)
|
2017-12-12 13:33:39 +01:00
|
|
|
return nil
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// parse a route template which contains the parameters organised.
|
2017-12-04 04:06:03 +01:00
|
|
|
tmpl, err := macro.Parse(path, c.Router.Macros())
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("MVC: fail to parse the path for '%s.%s': %v", c.FullName, funcName, err)
|
|
|
|
c.Router.GetReporter().AddErr(err)
|
2017-12-12 13:33:39 +01:00
|
|
|
return nil
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
// 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.
|
|
|
|
c.reservedMethods = append(c.reservedMethods, funcName)
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// get the function's input.
|
|
|
|
funcIn := getInputArgsFromFunc(m.Type)
|
2017-12-04 04:06:03 +01:00
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// get the path parameters bindings from the template,
|
|
|
|
// use the function's input except the receiver which is the
|
|
|
|
// end-dev's controller pointer.
|
2017-12-10 06:00:51 +01:00
|
|
|
pathParams := getPathParamsForInput(tmpl.Params, funcIn[1:]...)
|
2017-12-12 13:33:39 +01:00
|
|
|
// get the function's input arguments' bindings.
|
2017-12-10 06:00:51 +01:00
|
|
|
funcBindings := newTargetFunc(m.Func, pathParams...)
|
2017-12-04 04:06:03 +01:00
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// 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.
|
2017-12-10 06:00:51 +01:00
|
|
|
n := len(funcIn)
|
2017-12-13 05:17:28 +01:00
|
|
|
// the element value, not the pointer, wil lbe used to create a
|
|
|
|
// new controller on each incoming request.
|
|
|
|
elemTyp := indirectTyp(c.Type)
|
|
|
|
|
|
|
|
implementsBase := isBaseController(c.Type)
|
2017-12-04 04:06:03 +01:00
|
|
|
|
2017-12-13 05:17:28 +01:00
|
|
|
handler := func(ctx context.Context) {
|
2017-12-04 04:06:03 +01:00
|
|
|
// create a new controller instance of that type(>ptr).
|
2017-12-10 06:00:51 +01:00
|
|
|
ctrl := reflect.New(elemTyp)
|
2017-12-13 05:17:28 +01:00
|
|
|
|
|
|
|
// // the Interface(). is faster than MethodByName or pre-selected methods.
|
|
|
|
// b := ctrl.Interface().(BaseController)
|
|
|
|
// // init the request.
|
|
|
|
// b.BeginRequest(ctx)
|
|
|
|
|
|
|
|
// // if begin request stopped the execution.
|
|
|
|
// if ctx.IsStopped() {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
if implementsBase {
|
|
|
|
// the Interface(). is faster than MethodByName or pre-selected methods.
|
|
|
|
b := ctrl.Interface().(BaseController)
|
|
|
|
// init the request.
|
|
|
|
b.BeginRequest(ctx)
|
|
|
|
|
|
|
|
// if begin request stopped the execution.
|
|
|
|
if ctx.IsStopped() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndRequest will be called at any case except the `BeginRequest` is
|
|
|
|
// stopped.
|
|
|
|
defer b.EndRequest(ctx)
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
if !c.bindings.Valid && !funcBindings.Valid {
|
|
|
|
DispatchFuncResult(ctx, ctrl.Method(m.Index).Call(emptyIn))
|
2017-12-04 04:06:03 +01:00
|
|
|
} else {
|
2017-12-10 06:00:51 +01:00
|
|
|
ctxValue := reflect.ValueOf(ctx)
|
|
|
|
if c.bindings.Valid {
|
|
|
|
elem := ctrl.Elem()
|
|
|
|
c.bindings.Fill(elem, ctxValue)
|
|
|
|
if ctx.IsStopped() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// we do this in order to reduce in := make...
|
|
|
|
// if not func input binders, we execute the handler with empty input args.
|
|
|
|
if !funcBindings.Valid {
|
|
|
|
DispatchFuncResult(ctx, ctrl.Method(m.Index).Call(emptyIn))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// otherwise, it has one or more valid input binders,
|
|
|
|
// make the input and call the func using those.
|
|
|
|
if funcBindings.Valid {
|
|
|
|
in := make([]reflect.Value, n, n)
|
|
|
|
in[0] = ctrl
|
|
|
|
funcBindings.Fill(&in, ctxValue)
|
|
|
|
if ctx.IsStopped() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchFuncResult(ctx, m.Func.Call(in))
|
|
|
|
}
|
|
|
|
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 05:17:28 +01:00
|
|
|
// if ctx.IsStopped() {
|
|
|
|
// return
|
|
|
|
// }
|
2017-12-12 13:33:39 +01:00
|
|
|
|
2017-12-13 05:17:28 +01:00
|
|
|
// b.EndRequest(ctx)
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// register the handler now.
|
2017-12-12 13:33:39 +01:00
|
|
|
route := c.Router.Handle(method, path, append(middleware, handler)...)
|
|
|
|
if route != nil {
|
2017-12-10 06:00:51 +01:00
|
|
|
// change the main handler's name in order to respect the controller's and give
|
|
|
|
// a proper debug message.
|
2017-12-12 13:33:39 +01:00
|
|
|
route.MainHandlerName = fmt.Sprintf("%s.%s", c.FullName, funcName)
|
|
|
|
}
|
2017-12-04 04:06:03 +01:00
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
return route
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|