mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
.DI() to ConfigureContainer(...builders)
Former-commit-id: 169671a8b5b706dc8f136e68c1a060f27a2c421b
This commit is contained in:
@@ -118,8 +118,8 @@ func getBindingsFor(inputs []reflect.Type, deps []*Dependency, paramsCount int)
|
||||
// That way the above will work as expected:
|
||||
// 1. mvc.New(app.Party("/path/{firstparam}")).Handle(....Controller.GetBy(secondparam string))
|
||||
// 2. mvc.New(app.Party("/path/{firstparam}/{secondparam}")).Handle(...Controller.GetBy(firstparam, secondparam string))
|
||||
// 3. usersRouter := app.Party("/users/{id:uint64}"); usersRouter.DI().Handle(method, "/", handler(id uint64))
|
||||
// 4. usersRouter.Party("/friends").DI().Handle(method, "/{friendID:uint64}", handler(friendID uint64))
|
||||
// 3. usersRouter := app.Party("/users/{id:uint64}"); usersRouter.ConfigureContainer().Handle(method, "/", handler(id uint64))
|
||||
// 4. usersRouter.Party("/friends").ConfigureContainer().Handle(method, "/{friendID:uint64}", handler(friendID uint64))
|
||||
//
|
||||
// Therefore, count the inputs that can be path parameters first.
|
||||
shouldBindParams := make(map[int]struct{})
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/kataras/iris/v12/context"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
@@ -323,21 +324,29 @@ func dispatchCommon(ctx context.Context,
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
switch context.TrimHeaderValue(contentType) {
|
||||
case context.ContentXMLHeaderValue:
|
||||
_, err := ctx.XML(v)
|
||||
return err
|
||||
case context.ContentYAMLHeaderValue:
|
||||
_, err := ctx.YAML(v)
|
||||
return err
|
||||
case context.ContentProtobufHeaderValue:
|
||||
msg, ok := v.(proto.Message)
|
||||
if !ok {
|
||||
return context.ErrContentNotSupported
|
||||
}
|
||||
|
||||
if strings.HasPrefix(contentType, context.ContentJavascriptHeaderValue) {
|
||||
_, err = ctx.JSONP(v)
|
||||
} else if strings.HasPrefix(contentType, context.ContentXMLHeaderValue) {
|
||||
_, err = ctx.XML(v)
|
||||
// no need: context.XML{Indent: " "}), after v12.2,
|
||||
// if not iris.WithOptimizations passed and indent is empty then it sets it to two spaces for JSON, JSONP and XML,
|
||||
// otherwise given indentation.
|
||||
} else {
|
||||
// defaults to json if content type is missing or its application/json.
|
||||
_, err = ctx.JSON(v)
|
||||
_, err := ctx.Protobuf(msg)
|
||||
return err
|
||||
case context.ContentMsgPackHeaderValue, context.ContentMsgPack2HeaderValue:
|
||||
_, err := ctx.MsgPack(v)
|
||||
return err
|
||||
default:
|
||||
// otherwise default to JSON.
|
||||
_, err := ctx.JSON(v)
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
ctx.ContentType(contentType)
|
||||
|
||||
@@ -220,16 +220,16 @@ func TestHandlerPathParams(t *testing.T) {
|
||||
return fmt.Sprintf("%d", id)
|
||||
}
|
||||
|
||||
app.PartyFunc("/users", func(r iris.Party) {
|
||||
r.DI().Get("/{id:uint64}", handler)
|
||||
app.Party("/users").ConfigureContainer(func(api *iris.APIContainer) {
|
||||
api.Get("/{id:uint64}", handler)
|
||||
})
|
||||
|
||||
app.PartyFunc("/editors/{id:uint64}", func(r iris.Party) {
|
||||
r.DI().Get("/", handler)
|
||||
app.Party("/editors/{id:uint64}").ConfigureContainer(func(api *iris.APIContainer) {
|
||||
api.Get("/", handler)
|
||||
})
|
||||
|
||||
// should receive the last one, as we expected only one useful for MVC (there is a similar test there too).
|
||||
app.DI().Get("/{ownerID:uint64}/book/{booKID:uint64}", handler)
|
||||
app.ConfigureContainer().Get("/{ownerID:uint64}/book/{booKID:uint64}", handler)
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user