1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +00:00

Update to 8.4.0 | New macro type, new high-optimized MVC features. Read HISTORY.md

Former-commit-id: b72a23ba063be60a9750c8b1b0df024b0c8ed549
This commit is contained in:
kataras
2017-08-27 18:46:04 +03:00
parent 8602517371
commit 591806795e
37 changed files with 1242 additions and 453 deletions

View File

@@ -197,6 +197,10 @@ func (api *APIBuilder) HandleMany(method string, relativePath string, handlers .
paths := strings.Split(trimmedPath, " ")
for _, p := range paths {
if p != "" {
if method == "ANY" || method == "ALL" {
routes = append(routes, api.Any(p, handlers...)...)
continue
}
routes = append(routes, api.Handle(method, p, handlers...))
}
}
@@ -511,12 +515,11 @@ func (api *APIBuilder) Any(relativePath string, handlers ...context.Handler) (ro
// Read more at `/mvc#Controller`.
func (api *APIBuilder) Controller(relativePath string, controller activator.BaseController,
bindValues ...interface{}) (routes []*Route) {
registerFunc := func(method string, handlers ...context.Handler) {
if method == "ANY" || method == "ALL" {
routes = api.Any(relativePath, handlers...)
} else {
routes = append(routes, api.HandleMany(method, relativePath, handlers...)...)
}
registerFunc := func(ifRelPath string, method string, handlers ...context.Handler) {
relPath := relativePath + ifRelPath
r := api.HandleMany(method, relPath, handlers...)
routes = append(routes, r...)
}
// bind any values to the controller's relative fields
@@ -527,9 +530,7 @@ func (api *APIBuilder) Controller(relativePath string, controller activator.Base
// and tag them with `iris:"persistence"`.
//
// don't worry it will never be handled if empty values.
err := activator.Register(controller, bindValues, nil, registerFunc)
if err != nil {
if err := activator.Register(controller, bindValues, registerFunc); err != nil {
api.reporter.Add("%v for path: '%s'", err, relativePath)
}

View File

@@ -36,6 +36,7 @@ func registerBuiltinsMacroFuncs(out *macro.Map) {
// these can be overridden by the user, later on.
registerStringMacroFuncs(out.String)
registerIntMacroFuncs(out.Int)
registerIntMacroFuncs(out.Long)
registerAlphabeticalMacroFuncs(out.Alphabetical)
registerFileMacroFuncs(out.File)
registerPathMacroFuncs(out.Path)

View File

@@ -21,6 +21,10 @@ const (
// Allows only numbers (0-9)
// Declaration: /mypath/{myparam:int}
ParamTypeInt
// ParamTypeLong is the integer, a number type.
// Allows only numbers (0-9)
// Declaration: /mypath/{myparam:long}
ParamTypeLong
// ParamTypeAlphabetical is the alphabetical/letter type type.
// Allows letters only (upper or lowercase)
// Declaration: /mypath/{myparam:alphabetical}
@@ -44,6 +48,7 @@ const (
var paramTypes = map[string]ParamType{
"string": ParamTypeString,
"int": ParamTypeInt,
"long": ParamTypeLong,
"alphabetical": ParamTypeAlphabetical,
"file": ParamTypeFile,
"path": ParamTypePath,

View File

@@ -60,6 +60,7 @@ func TestParseParam(t *testing.T) {
},
ErrorCode: 404,
}}, // 0
{true,
ast.ParamStatement{
Src: "{id:int range(1,5)}",
@@ -123,6 +124,13 @@ func TestParseParam(t *testing.T) {
},
ErrorCode: 404,
}}, // 7
{true,
ast.ParamStatement{
Src: "{id:long else 404}",
Name: "id",
Type: ast.ParamTypeLong,
ErrorCode: 404,
}}, // 8
}

View File

@@ -208,7 +208,7 @@ func (m *Macro) getFunc(funcName string) ParamEvaluatorBuilder {
// Map contains the default macros mapped to their types.
// This is the manager which is used by the caller to register custom
// parameter functions per param-type (String, Int, Alphabetical, File, Path).
// parameter functions per param-type (String, Int, Long, Alphabetical, File, Path).
type Map struct {
// string type
// anything
@@ -216,6 +216,9 @@ type Map struct {
// int type
// only numbers (0-9)
Int *Macro
// long an int64 type
// only numbers (0-9)
Long *Macro
// alphabetical/letter type
// letters only (upper or lowercase)
Alphabetical *Macro
@@ -241,6 +244,7 @@ func NewMap() *Map {
// it allows everything, so no need for a regexp here.
String: newMacro(func(string) bool { return true }),
Int: newMacro(MustNewEvaluatorFromRegexp("^[0-9]+$")),
Long: newMacro(MustNewEvaluatorFromRegexp("^[0-9]+$")),
Alphabetical: newMacro(MustNewEvaluatorFromRegexp("^[a-zA-Z ]+$")),
File: newMacro(MustNewEvaluatorFromRegexp("^[a-zA-Z0-9_.-]*$")),
// it allows everything, we have String and Path as different
@@ -259,6 +263,8 @@ func (m *Map) Lookup(typ ast.ParamType) *Macro {
switch typ {
case ast.ParamTypeInt:
return m.Int
case ast.ParamTypeLong:
return m.Long
case ast.ParamTypeAlphabetical:
return m.Alphabetical
case ast.ParamTypeFile: