Skip to content

Commit

Permalink
[v3 Maintenance]: Consolidate and Document Core Changes in v3 (#2934)
Browse files Browse the repository at this point in the history
* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3

* [v3 Maintenance]: Consolidate and Document Core Changes in v3
  • Loading branch information
ReneWerner87 committed Apr 23, 2024
1 parent 4d1e993 commit 077968a
Show file tree
Hide file tree
Showing 62 changed files with 2,305 additions and 1,423 deletions.
16 changes: 6 additions & 10 deletions app.go
Expand Up @@ -276,6 +276,8 @@ type Config struct {
// StreamRequestBody enables request body streaming,
// and calls the handler sooner when given body is
// larger than the current limit.
//
// Default: false
StreamRequestBody bool

// Will not pre parse Multipart Form data if set to true.
Expand All @@ -284,6 +286,8 @@ type Config struct {
// multipart form data as a binary blob, or choose when to parse the data.
//
// Server pre parses multipart form data by default.
//
// Default: false
DisablePreParseMultipartForm bool

// Aggressively reduces memory usage at the cost of higher CPU usage
Expand All @@ -296,14 +300,6 @@ type Config struct {
// Default: false
ReduceMemoryUsage bool `json:"reduce_memory_usage"`

// FEATURE: v2.3.x
// The router executes the same handler by default if StrictRouting or CaseSensitive is disabled.
// Enabling RedirectFixedPath will change this behavior into a client redirect to the original route path.
// Using the status code 301 for GET requests and 308 for all other request methods.
//
// Default: false
// RedirectFixedPath bool

// When set by an external client of Fiber it will use the provided implementation of a
// JSONMarshal
//
Expand Down Expand Up @@ -595,13 +591,13 @@ func (app *App) RegisterCustomConstraint(constraint CustomConstraint) {
app.customConstraints = append(app.customConstraints, constraint)
}

// You can register custom binders to use as Bind().Custom("name").
// RegisterCustomBinder Allows to register custom binders to use as Bind().Custom("name").
// They should be compatible with CustomBinder interface.
func (app *App) RegisterCustomBinder(binder CustomBinder) {
app.customBinders = append(app.customBinders, binder)
}

// You can use SetTLSHandler to use ClientHelloInfo when using TLS with Listener.
// SetTLSHandler Can be used to set ClientHelloInfo when using TLS with Listener.
func (app *App) SetTLSHandler(tlsHandler *TLSHandler) {
// Attach the tlsHandler to the config
app.mutex.Lock()
Expand Down
43 changes: 21 additions & 22 deletions bind.go
Expand Up @@ -5,34 +5,33 @@ import (
"github.com/gofiber/utils/v2"
)

// An interface to register custom binders.
// CustomBinder An interface to register custom binders.
type CustomBinder interface {
Name() string
MIMETypes() []string
Parse(c Ctx, out any) error
}

// An interface to register custom struct validator for binding.
// StructValidator is an interface to register custom struct validator for binding.
type StructValidator interface {
Engine() any
ValidateStruct(out any) error
Validate(out any) error
}

// Bind struct
type Bind struct {
ctx *DefaultCtx
ctx Ctx
should bool
}

// To handle binder errors manually, you can prefer Should method.
// Should To handle binder errors manually, you can prefer Should method.
// It's default behavior of binder.
func (b *Bind) Should() *Bind {
b.should = true

return b
}

// If you want to handle binder errors automatically, you can use Must.
// Must If you want to handle binder errors automatically, you can use Must.
// If there's an error it'll return error and 400 as HTTP status.
func (b *Bind) Must() *Bind {
b.should = false
Expand All @@ -52,15 +51,15 @@ func (b *Bind) returnErr(err error) error {

// Struct validation.
func (b *Bind) validateStruct(out any) error {
validator := b.ctx.app.config.StructValidator
validator := b.ctx.App().config.StructValidator
if validator != nil {
return validator.ValidateStruct(out)
return validator.Validate(out)
}

return nil
}

// To use custom binders, you have to use this method.
// 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/Must is still valid for Custom binders.
Expand Down Expand Up @@ -103,7 +102,7 @@ func (b *Bind) Cookie(out any) error {
return b.validateStruct(out)
}

// QueryParser binds the query string into the struct, map[string]string and map[string][]string.
// Query binds the query string into the struct, map[string]string and map[string][]string.
func (b *Bind) Query(out any) error {
if err := b.returnErr(binder.QueryBinder.Bind(b.ctx.Context(), out)); err != nil {
return err
Expand Down Expand Up @@ -141,7 +140,7 @@ func (b *Bind) Form(out any) error {

// URI binds the route parameters into the struct, map[string]string and map[string][]string.
func (b *Bind) URI(out any) error {
if err := b.returnErr(binder.URIBinder.Bind(b.ctx.route.Params, b.ctx.Params, out)); err != nil {
if err := b.returnErr(binder.URIBinder.Bind(b.ctx.Route().Params, b.ctx.Params, out)); err != nil {
return err
}

Expand All @@ -167,6 +166,16 @@ func (b *Bind) Body(out any) error {
ctype := utils.ToLower(utils.UnsafeString(b.ctx.Context().Request.Header.ContentType()))
ctype = binder.FilterFlags(utils.ParseVendorSpecificContentType(ctype))

// Check custom binders
binders := b.ctx.App().customBinders
for _, customBinder := range binders {
for _, mime := range customBinder.MIMETypes() {
if mime == ctype {
return b.returnErr(customBinder.Parse(b.ctx, out))
}
}
}

// Parse body accordingly
switch ctype {
case MIMEApplicationJSON:
Expand All @@ -179,16 +188,6 @@ func (b *Bind) Body(out any) error {
return b.MultipartForm(out)
}

// Check custom binders
binders := b.ctx.App().customBinders
for _, customBinder := range binders {
for _, mime := range customBinder.MIMETypes() {
if mime == ctype {
return b.returnErr(customBinder.Parse(b.ctx, out))
}
}
}

// No suitable content type found
return ErrUnprocessableEntity
}
6 changes: 1 addition & 5 deletions bind_test.go
Expand Up @@ -1547,11 +1547,7 @@ func Test_Bind_Must(t *testing.T) {
// simple struct validator for testing
type structValidator struct{}

func (*structValidator) Engine() any {
return ""
}

func (*structValidator) ValidateStruct(out any) error {
func (*structValidator) Validate(out any) error {
out = reflect.ValueOf(out).Elem().Interface()
sq, ok := out.(simpleQuery)
if !ok {
Expand Down

0 comments on commit 077968a

Please sign in to comment.