mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
Update to v8.3.1 | MVC: RelPath and RelTmpl implemented. Read HISTORY.md
Read HISTORY.md https://github.com/kataras/iris/blob/master/HISTORY.md#sa-19-august-2017--v831 Former-commit-id: 23f7c1c0dc3bc64f27db591a9b22cd5934337891
This commit is contained in:
@@ -16,7 +16,7 @@ type (
|
||||
// think it as a "supervisor" of your Controller which
|
||||
// cares about you.
|
||||
TController struct {
|
||||
// the type of the user/dev's "c" controller (interface{})
|
||||
// the type of the user/dev's "c" controller (interface{}).
|
||||
Type reflect.Type
|
||||
// it's the first passed value of the controller instance,
|
||||
// we need this to collect and save the persistence fields' values.
|
||||
@@ -123,6 +123,7 @@ var (
|
||||
// End-User doesn't need to have any knowledge of this if she/he doesn't want to implement
|
||||
// a new Controller type.
|
||||
type BaseController interface {
|
||||
SetName(name string)
|
||||
BeginRequest(ctx context.Context)
|
||||
EndRequest(ctx context.Context)
|
||||
}
|
||||
@@ -201,6 +202,7 @@ func ActivateController(base BaseController, bindValues []interface{},
|
||||
// builds the handler for a type based on the method index (i.e Get() -> [0], Post() -> [1]).
|
||||
func buildMethodHandler(t TController, methodFuncIndex int) context.Handler {
|
||||
elem := t.Type.Elem()
|
||||
ctrlName := t.Value.Type().Name()
|
||||
/*
|
||||
// good idea, it speeds up the whole thing by ~1MB per 20MB at my personal
|
||||
// laptop but this way the Model for example which is not a persistence
|
||||
@@ -255,6 +257,7 @@ func buildMethodHandler(t TController, methodFuncIndex int) context.Handler {
|
||||
// but if somone tries to "crack" that, then just stop the world in order to be notified,
|
||||
// we don't want to go away from that type of mistake.
|
||||
b := c.Interface().(BaseController)
|
||||
b.SetName(ctrlName)
|
||||
|
||||
// init the request.
|
||||
b.BeginRequest(ctx)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package mvc
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/memstore"
|
||||
"github.com/kataras/iris/mvc/activator"
|
||||
@@ -58,8 +61,29 @@ import (
|
||||
//
|
||||
// Look `core/router/APIBuilder#Controller` method too.
|
||||
type Controller struct {
|
||||
// path and path params.
|
||||
Path string
|
||||
// Name contains the current controller's full name.
|
||||
Name string
|
||||
// contains the `Name` as different words, all lowercase,
|
||||
// without the "Controller" suffix if exists.
|
||||
// we need this as field because the activator
|
||||
// we will not try to parse these if not needed
|
||||
// it's up to the end-developer to call `RelPath()` or `RelTmpl()`
|
||||
// which will result to fill them.
|
||||
nameAsWords []string
|
||||
|
||||
// relPath the "as assume" relative request path.
|
||||
//
|
||||
// If UserController and request path is "/user/messages" then it's "/messages"
|
||||
// if UserPostController and request path is "/user/post" then it's "/"
|
||||
// if UserProfile and request path is "/user/profile/likes" then it's "/likes"
|
||||
relPath string
|
||||
|
||||
// request path and its parameters, read-write.
|
||||
// Path is the current request path.
|
||||
Path string
|
||||
// Params are the request path's parameters, i.e
|
||||
// for route like "/user/{id}" and request to "/user/42"
|
||||
// it contains the "id" = 42.
|
||||
Params *context.RequestParams
|
||||
|
||||
// some info read and write,
|
||||
@@ -67,6 +91,12 @@ type Controller struct {
|
||||
Status int
|
||||
Values *memstore.Store
|
||||
|
||||
// relTmpl the "as assume" relative path to the view root folder.
|
||||
//
|
||||
// If UserController then it's "user/"
|
||||
// if UserPostController then it's "user/post/"
|
||||
// if UserProfile then it's "user/profile/".
|
||||
relTmpl string
|
||||
// view read and write,
|
||||
// can be already set-ed by previous handlers as well.
|
||||
Layout string
|
||||
@@ -77,9 +107,74 @@ type Controller struct {
|
||||
Ctx context.Context
|
||||
}
|
||||
|
||||
// SetName sets the controller's full name.
|
||||
// It's called internally.
|
||||
func (c *Controller) SetName(name string) {
|
||||
c.Name = name
|
||||
}
|
||||
|
||||
func (c *Controller) getNameWords() []string {
|
||||
if len(c.nameAsWords) == 0 {
|
||||
c.nameAsWords = findCtrlWords(c.Name)
|
||||
}
|
||||
return c.nameAsWords
|
||||
}
|
||||
|
||||
const slashStr = "/"
|
||||
|
||||
// RelPath tries to return the controller's name
|
||||
// without the "Controller" prefix, all lowercase
|
||||
// prefixed with slash and splited by slash appended
|
||||
// with the rest of the request path.
|
||||
// For example:
|
||||
// If UserController and request path is "/user/messages" then it's "/messages"
|
||||
// if UserPostController and request path is "/user/post" then it's "/"
|
||||
// if UserProfile and request path is "/user/profile/likes" then it's "/likes"
|
||||
//
|
||||
// It's useful for things like path checking and redirect.
|
||||
func (c *Controller) RelPath() string {
|
||||
if c.relPath == "" {
|
||||
w := c.getNameWords()
|
||||
rel := strings.Join(w, slashStr)
|
||||
|
||||
reqPath := c.Ctx.Path()
|
||||
if len(reqPath) == 0 {
|
||||
// it never come here
|
||||
// but to protect ourselves jsut return an empty slash.
|
||||
return slashStr
|
||||
}
|
||||
// [1:]to ellimuate the prefixes like "//"
|
||||
// request path has always "/"
|
||||
rel = strings.Replace(c.Ctx.Path()[1:], rel, "", 1)
|
||||
if rel == "" {
|
||||
rel = slashStr
|
||||
}
|
||||
c.relPath = rel
|
||||
}
|
||||
|
||||
return c.relPath
|
||||
}
|
||||
|
||||
// RelTmpl tries to return the controller's name
|
||||
// without the "Controller" prefix, all lowercase
|
||||
// splited by slash and suffixed by slash.
|
||||
// For example:
|
||||
// If UserController then it's "user/"
|
||||
// if UserPostController then it's "user/post/"
|
||||
// if UserProfile then it's "user/profile/".
|
||||
//
|
||||
// It's useful to locate templates if the controller and views path have aligned names.
|
||||
func (c *Controller) RelTmpl() string {
|
||||
if c.relTmpl == "" {
|
||||
c.relTmpl = strings.Join(c.getNameWords(), slashStr) + slashStr
|
||||
}
|
||||
return c.relTmpl
|
||||
}
|
||||
|
||||
// BeginRequest starts the main controller
|
||||
// it initialize the Ctx and other fields.
|
||||
//
|
||||
// It's called internally.
|
||||
// End-Developer can ovverride it but it still MUST be called.
|
||||
func (c *Controller) BeginRequest(ctx context.Context) {
|
||||
// path and path params
|
||||
@@ -101,6 +196,7 @@ func (c *Controller) BeginRequest(ctx context.Context) {
|
||||
// It checks for the fields and calls the necessary context's
|
||||
// methods to modify the response to the client.
|
||||
//
|
||||
// It's called internally.
|
||||
// End-Developer can ovveride it but still should be called at the end.
|
||||
func (c *Controller) EndRequest(ctx context.Context) {
|
||||
if path := c.Path; path != "" && path != ctx.Path() {
|
||||
@@ -126,4 +222,6 @@ func (c *Controller) EndRequest(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
var ctrlSuffix = reflect.TypeOf(Controller{}).Name()
|
||||
|
||||
var _ activator.BaseController = &Controller{}
|
||||
|
||||
@@ -282,3 +282,60 @@ func TestControllerBind(t *testing.T) {
|
||||
e.GET("/deep").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal(expected)
|
||||
}
|
||||
|
||||
type (
|
||||
UserController struct{ mvc.Controller }
|
||||
Profile struct{ mvc.Controller }
|
||||
UserProfilePostController struct{ mvc.Controller }
|
||||
)
|
||||
|
||||
func writeRelatives(c mvc.Controller) {
|
||||
c.Ctx.JSON(iris.Map{
|
||||
"RelPath": c.RelPath(),
|
||||
"TmplPath": c.RelTmpl(),
|
||||
})
|
||||
}
|
||||
func (c *UserController) Get() {
|
||||
writeRelatives(c.Controller)
|
||||
}
|
||||
|
||||
func (c *Profile) Get() {
|
||||
writeRelatives(c.Controller)
|
||||
}
|
||||
|
||||
func (c *UserProfilePostController) Get() {
|
||||
writeRelatives(c.Controller)
|
||||
}
|
||||
|
||||
func TestControllerRelPathAndRelTmpl(t *testing.T) {
|
||||
app := iris.New()
|
||||
var tests = map[string]iris.Map{
|
||||
// UserController
|
||||
"/user": {"RelPath": "/", "TmplPath": "user/"},
|
||||
"/user/42": {"RelPath": "/42", "TmplPath": "user/"},
|
||||
"/user/me": {"RelPath": "/me", "TmplPath": "user/"},
|
||||
// Profile (without Controller suffix, should work as expected)
|
||||
"/profile": {"RelPath": "/", "TmplPath": "profile/"},
|
||||
"/profile/42": {"RelPath": "/42", "TmplPath": "profile/"},
|
||||
"/profile/me": {"RelPath": "/me", "TmplPath": "profile/"},
|
||||
// UserProfilePost
|
||||
"/user/profile/post": {"RelPath": "/", "TmplPath": "user/profile/post/"},
|
||||
"/user/profile/post/42": {"RelPath": "/42", "TmplPath": "user/profile/post/"},
|
||||
"/user/profile/post/mine": {"RelPath": "/mine", "TmplPath": "user/profile/post/"},
|
||||
}
|
||||
|
||||
app.Controller("/user /user/me /user/{id}",
|
||||
new(UserController))
|
||||
|
||||
app.Controller("/profile /profile/me /profile/{id}",
|
||||
new(Profile))
|
||||
|
||||
app.Controller("/user/profile/post /user/profile/post/mine /user/profile/post/{id}",
|
||||
new(UserProfilePostController))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
for path, tt := range tests {
|
||||
e.GET(path).Expect().Status(httptest.StatusOK).JSON().Equal(tt)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
38
mvc/strutil.go
Normal file
38
mvc/strutil.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package mvc
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func findCtrlWords(ctrlName string) (w []string) {
|
||||
end := len(ctrlName)
|
||||
start := -1
|
||||
for i, n := 0, end; i < n; i++ {
|
||||
c := rune(ctrlName[i])
|
||||
if unicode.IsUpper(c) {
|
||||
// it doesn't count the last uppercase
|
||||
if start != -1 {
|
||||
end = i
|
||||
w = append(w, strings.ToLower(ctrlName[start:end]))
|
||||
}
|
||||
start = i
|
||||
continue
|
||||
}
|
||||
end = i + 1
|
||||
|
||||
}
|
||||
|
||||
// We can't omit the last name, we have to take it.
|
||||
// because of controller names like
|
||||
// "UserProfile", we need to return "user", "profile"
|
||||
// if "UserController", we need to return "user"
|
||||
// if "User", we need to return "user".
|
||||
last := ctrlName[start:end]
|
||||
if last == ctrlSuffix {
|
||||
return
|
||||
}
|
||||
|
||||
w = append(w, strings.ToLower(last))
|
||||
return
|
||||
}
|
||||
31
mvc/strutil_test.go
Normal file
31
mvc/strutil_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package mvc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindCtrlWords(t *testing.T) {
|
||||
var tests = map[string][]string{
|
||||
"UserController": {"user"},
|
||||
"UserPostController": {"user", "post"},
|
||||
"ProfileController": {"profile"},
|
||||
"UserProfileController": {"user", "profile"},
|
||||
"UserProfilePostController": {"user", "profile", "post"},
|
||||
"UserProfile": {"user", "profile"},
|
||||
"Profile": {"profile"},
|
||||
"User": {"user"},
|
||||
}
|
||||
|
||||
for ctrlName, expected := range tests {
|
||||
words := findCtrlWords(ctrlName)
|
||||
if len(expected) != len(words) {
|
||||
t.Fatalf("expected words and return don't have the same length: [%d] != [%d] | '%s' != '%s'",
|
||||
len(expected), len(words), expected, words)
|
||||
}
|
||||
for i, w := range words {
|
||||
if expected[i] != w {
|
||||
t.Fatalf("expected word is not equal with the return one: '%s' != '%s'", expected[i], w)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user