1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-24 05:17:03 +00:00

create a new package, name it as hero, I was thinking super or superb but hero is better name for what it does - the goal is to split the new 'mvc handlers' from the mvc system because they are not the same, users should know that they can use these type of rich binded handlers without controllers as well, like a normal handler and that I implemented here, the old files exist on the mvc package but will be removed at the next commit, I have to decide if we want type aliases for Result or no

Former-commit-id: cb775edc72bedc88aeab4c5a6de6bfc6bd56fae2
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-25 20:05:32 +02:00
parent 4ab889da5f
commit 46505f62db
43 changed files with 2680 additions and 20 deletions

68
hero/param.go Normal file
View File

@@ -0,0 +1,68 @@
package hero
import (
"reflect"
"github.com/kataras/iris/context"
)
// weak because we don't have access to the path, neither
// the macros, so this is just a guess based on the index of the path parameter,
// the function's path parameters should be like a chain, in the same order as
// the caller registers a route's path.
// A context or any value(s) can be in front or back or even between them.
type params struct {
// the next function input index of where the next path parameter
// should be inside the CONTEXT.
next int
}
func (p *params) resolve(index int, typ reflect.Type) (reflect.Value, bool) {
currentParamIndex := p.next
v, ok := resolveParam(currentParamIndex, typ)
p.next = p.next + 1
return v, ok
}
func resolveParam(currentParamIndex int, typ reflect.Type) (reflect.Value, bool) {
var fn interface{}
switch typ.Kind() {
case reflect.Int:
fn = func(ctx context.Context) int {
// the second "ok/found" check is not necessary,
// because even if the entry didn't found on that "index"
// it will return an empty entry which will return the
// default value passed from the xDefault(def) because its `ValueRaw` is nil.
entry, _ := ctx.Params().GetEntryAt(currentParamIndex)
v, _ := entry.IntDefault(0)
return v
}
case reflect.Int64:
fn = func(ctx context.Context) int64 {
entry, _ := ctx.Params().GetEntryAt(currentParamIndex)
v, _ := entry.Int64Default(0)
return v
}
case reflect.Bool:
fn = func(ctx context.Context) bool {
entry, _ := ctx.Params().GetEntryAt(currentParamIndex)
v, _ := entry.BoolDefault(false)
return v
}
case reflect.String:
fn = func(ctx context.Context) string {
entry, _ := ctx.Params().GetEntryAt(currentParamIndex)
// print(entry.Key + " with index of: ")
// print(currentParamIndex)
// println(" and value: " + entry.String())
return entry.String()
}
default:
return reflect.Value{}, false
}
return reflect.ValueOf(fn), true
}