2017-12-16 05:38:28 +01:00
|
|
|
package mvc
|
2017-12-04 04:08:05 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"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) {
|
|
|
|
if len(funcIn) == 0 || len(params) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-16 05:38:28 +01:00
|
|
|
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
|
2017-12-16 16:57:20 +01:00
|
|
|
// fmt.Printf("param.go: bind path param func for paramName = '%s' and paramType = '%s'\n", paramName, paramType.String())
|
2017-12-16 05:38:28 +01:00
|
|
|
values = append(values, makeFuncParamGetter(paramType, paramName))
|
|
|
|
}
|
2017-12-10 06:00:51 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-04 04:08:05 +01:00
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
return
|
|
|
|
}
|
2017-12-04 04:08:05 +01:00
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
func makeFuncParamGetter(paramType ast.ParamType, paramName string) reflect.Value {
|
|
|
|
var fn interface{}
|
2017-12-04 04:08:05 +01:00
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
switch paramType {
|
|
|
|
case ast.ParamTypeInt:
|
|
|
|
fn = func(ctx context.Context) int {
|
|
|
|
v, _ := ctx.Params().GetInt(paramName)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
case ast.ParamTypeLong:
|
|
|
|
fn = func(ctx context.Context) int64 {
|
|
|
|
v, _ := ctx.Params().GetInt64(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)
|
2017-12-04 04:08:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
return reflect.ValueOf(fn)
|
2017-12-04 04:08:05 +01:00
|
|
|
}
|