From 8ec94277f3394438e6354e64e21b188a11aa51a9 Mon Sep 17 00:00:00 2001 From: ItsMeSamey Date: Thu, 21 Nov 2024 17:25:59 +0530 Subject: [PATCH] renamed Should to WithoutAutoHandling and Bind.should to Bind.dontHandle --- bind.go | 23 ++++++++++++----------- binder/README.md | 2 +- ctx.go | 8 ++++---- docs/api/bind.md | 6 +++--- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/bind.go b/bind.go index ef0f349be7..03bca4662b 100644 --- a/bind.go +++ b/bind.go @@ -19,29 +19,30 @@ type StructValidator interface { // Bind struct type Bind struct { - ctx Ctx - should bool + ctx Ctx + dontHandle bool } -// Should To handle binder errors manually, you can prefer Should method. +// If you want to handle binder errors manually, you can use `WithoutAutoHandling`. // It's default behavior of binder. -func (b *Bind) Should() *Bind { - b.should = true +func (b *Bind) WithoutAutoHandling() *Bind { + b.dontHandle = true return b } -// If you want to handle binder errors automatically, you can use WithAutoHandling. -// If there's an error it'll return error and 400 as HTTP status. +// If you want to handle binder errors automatically, you can use `WithAutoHandling`. +// If there's an error, it will return the error and set HTTP status to `400 Bad Request`. +// You must still return on error explicitly func (b *Bind) WithAutoHandling() *Bind { - b.should = false + b.dontHandle = false return b } -// Check Should/WithAutoHandling errors and return it by usage. +// Check WithAutoHandling/WithoutAutoHandling errors and return it by usage. func (b *Bind) returnErr(err error) error { - if err == nil || b.should { + if err == nil || b.dontHandle { return err } @@ -62,7 +63,7 @@ func (b *Bind) validateStruct(out any) error { // Custom To use custom binders, you have to use this method. // You can register them from RegisterCustomBinder method of Fiber instance. // They're checked by name, if it's not found, it will return an error. -// NOTE: Should/WithAutoHandling is still valid for Custom binders. +// NOTE: WithAutoHandling/WithAutoHandling is still valid for Custom binders. func (b *Bind) Custom(name string, dest any) error { binders := b.ctx.App().customBinders for _, customBinder := range binders { diff --git a/binder/README.md b/binder/README.md index 8a42f981e5..f317f8b3a3 100644 --- a/binder/README.md +++ b/binder/README.md @@ -83,7 +83,7 @@ app.Get("/", func(c fiber.Ctx) error { // curl "http://localhost:3000/?name=john&pass=doe&products=shoe,hat" ``` -### Behaviors of Should/WithAutoHandling +### Behaviors of WithAutoHandling/WithAutoHandlingWithStatus Normally, Fiber returns binder error directly. However; if you want to handle it automatically, you can prefer `WithAutoHandling()`. diff --git a/ctx.go b/ctx.go index a2eee2754b..48c70f28f8 100644 --- a/ctx.go +++ b/ctx.go @@ -640,7 +640,7 @@ func (c *DefaultCtx) Get(key string, defaultValue ...string) string { } // GetReqHeader returns the HTTP request header specified by filed. -// This function is generic and can handle differnet headers type values. +// This function is generic and can handle different headers type values. func GetReqHeader[V GenericType](c Ctx, key string, defaultValue ...V) V { var v V return genericParseType[V](c.App().getString(c.Request().Header.Peek(key)), v, defaultValue...) @@ -1083,7 +1083,7 @@ func (c *DefaultCtx) Params(key string, defaultValue ...string) string { } // Params is used to get the route parameters. -// This function is generic and can handle differnet route parameters type values. +// This function is generic and can handle different route parameters type values. // // Example: // @@ -1860,8 +1860,8 @@ func (c *DefaultCtx) IsFromLocal() bool { func (c *DefaultCtx) Bind() *Bind { if c.bind == nil { c.bind = &Bind{ - ctx: c, - should: true, + ctx : c, + dontHandle: true, } } return c.bind diff --git a/docs/api/bind.md b/docs/api/bind.md index 877600e83d..a1cbedd18d 100644 --- a/docs/api/bind.md +++ b/docs/api/bind.md @@ -468,13 +468,13 @@ This function does NOT panic therefor you must still return on error explicitly func (b *Bind) WithAutoHandling() *Bind ``` -### Should +### WithoutAutoHandling -To handle binder errors manually, you can use the `Should` method. +To handle binder errors manually, you can use the `WithoutAutoHandling` method. It's the default behavior of the binder. ```go title="Signature" -func (b *Bind) Should() *Bind +func (b *Bind) WithoutAutoHandling() *Bind ``` ## SetParserDecoder