1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-19 19:07:06 +00:00

ok the new mvc system works great, all tests done and almost x3 smaller LOC used plus new three awesome features:) - next commit will be commenting out and replace the mvc package with the new mvc2

Former-commit-id: 552095d29256a1116849cc6054c82001e790e705
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-10 07:00:51 +02:00
parent e02da3d442
commit ed79f0c3cd
31 changed files with 1344 additions and 1572 deletions

View File

@@ -3,10 +3,10 @@ package mvc2
import (
"fmt"
"reflect"
"runtime"
"github.com/kataras/golog"
"github.com/kataras/iris/context"
"github.com/kataras/iris/mvc/activator/methodfunc"
)
// checks if "handler" is context.Handler; func(context.Context).
@@ -28,18 +28,13 @@ func validateHandler(handler interface{}) error {
return nil
}
var (
contextTyp = reflect.TypeOf(context.NewContext(nil))
emptyIn = []reflect.Value{}
)
// MustMakeHandler calls the `MakeHandler` and returns its first resultthe low-level handler), see its docs.
// It panics on error.
func MustMakeHandler(handler interface{}, binders ...interface{}) context.Handler {
h, err := MakeHandler(handler, binders...)
// MustMakeHandler calls the `MakeHandler` and panics on any error.
func MustMakeHandler(handler interface{}, bindValues ...reflect.Value) context.Handler {
h, err := MakeHandler(handler, bindValues...)
if err != nil {
panic(err)
}
return h
}
@@ -48,9 +43,8 @@ func MustMakeHandler(handler interface{}, binders ...interface{}) context.Handle
// custom structs, Result(View | Response) and anything that you already know that mvc implementation supports,
// and returns a low-level `context/iris.Handler` which can be used anywhere in the Iris Application,
// as middleware or as simple route handler or party handler or subdomain handler-router.
func MakeHandler(handler interface{}, binders ...interface{}) (context.Handler, error) {
func MakeHandler(handler interface{}, bindValues ...reflect.Value) (context.Handler, error) {
if err := validateHandler(handler); err != nil {
golog.Errorf("mvc handler: %v", err)
return nil, err
}
@@ -59,71 +53,39 @@ func MakeHandler(handler interface{}, binders ...interface{}) (context.Handler,
return h, nil
}
inputBinders := make([]reflect.Value, len(binders), len(binders))
for i := range binders {
inputBinders[i] = reflect.ValueOf(binders[i])
}
return makeHandler(reflect.ValueOf(handler), inputBinders), nil
// typ := indirectTyp(reflect.TypeOf(handler))
// n := typ.NumIn()
// typIn := make([]reflect.Type, n, n)
// for i := 0; i < n; i++ {
// typIn[i] = typ.In(i)
// }
// m := getBindersForInput(binders, typIn...)
// if len(m) != n {
// err := fmt.Errorf("input arguments length(%d) of types(%s) and valid binders length(%d) are not equal", n, typIn, len(m))
// golog.Errorf("mvc handler: %v", err)
// return nil, err
// }
// return makeHandler(reflect.ValueOf(handler), m), nil
}
func makeHandler(fn reflect.Value, inputBinders []reflect.Value) context.Handler {
inLen := fn.Type().NumIn()
if inLen == 0 {
return func(ctx context.Context) {
methodfunc.DispatchFuncResult(ctx, fn.Call(emptyIn))
}
}
s := getServicesFor(fn, inputBinders)
if len(s) == 0 {
golog.Errorf("mvc handler: input arguments length(%d) and valid binders length(%d) are not equal", inLen, len(s))
return nil
}
fn := reflect.ValueOf(handler)
n := fn.Type().NumIn()
// contextIndex := -1
// if n > 0 {
// if isContext(fn.Type().In(0)) {
// contextIndex = 0
// }
// }
return func(ctx context.Context) {
ctxValue := []reflect.Value{reflect.ValueOf(ctx)}
in := make([]reflect.Value, n, n)
// if contextIndex >= 0 {
// in[contextIndex] = ctxValue[0]
// }
// ctxValues := []reflect.Value{reflect.ValueOf(ctx)}
// for k, v := range m {
// in[k] = v.BindFunc(ctxValues)
// if ctx.IsStopped() {
// return
// }
// }
// methodfunc.DispatchFuncResult(ctx, fn.Call(in))
if n == 0 {
h := func(ctx context.Context) {
DispatchFuncResult(ctx, fn.Call(emptyIn))
}
s.FillFuncInput(ctxValue, &in)
methodfunc.DispatchFuncResult(ctx, fn.Call(in))
return h, nil
}
s := newTargetFunc(fn, bindValues...)
if !s.Valid {
pc := fn.Pointer()
fpc := runtime.FuncForPC(pc)
callerFileName, callerLineNumber := fpc.FileLine(pc)
callerName := fpc.Name()
err := fmt.Errorf("input arguments length(%d) and valid binders length(%d) are not equal for typeof '%s' which is defined at %s:%d by %s",
n, len(s.Inputs), fn.Type().String(), callerFileName, callerLineNumber, callerName)
return nil, err
}
h := func(ctx context.Context) {
in := make([]reflect.Value, n, n)
s.Fill(&in, reflect.ValueOf(ctx))
if ctx.IsStopped() {
return
}
DispatchFuncResult(ctx, fn.Call(in))
}
return h, nil
}