1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-21 11:57:02 +00:00

add the ability to add custom parameter types to the interpreter and mapped macros with any number of macro functions - example added - although it's working it is not ready yet - I have to do some cleanup, doc comments and a TODO

Former-commit-id: 8ac751b649a3b8e59948fd4c89ad53d25f49d0d5
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-09-26 11:37:11 +03:00
parent 52a07df0f4
commit dc3c38b189
26 changed files with 1070 additions and 1036 deletions

View File

@@ -5,7 +5,6 @@ import (
"github.com/kataras/iris/context"
"github.com/kataras/iris/core/router/macro"
"github.com/kataras/iris/core/router/macro/interpreter/ast"
)
func getPathParamsForInput(params []macro.TemplateParam, funcIn ...reflect.Type) (values []reflect.Value) {
@@ -13,61 +12,40 @@ func getPathParamsForInput(params []macro.TemplateParam, funcIn ...reflect.Type)
return
}
consumedParams := make(map[int]bool, 0)
for _, in := range funcIn {
for j, p := range params {
if _, consumed := consumedParams[j]; consumed {
continue
}
paramType := p.Type
paramName := p.Name
// fmt.Printf("%s input arg type vs %s param type\n", in.Kind().String(), p.Type.Kind().String())
if paramType.Assignable(in.Kind()) {
consumedParams[j] = true
// fmt.Printf("param.go: bind path param func for paramName = '%s' and paramType = '%s'\n", paramName, paramType.String())
values = append(values, makeFuncParamGetter(paramType, paramName))
}
// consumedParams := make(map[int]bool, 0)
// for _, in := range funcIn {
// for j, p := range params {
// if _, consumed := consumedParams[j]; consumed {
// continue
// }
// // fmt.Printf("%s input arg type vs %s param type\n", in.Kind().String(), p.Type.Kind().String())
// if m := macros.Lookup(p.Type); m != nil && m.GoType == in.Kind() {
// consumedParams[j] = true
// // fmt.Printf("param.go: bind path param func for paramName = '%s' and paramType = '%s'\n", paramName, paramType.String())
// funcDep, ok := context.ParamResolverByKindAndIndex(m.GoType, p.Index)
// // funcDep, ok := context.ParamResolverByKindAndKey(in.Kind(), paramName)
// if !ok {
// // here we can add a logger about invalid parameter type although it should never happen here
// // unless the end-developer modified the macro/macros with a special type but not the context/ParamResolvers.
// continue
// }
// values = append(values, funcDep)
// }
// }
// }
for i, param := range params {
if len(funcIn) <= i {
return
}
funcDep, ok := context.ParamResolverByKindAndIndex(funcIn[i].Kind(), param.Index)
if !ok {
continue
}
values = append(values, funcDep)
}
return
}
func makeFuncParamGetter(paramType ast.ParamType, paramName string) reflect.Value {
var fn interface{}
switch paramType {
case ast.ParamTypeNumber:
fn = func(ctx context.Context) int {
v, _ := ctx.Params().GetInt(paramName)
return v
}
case ast.ParamTypeInt64:
fn = func(ctx context.Context) int64 {
v, _ := ctx.Params().GetInt64(paramName)
return v
}
case ast.ParamTypeUint8:
fn = func(ctx context.Context) uint8 {
v, _ := ctx.Params().GetUint8(paramName)
return v
}
case ast.ParamTypeUint64:
fn = func(ctx context.Context) uint64 {
v, _ := ctx.Params().GetUint64(paramName)
return v
}
case ast.ParamTypeBoolean:
fn = func(ctx context.Context) bool {
v, _ := ctx.Params().GetBool(paramName)
return v
}
default:
// string, path...
fn = func(ctx context.Context) string {
return ctx.Params().Get(paramName)
}
}
return reflect.ValueOf(fn)
}