Skip to content

Commit

Permalink
refactor: ctx.bind interface
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Sep 22, 2023
1 parent e4084df commit a6e4159
Showing 1 changed file with 23 additions and 36 deletions.
59 changes: 23 additions & 36 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1317,58 +1317,54 @@ func bodyAllowedForStatus(status int) bool {
return true
}

func (ctx *RequestContext) getBinder() binding.Binder {
if ctx.binder != nil {
return ctx.binder
}
return binding.DefaultBinder()
}

func (ctx *RequestContext) getValidator() binding.StructValidator {
if ctx.validator != nil {
return ctx.validator
}
return binding.DefaultValidator()
}

// BindAndValidate binds data from *RequestContext to obj and validates them if needed.
// NOTE: obj should be a pointer.
func (ctx *RequestContext) BindAndValidate(obj interface{}) error {
if ctx.binder != nil {
return ctx.binder.BindAndValidate(&ctx.Request, obj, ctx.Params)
}
return binding.BindAndValidate(&ctx.Request, obj, ctx.Params)
return ctx.getBinder().BindAndValidate(&ctx.Request, obj, ctx.Params)
}

// Bind binds data from *RequestContext to obj.
// NOTE: obj should be a pointer.
func (ctx *RequestContext) Bind(obj interface{}) error {
if ctx.binder != nil {
return ctx.binder.Bind(&ctx.Request, obj, ctx.Params)
}
return binding.Bind(&ctx.Request, obj, ctx.Params)
return ctx.getBinder().Bind(&ctx.Request, obj, ctx.Params)
}

// Validate validates obj with "vd" tag
// NOTE: obj should be a pointer.
func (ctx *RequestContext) Validate(obj interface{}) error {
if ctx.validator != nil {
return ctx.validator.ValidateStruct(obj)
}
return binding.Validate(obj)
return ctx.getValidator().ValidateStruct(obj)
}

// BindQuery binds query parameters from *RequestContext to obj with 'query' tag. It will only use 'query' tag for binding.
// NOTE: obj should be a pointer.
func (ctx *RequestContext) BindQuery(obj interface{}) error {
if ctx.binder != nil {
return ctx.binder.BindQuery(&ctx.Request, obj)
}
return binding.DefaultBinder().BindQuery(&ctx.Request, obj)
return ctx.getBinder().BindQuery(&ctx.Request, obj)
}

// BindHeader binds header parameters from *RequestContext to obj with 'header' tag. It will only use 'header' tag for binding.
// NOTE: obj should be a pointer.
func (ctx *RequestContext) BindHeader(obj interface{}) error {
if ctx.binder != nil {
return ctx.binder.BindHeader(&ctx.Request, obj)
}
return binding.DefaultBinder().BindHeader(&ctx.Request, obj)
return ctx.getBinder().BindHeader(&ctx.Request, obj)
}

// BindPath binds router parameters from *RequestContext to obj with 'path' tag. It will only use 'path' tag for binding.
// NOTE: obj should be a pointer.
func (ctx *RequestContext) BindPath(obj interface{}) error {
if ctx.binder != nil {
return ctx.binder.BindPath(&ctx.Request, obj, ctx.Params)
}
return binding.DefaultBinder().BindPath(&ctx.Request, obj, ctx.Params)
return ctx.getBinder().BindPath(&ctx.Request, obj, ctx.Params)
}

// BindForm binds form parameters from *RequestContext to obj with 'form' tag. It will only use 'form' tag for binding.
Expand All @@ -1377,28 +1373,19 @@ func (ctx *RequestContext) BindForm(obj interface{}) error {
if len(ctx.Request.Body()) == 0 {
return fmt.Errorf("missing form body")
}
if ctx.binder != nil {
return ctx.binder.BindForm(&ctx.Request, obj)
}
return binding.DefaultBinder().BindForm(&ctx.Request, obj)
return ctx.getBinder().BindForm(&ctx.Request, obj)
}

// BindJSON binds JSON body from *RequestContext.
// NOTE: obj should be a pointer.
func (ctx *RequestContext) BindJSON(obj interface{}) error {
if ctx.binder != nil {
return ctx.binder.BindJSON(&ctx.Request, obj)
}
return binding.DefaultBinder().BindJSON(&ctx.Request, obj)
return ctx.getBinder().BindJSON(&ctx.Request, obj)
}

// BindProtobuf binds protobuf body from *RequestContext.
// NOTE: obj should be a pointer.
func (ctx *RequestContext) BindProtobuf(obj interface{}) error {
if ctx.binder != nil {
return ctx.binder.BindProtobuf(&ctx.Request, obj)
}
return binding.DefaultBinder().BindProtobuf(&ctx.Request, obj)
return ctx.getBinder().BindProtobuf(&ctx.Request, obj)
}

// BindByContentType will select the binding type on the ContentType automatically.
Expand Down

0 comments on commit a6e4159

Please sign in to comment.