fix 2 minor issues

This commit is contained in:
Gerasimos (Makis) Maropoulos 2022-02-26 21:16:08 +02:00
parent 09f494e406
commit 20fa672097
No known key found for this signature in database
GPG Key ID: 66FCC29BD385FCA6
7 changed files with 49 additions and 19 deletions

View File

@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and
## Fixes and Improvements ## Fixes and Improvements
- Fix a bug of `WithoutBodyConsumptionOnUnmarshal` configurator and a minor dependency injection issue caused by the previous alpha version between 20 and 26 February of 2022.
- New basic [cors middleware](middleware/cors). - New basic [cors middleware](middleware/cors).
- New `httptest.NewServer` helper. - New `httptest.NewServer` helper.
- New [x/errors](x/errors) sub-package, helps with HTTP Wire Errors. Example can be found [here](_examples/routing/http-wire-errors/main.go). - New [x/errors](x/errors) sub-package, helps with HTTP Wire Errors. Example can be found [here](_examples/routing/http-wire-errors/main.go).

View File

@ -47,19 +47,20 @@ func newApp() *iris.Application {
// or 202 status code and empty body // or 202 status code and empty body
// or a 409 status code and "my_error" body. // or a 409 status code and "my_error" body.
app.ConfigureContainer(func(api *iris.APIContainer) { app.ConfigureContainer(func(api *iris.APIContainer) {
// TODO: fix that test as well. // Enable execution of middlewares without ctx.Next requirement.
// api.Self.SetExecutionRules(iris.ExecutionRules{ api.Self.SetExecutionRules(iris.ExecutionRules{
// Begin: iris.ExecutionOptions{ Begin: iris.ExecutionOptions{
// Force: true, Force: true,
// }, },
// }) })
api.Use(middleware) api.Use(middleware)
api.Post("/{id:int}", handler) api.Post("/{id:int}", handler)
}) })
app.Configure( app.Configure(
iris.WithOptimizations, /* optional */ iris.WithOptimizations, /* optional */
iris.WithoutBodyConsumptionOnUnmarshal /* required when more than one handler is consuming request payload(testInput) */) iris.WithoutBodyConsumptionOnUnmarshal, /* required when more than one handler is consuming request payload(testInput) */
)
return app return app
} }

View File

@ -58,7 +58,7 @@ func postIndexStream(ctx iris.Context) {
var users []User var users []User
job := func(decode iris.DecodeFunc) error { job := func(decode iris.DecodeFunc) error {
var u User var u User
if err := decode(&u); err != nil { if err := decode(ctx, &u); err != nil {
return err return err
} }
users = append(users, u) users = append(users, u)

View File

@ -40,10 +40,7 @@ func newApp(db *DB) *iris.Application {
// //
// Look ./templates/index.html#L16 // Look ./templates/index.html#L16
tmpl.AddFunc("IsPositive", func(n int) bool { tmpl.AddFunc("IsPositive", func(n int) bool {
if n > 0 { return n > 0
return true
}
return false
}) })
app.RegisterView(tmpl) app.RegisterView(tmpl)
@ -72,7 +69,7 @@ func newApp(db *DB) *iris.Application {
return return
} }
ctx.Redirect(value, iris.StatusTemporaryRedirect) ctx.Redirect(value, iris.StatusBadGateway)
} }
app.Get("/u/{shortkey}", func(ctx iris.Context) { app.Get("/u/{shortkey}", func(ctx iris.Context) {
execShortURL(ctx, ctx.Params().Get("shortkey")) execShortURL(ctx, ctx.Params().Get("shortkey"))

View File

@ -37,7 +37,7 @@ func TestURLShortener(t *testing.T) {
// get // get
e.GET("/u/" + keys[0]).Expect(). e.GET("/u/" + keys[0]).Expect().
Status(httptest.StatusTemporaryRedirect).Header("Location").Equal(originalURL) Status(httptest.StatusBadGateway).Header("Location").Equal(originalURL)
// save the same again, it should add a new key // save the same again, it should add a new key
e.POST("/shorten"). e.POST("/shorten").

View File

@ -589,6 +589,11 @@ func (ctx *Context) HandlerIndex(n int) (currentIndex int) {
//} //}
// Alternative way is `!ctx.IsStopped()` if middleware make use of the `ctx.StopExecution()` on failure. // Alternative way is `!ctx.IsStopped()` if middleware make use of the `ctx.StopExecution()` on failure.
func (ctx *Context) Proceed(h Handler) bool { func (ctx *Context) Proceed(h Handler) bool {
_, ok := ctx.ProceedAndReportIfStopped(h)
return ok
}
func (ctx *Context) ProceedAndReportIfStopped(h Handler) (bool, bool) {
ctx.proceeded = internalPauseExecutionIndex ctx.proceeded = internalPauseExecutionIndex
// Store the current index. // Store the current index.
@ -604,15 +609,15 @@ func (ctx *Context) Proceed(h Handler) bool {
// Stop called, return false but keep the handlers index. // Stop called, return false but keep the handlers index.
if afterIdx == stopExecutionIndex { if afterIdx == stopExecutionIndex {
return false return true, false
} }
if proceededByNext { if proceededByNext {
return true return false, true
} }
// Next called or not. // Next called or not.
return afterIdx > beforeIdx return false, afterIdx > beforeIdx
} }
// HandlerName returns the current handler's name, helpful for debugging. // HandlerName returns the current handler's name, helpful for debugging.
@ -2382,6 +2387,20 @@ func wrapDecodeFunc(decodeFunc func(interface{}) error) DecodeFunc {
} }
} }
func (options JSONReader) unmarshal(ctx stdContext.Context, body []byte, outPtr interface{}) error {
if options.Optimize {
if outPtr != nil {
if _, supportsContext := outPtr.(unmarshalerContext); !supportsContext {
return gojson.Unmarshal(body, outPtr)
}
}
return gojson.UnmarshalContext(ctx, body, outPtr)
}
return json.Unmarshal(body, outPtr)
}
func (options JSONReader) getDecoder(r io.Reader, outPtr interface{}) (internalJSONDecoder, DecodeFunc) { func (options JSONReader) getDecoder(r io.Reader, outPtr interface{}) (internalJSONDecoder, DecodeFunc) {
var ( var (
decoder internalJSONDecoder decoder internalJSONDecoder
@ -2429,6 +2448,15 @@ func (ctx *Context) ReadJSON(outPtr interface{}, opts ...JSONReader) error {
options = opts[0] options = opts[0]
} }
if ctx.IsRecordingBody() {
body, err := GetBody(ctx.request, true)
if err != nil {
return err
}
return options.unmarshal(ctx.request.Context(), body, outPtr)
}
_, decodeFunc := options.getDecoder(ctx.request.Body, outPtr) _, decodeFunc := options.getDecoder(ctx.request.Body, outPtr)
return decodeFunc(ctx.request.Context(), outPtr) return decodeFunc(ctx.request.Context(), outPtr)

View File

@ -75,7 +75,9 @@ func (e ExecutionOptions) buildHandler(h context.Handler) context.Handler {
return func(ctx *context.Context) { return func(ctx *context.Context) {
// Proceed will fire the handler and return false here if it doesn't contain a `ctx.Next()`, // Proceed will fire the handler and return false here if it doesn't contain a `ctx.Next()`,
// so we add the `ctx.Next()` wherever is necessary in order to eliminate any dev's misuse. // so we add the `ctx.Next()` wherever is necessary in order to eliminate any dev's misuse.
if !ctx.Proceed(h) { //
// 26 Feb 2022: check if manually stopped, and if it's then don't call ctx.Next.
if hasStopped, hasNext := ctx.ProceedAndReportIfStopped(h); !hasStopped && !hasNext {
// `ctx.Next()` always checks for `ctx.IsStopped()` and handler(s) positions by-design. // `ctx.Next()` always checks for `ctx.IsStopped()` and handler(s) positions by-design.
ctx.Next() ctx.Next()
} }