Skip to content

Commit 58135f0

Browse files
authored
docs(context): add example comments for ShouldBind* methods (#4428)
- Added detailed example for ShouldBindJSON - Added consistent descriptive comments for ShouldBindXML, ShouldBindQuery, ShouldBindYAML, ShouldBindTOML, ShouldBindPlain, ShouldBindHeader, ShouldBindUri - Makes binding method usage clearer for new users
1 parent a85ef5c commit 58135f0

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎context.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,41 +830,71 @@ func (c *Context) ShouldBind(obj any) error {
830830
}
831831

832832
// ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).
833+
//
834+
// Example:
835+
//
836+
// POST /user
837+
// Content-Type: application/json
838+
//
839+
// Request Body:
840+
// {
841+
// "name": "Manu",
842+
// "age": 20
843+
// }
844+
//
845+
// type User struct {
846+
// Name string `json:"name"`
847+
// Age int `json:"age"`
848+
// }
849+
//
850+
// var user User
851+
// if err := c.ShouldBindJSON(&user); err != nil {
852+
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
853+
// return
854+
// }
855+
// c.JSON(http.StatusOK, user)
833856
func (c *Context) ShouldBindJSON(obj any) error {
834857
return c.ShouldBindWith(obj, binding.JSON)
835858
}
836859

837860
// ShouldBindXML is a shortcut for c.ShouldBindWith(obj, binding.XML).
861+
// It works like ShouldBindJSON but binds the request body as XML data.
838862
func (c *Context) ShouldBindXML(obj any) error {
839863
return c.ShouldBindWith(obj, binding.XML)
840864
}
841865

842866
// ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, binding.Query).
867+
// It works like ShouldBindJSON but binds query parameters from the URL.
843868
func (c *Context) ShouldBindQuery(obj any) error {
844869
return c.ShouldBindWith(obj, binding.Query)
845870
}
846871

847872
// ShouldBindYAML is a shortcut for c.ShouldBindWith(obj, binding.YAML).
873+
// It works like ShouldBindJSON but binds the request body as YAML data.
848874
func (c *Context) ShouldBindYAML(obj any) error {
849875
return c.ShouldBindWith(obj, binding.YAML)
850876
}
851877

852878
// ShouldBindTOML is a shortcut for c.ShouldBindWith(obj, binding.TOML).
879+
// It works like ShouldBindJSON but binds the request body as TOML data.
853880
func (c *Context) ShouldBindTOML(obj any) error {
854881
return c.ShouldBindWith(obj, binding.TOML)
855882
}
856883

857884
// ShouldBindPlain is a shortcut for c.ShouldBindWith(obj, binding.Plain).
885+
// It works like ShouldBindJSON but binds plain text data from the request body.
858886
func (c *Context) ShouldBindPlain(obj any) error {
859887
return c.ShouldBindWith(obj, binding.Plain)
860888
}
861889

862890
// ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, binding.Header).
891+
// It works like ShouldBindJSON but binds values from HTTP headers.
863892
func (c *Context) ShouldBindHeader(obj any) error {
864893
return c.ShouldBindWith(obj, binding.Header)
865894
}
866895

867896
// ShouldBindUri binds the passed struct pointer using the specified binding engine.
897+
// It works like ShouldBindJSON but binds parameters from the URI.
868898
func (c *Context) ShouldBindUri(obj any) error {
869899
m := make(map[string][]string, len(c.Params))
870900
for _, v := range c.Params {

0 commit comments

Comments
 (0)