mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 09:57:01 +00:00
Update to version 8.5.1. It contains a minor fix. Read HISTORY.md
https://github.com/kataras/iris/blob/master/HISTORY.md#tu-10-october-2017--v851 Former-commit-id: af1424c691613ccde911bef7d1371aa52e6abb79
This commit is contained in:
@@ -131,7 +131,7 @@ func (t TController) HandlerOf(methodFunc methodfunc.MethodFunc) context.Handler
|
||||
t.persistenceController.Handle(c)
|
||||
}
|
||||
|
||||
// if previous (binded) handlers stoped the execution
|
||||
// if previous (binded) handlers stopped the execution
|
||||
// we should know that.
|
||||
if ctx.IsStopped() {
|
||||
return
|
||||
|
||||
@@ -347,7 +347,19 @@ func (c *Controller) EndRequest(ctx context.Context) {
|
||||
ctx.ViewLayout(layout)
|
||||
}
|
||||
if len(c.Data) > 0 {
|
||||
ctx.Values().Set(ctx.Application().ConfigurationReadOnly().GetViewDataContextKey(), c.Data)
|
||||
dataKey := ctx.Application().ConfigurationReadOnly().GetViewDataContextKey()
|
||||
// In order to respect any c.Ctx.ViewData that may called manually before;
|
||||
if ctx.Values().Get(dataKey) == nil {
|
||||
// if no c.Ctx.ViewData then it's empty do a
|
||||
// pure set, it's faster.
|
||||
ctx.Values().Set(dataKey, c.Data)
|
||||
} else {
|
||||
// else do a range loop and set the data one by one.
|
||||
for k, v := range c.Data {
|
||||
ctx.ViewData(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ctx.View(view)
|
||||
|
||||
@@ -222,3 +222,50 @@ func TestControllerMethodResultTypes(t *testing.T) {
|
||||
// it will fire the error's text
|
||||
Body().Equal("omit return of testCustomStruct and fire error")
|
||||
}
|
||||
|
||||
type testControllerViewResultRespectCtxViewData struct {
|
||||
T *testing.T
|
||||
mvc.C
|
||||
}
|
||||
|
||||
func (t *testControllerViewResultRespectCtxViewData) BeginRequest(ctx context.Context) {
|
||||
t.C.BeginRequest(ctx)
|
||||
ctx.ViewData("name_begin", "iris_begin")
|
||||
}
|
||||
|
||||
func (t *testControllerViewResultRespectCtxViewData) EndRequest(ctx context.Context) {
|
||||
t.C.EndRequest(ctx)
|
||||
// check if data is not overriden by return mvc.View {Data: context.Map...}
|
||||
|
||||
dataWritten := ctx.GetViewData()
|
||||
if dataWritten == nil {
|
||||
t.T.Fatalf("view data is nil, both BeginRequest and Get failed to write the data")
|
||||
return
|
||||
}
|
||||
|
||||
if dataWritten["name_begin"] == nil {
|
||||
t.T.Fatalf(`view data[name_begin] is nil,
|
||||
BeginRequest's ctx.ViewData call have been overriden by Get's return mvc.View {Data: }.
|
||||
Total view data: %v`, dataWritten)
|
||||
}
|
||||
|
||||
if dataWritten["name"] == nil {
|
||||
t.T.Fatalf("view data[name] is nil, Get's return mvc.View {Data: } didn't work. Total view data: %v", dataWritten)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *testControllerViewResultRespectCtxViewData) Get() mvc.Result {
|
||||
return mvc.View{
|
||||
Name: "doesnt_exists.html",
|
||||
Data: context.Map{"name": "iris"}, // we care about this only.
|
||||
Code: iris.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerViewResultRespectCtxViewData(t *testing.T) {
|
||||
app := iris.New()
|
||||
app.Controller("/", new(testControllerViewResultRespectCtxViewData), t)
|
||||
e := httptest.New(t, app)
|
||||
|
||||
e.GET("/").Expect().Status(iris.StatusInternalServerError)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/mvc/activator/methodfunc"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
)
|
||||
|
||||
// View completes the `methodfunc.Result` interface.
|
||||
@@ -66,12 +68,32 @@ func (r View) Dispatch(ctx context.Context) { // r as Response view.
|
||||
}
|
||||
|
||||
if r.Data != nil {
|
||||
ctx.Values().Set(
|
||||
ctx.Application().ConfigurationReadOnly().GetViewDataContextKey(),
|
||||
r.Data,
|
||||
)
|
||||
// In order to respect any c.Ctx.ViewData that may called manually before;
|
||||
dataKey := ctx.Application().ConfigurationReadOnly().GetViewDataContextKey()
|
||||
if ctx.Values().Get(dataKey) == nil {
|
||||
// if no c.Ctx.ViewData then it's empty do a
|
||||
// pure set, it's faster.
|
||||
ctx.Values().Set(dataKey, r.Data)
|
||||
} else {
|
||||
// else check if r.Data is map or struct, if struct convert it to map,
|
||||
// do a range loop and set the data one by one.
|
||||
// context.Map is actually a map[string]interface{} but we have to make that check;
|
||||
if m, ok := r.Data.(map[string]interface{}); ok {
|
||||
setViewData(ctx, m)
|
||||
} else if m, ok := r.Data.(context.Map); ok {
|
||||
setViewData(ctx, m)
|
||||
} else if structs.IsStruct(r.Data) {
|
||||
setViewData(ctx, structs.Map(r))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.View(r.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func setViewData(ctx context.Context, data map[string]interface{}) {
|
||||
for k, v := range data {
|
||||
ctx.ViewData(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user