fix: removed YODA conditions, unnecessary binding.binding

This commit is contained in:
Kacper Bąk 2022-04-22 22:36:42 +02:00
parent 444e156fb1
commit d6c1821fc8
5 changed files with 10 additions and 10 deletions

View File

@ -32,9 +32,9 @@ type Binding interface {
Bind(*http.Request, any) error Bind(*http.Request, any) error
} }
// BindingBody adds BindBody method to Binding. BindBody is similar with Bind, // Body adds BindBody method to Binding. BindBody is similar with Bind,
// but it reads the body from supplied bytes instead of req.Body. // but it reads the body from supplied bytes instead of req.Body.
type BindingBody interface { type Body interface {
Binding Binding
BindBody([]byte, any) error BindBody([]byte, any) error
} }

View File

@ -1339,10 +1339,10 @@ func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body
err := b.Bind(req, &obj) err := b.Bind(req, &obj)
assert.Error(t, err) assert.Error(t, err)
invalid_obj := FooStruct{} invalidobj := FooStruct{}
req.Body = ioutil.NopCloser(strings.NewReader(`{"msg":"hello"}`)) req.Body = ioutil.NopCloser(strings.NewReader(`{"msg":"hello"}`))
req.Header.Add("Content-Type", MIMEPROTOBUF) req.Header.Add("Content-Type", MIMEPROTOBUF)
err = b.Bind(req, &invalid_obj) err = b.Bind(req, &invalidobj)
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, err.Error(), "obj is not ProtoMessage") assert.Equal(t, err.Error(), "obj is not ProtoMessage")

View File

@ -716,7 +716,7 @@ func (c *Context) ShouldBindWith(obj any, b binding.Binding) error {
// //
// NOTE: This method reads the body before binding. So you should use // NOTE: This method reads the body before binding. So you should use
// ShouldBindWith for better performance if you need to call only once. // ShouldBindWith for better performance if you need to call only once.
func (c *Context) ShouldBindBodyWith(obj any, bb binding.BindingBody) (err error) { func (c *Context) ShouldBindBodyWith(obj any, bb binding.Body) (err error) {
var body []byte var body []byte
if cb, ok := c.Get(BodyBytesKey); ok { if cb, ok := c.Get(BodyBytesKey); ok {
if cbb, ok := cb.([]byte); ok { if cbb, ok := cb.([]byte); ok {

View File

@ -1801,7 +1801,7 @@ func TestContextShouldBindBodyWith(t *testing.T) {
} }
for _, tt := range []struct { for _, tt := range []struct {
name string name string
bindingA, bindingB binding.BindingBody bindingA, bindingB binding.Body
bodyA, bodyB string bodyA, bodyB string
}{ }{
{ {

View File

@ -296,8 +296,8 @@ func TestShouldBindUri(t *testing.T) {
router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) { router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) {
var person Person var person Person
assert.NoError(t, c.ShouldBindUri(&person)) assert.NoError(t, c.ShouldBindUri(&person))
assert.True(t, "" != person.Name) assert.True(t, person.Name != "")
assert.True(t, "" != person.ID) assert.True(t, person.ID != "")
c.String(http.StatusOK, "ShouldBindUri test OK") c.String(http.StatusOK, "ShouldBindUri test OK")
}) })
@ -318,8 +318,8 @@ func TestBindUri(t *testing.T) {
router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) { router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) {
var person Person var person Person
assert.NoError(t, c.BindUri(&person)) assert.NoError(t, c.BindUri(&person))
assert.True(t, "" != person.Name) assert.True(t, person.Name != "")
assert.True(t, "" != person.ID) assert.True(t, person.ID != "")
c.String(http.StatusOK, "BindUri test OK") c.String(http.StatusOK, "BindUri test OK")
}) })