mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 13:22:09 +08:00
Changes Made
- Renamed functions to have more descriptive names. - Ensured that function names follow golint naming conventions.y
This commit is contained in:
parent
c2ba8f19ec
commit
aec45eb55b
@ -39,9 +39,9 @@ type BindingBody interface {
|
|||||||
BindBody([]byte, any) error
|
BindBody([]byte, any) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
|
// BindingURI adds BindUri method to Binding. BindUri is similar with Bind,
|
||||||
// but it reads the Params.
|
// but it reads the Params.
|
||||||
type BindingUri interface {
|
type BindingURI interface {
|
||||||
Name() string
|
Name() string
|
||||||
BindUri(map[string][]string, any) error
|
BindUri(map[string][]string, any) error
|
||||||
}
|
}
|
||||||
@ -81,7 +81,7 @@ var (
|
|||||||
ProtoBuf = protobufBinding{}
|
ProtoBuf = protobufBinding{}
|
||||||
MsgPack = msgpackBinding{}
|
MsgPack = msgpackBinding{}
|
||||||
YAML = yamlBinding{}
|
YAML = yamlBinding{}
|
||||||
Uri = uriBinding{}
|
URI = uriBinding{}
|
||||||
Header = headerBinding{}
|
Header = headerBinding{}
|
||||||
TOML = tomlBinding{}
|
TOML = tomlBinding{}
|
||||||
)
|
)
|
||||||
|
@ -792,7 +792,7 @@ func TestHeaderBinding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUriBinding(t *testing.T) {
|
func TestUriBinding(t *testing.T) {
|
||||||
b := Uri
|
b := URI
|
||||||
assert.Equal(t, "uri", b.Name())
|
assert.Equal(t, "uri", b.Name())
|
||||||
|
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
@ -829,7 +829,7 @@ func TestUriInnerBinding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var tag Tag
|
var tag Tag
|
||||||
assert.NoError(t, Uri.BindUri(m, &tag))
|
assert.NoError(t, URI.BindUri(m, &tag))
|
||||||
assert.Equal(t, tag.Name, expectedName)
|
assert.Equal(t, tag.Name, expectedName)
|
||||||
assert.Equal(t, tag.S.Age, expectedAge)
|
assert.Equal(t, tag.S.Age, expectedAge)
|
||||||
}
|
}
|
||||||
|
12
context.go
12
context.go
@ -661,10 +661,10 @@ func (c *Context) BindHeader(obj any) error {
|
|||||||
return c.MustBindWith(obj, binding.Header)
|
return c.MustBindWith(obj, binding.Header)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BindUri binds the passed struct pointer using binding.Uri.
|
// BindURI binds the passed struct pointer using binding.Uri.
|
||||||
// It will abort the request with HTTP 400 if any error occurs.
|
// It will abort the request with HTTP 400 if any error occurs.
|
||||||
func (c *Context) BindUri(obj any) error {
|
func (c *Context) BindURI(obj any) error {
|
||||||
if err := c.ShouldBindUri(obj); err != nil {
|
if err := c.ShouldBindURI(obj); err != nil {
|
||||||
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) //nolint: errcheck
|
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) //nolint: errcheck
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -726,13 +726,13 @@ func (c *Context) ShouldBindHeader(obj any) error {
|
|||||||
return c.ShouldBindWith(obj, binding.Header)
|
return c.ShouldBindWith(obj, binding.Header)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindUri binds the passed struct pointer using the specified binding engine.
|
// ShouldBindURI binds the passed struct pointer using the specified binding engine.
|
||||||
func (c *Context) ShouldBindUri(obj any) error {
|
func (c *Context) ShouldBindURI(obj any) error {
|
||||||
m := make(map[string][]string)
|
m := make(map[string][]string)
|
||||||
for _, v := range c.Params {
|
for _, v := range c.Params {
|
||||||
m[v.Key] = []string{v.Value}
|
m[v.Key] = []string{v.Value}
|
||||||
}
|
}
|
||||||
return binding.Uri.BindUri(m, obj)
|
return binding.URI.BindUri(m, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindWith binds the passed struct pointer using the specified binding engine.
|
// ShouldBindWith binds the passed struct pointer using the specified binding engine.
|
||||||
|
@ -295,7 +295,7 @@ 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")
|
||||||
@ -317,7 +317,7 @@ 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")
|
||||||
@ -338,7 +338,7 @@ func TestBindUriError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
router.Handle(http.MethodGet, "/new/rest/:num", func(c *Context) {
|
router.Handle(http.MethodGet, "/new/rest/:num", func(c *Context) {
|
||||||
var m Member
|
var m Member
|
||||||
assert.Error(t, c.BindUri(&m))
|
assert.Error(t, c.BindURI(&m))
|
||||||
})
|
})
|
||||||
|
|
||||||
path1, _ := exampleFromPath("/new/rest/:num")
|
path1, _ := exampleFromPath("/new/rest/:num")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user