Skip to content
Myr

feat(bind): ShouldBindBodyWith shortcut and change doc (#3871)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat: ShouldBindBodyWith shortcut and change doc * fix: yaml can parse json test case * style: fix new test case in context_test.go * chore: modify the code style to specify binding type * chroe: gofmt modifies the code format

authored by RedCrazyGhost · committed by GitHub · parent fd1faad · Browse files · 3 files+284 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+284 added−2 removed

  • other:+284 added−2 removed

3 files+284 added−2 removed

modified:docs/doc.md+7 added-2 removed
1954 unmodified lines
1955 }1956}1957```19581959For this, you can use `c.ShouldBindBodyWith`.1959For this, you can use `c.ShouldBindBodyWith` or shortcuts.19601961- `c.ShouldBindBodyWithJSON` is a shortcut for c.ShouldBindBodyWith(obj, binding.JSON).1962- `c.ShouldBindBodyWithXML` is a shortcut for c.ShouldBindBodyWith(obj, binding.XML).1963- `c.ShouldBindBodyWithYAML` is a shortcut for c.ShouldBindBodyWith(obj, binding.YAML).1964- `c.ShouldBindBodyWithTOML` is a shortcut for c.ShouldBindBodyWith(obj, binding.TOML).19651966```go1967func SomeHandler(c *gin.Context) {1968 objA := formA{}1969 objB := formB{}
3 unmodified lines
1973 // At this time, it reuses body stored in the context.1974 } else if errB := c.ShouldBindBodyWith(&objB, binding.JSON); errB == nil {1975 c.String(http.StatusOK, `the body should be formB JSON`)1976 // And it can accepts other formats1972 } else if errB2 := c.ShouldBindBodyWith(&objB, binding.XML); errB2 == nil {1977 } else if errB2 := c.ShouldBindBodyWithXML(&objB); errB2 == nil {1978 c.String(http.StatusOK, `the body should be formB XML`)1979 } else {1980 ...1981 }
311 unmodified lines
modified:context_test.go+257 added
1975 unmodified lines
1976 }1977 }1978}19791980func TestContextShouldBindBodyWithJSON(t *testing.T) {1981 for _, tt := range []struct {1982 name string1983 bindingBody binding.BindingBody1984 body string1985 }{1986 {1987 name: " JSON & JSON-BODY ",1988 bindingBody: binding.JSON,1989 body: `{"foo":"FOO"}`,1990 },1991 {1992 name: " JSON & XML-BODY ",1993 bindingBody: binding.XML,1994 body: `<?xml version="1.0" encoding="UTF-8"?>1995<root>1996<foo>FOO</foo>1997</root>`,1998 },1999 {2000 name: " JSON & YAML-BODY ",2001 bindingBody: binding.YAML,2002 body: `foo: FOO`,2003 },2004 {2005 name: " JSON & TOM-BODY ",2006 bindingBody: binding.TOML,2007 body: `foo=FOO`,2008 },2009 } {2010 t.Logf("testing: %s", tt.name)20112012 w := httptest.NewRecorder()2013 c, _ := CreateTestContext(w)20142015 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString(tt.body))20162017 type typeJSON struct {2018 Foo string `json:"foo" binding:"required"`2019 }2020 objJSON := typeJSON{}20212022 if tt.bindingBody == binding.JSON {2023 assert.NoError(t, c.ShouldBindBodyWithJSON(&objJSON))2024 assert.Equal(t, typeJSON{"FOO"}, objJSON)2025 }20262027 if tt.bindingBody == binding.XML {2028 assert.Error(t, c.ShouldBindBodyWithJSON(&objJSON))2029 assert.Equal(t, typeJSON{}, objJSON)2030 }20312032 if tt.bindingBody == binding.YAML {2033 assert.Error(t, c.ShouldBindBodyWithJSON(&objJSON))2034 assert.Equal(t, typeJSON{}, objJSON)2035 }20362037 if tt.bindingBody == binding.TOML {2038 assert.Error(t, c.ShouldBindBodyWithJSON(&objJSON))2039 assert.Equal(t, typeJSON{}, objJSON)2040 }2041 }2042}20432044func TestContextShouldBindBodyWithXML(t *testing.T) {2045 for _, tt := range []struct {2046 name string2047 bindingBody binding.BindingBody2048 body string2049 }{2050 {2051 name: " XML & JSON-BODY ",2052 bindingBody: binding.JSON,2053 body: `{"foo":"FOO"}`,2054 },2055 {2056 name: " XML & XML-BODY ",2057 bindingBody: binding.XML,2058 body: `<?xml version="1.0" encoding="UTF-8"?>2059<root>2060<foo>FOO</foo>2061</root>`,2062 },2063 {2064 name: " XML & YAML-BODY ",2065 bindingBody: binding.YAML,2066 body: `foo: FOO`,2067 },2068 {2069 name: " XML & TOM-BODY ",2070 bindingBody: binding.TOML,2071 body: `foo=FOO`,2072 },2073 } {2074 t.Logf("testing: %s", tt.name)20752076 w := httptest.NewRecorder()2077 c, _ := CreateTestContext(w)20782079 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString(tt.body))20802081 type typeXML struct {2082 Foo string `xml:"foo" binding:"required"`2083 }2084 objXML := typeXML{}20852086 if tt.bindingBody == binding.JSON {2087 assert.Error(t, c.ShouldBindBodyWithXML(&objXML))2088 assert.Equal(t, typeXML{}, objXML)2089 }20902091 if tt.bindingBody == binding.XML {2092 assert.NoError(t, c.ShouldBindBodyWithXML(&objXML))2093 assert.Equal(t, typeXML{"FOO"}, objXML)2094 }20952096 if tt.bindingBody == binding.YAML {2097 assert.Error(t, c.ShouldBindBodyWithXML(&objXML))2098 assert.Equal(t, typeXML{}, objXML)2099 }21002101 if tt.bindingBody == binding.TOML {2102 assert.Error(t, c.ShouldBindBodyWithXML(&objXML))2103 assert.Equal(t, typeXML{}, objXML)2104 }2105 }2106}21072108func TestContextShouldBindBodyWithYAML(t *testing.T) {2109 for _, tt := range []struct {2110 name string2111 bindingBody binding.BindingBody2112 body string2113 }{2114 {2115 name: " YAML & JSON-BODY ",2116 bindingBody: binding.JSON,2117 body: `{"foo":"FOO"}`,2118 },2119 {2120 name: " YAML & XML-BODY ",2121 bindingBody: binding.XML,2122 body: `<?xml version="1.0" encoding="UTF-8"?>2123<root>2124<foo>FOO</foo>2125</root>`,2126 },2127 {2128 name: " YAML & YAML-BODY ",2129 bindingBody: binding.YAML,2130 body: `foo: FOO`,2131 },2132 {2133 name: " YAML & TOM-BODY ",2134 bindingBody: binding.TOML,2135 body: `foo=FOO`,2136 },2137 } {2138 t.Logf("testing: %s", tt.name)21392140 w := httptest.NewRecorder()2141 c, _ := CreateTestContext(w)21422143 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString(tt.body))21442145 type typeYAML struct {2146 Foo string `yaml:"foo" binding:"required"`2147 }2148 objYAML := typeYAML{}21492150 // YAML belongs to a super collection of JSON, so JSON can be parsed by YAML2151 if tt.bindingBody == binding.JSON {2152 assert.NoError(t, c.ShouldBindBodyWithYAML(&objYAML))2153 assert.Equal(t, typeYAML{"FOO"}, objYAML)2154 }21552156 if tt.bindingBody == binding.XML {2157 assert.Error(t, c.ShouldBindBodyWithYAML(&objYAML))2158 assert.Equal(t, typeYAML{}, objYAML)2159 }21602161 if tt.bindingBody == binding.YAML {2162 assert.NoError(t, c.ShouldBindBodyWithYAML(&objYAML))2163 assert.Equal(t, typeYAML{"FOO"}, objYAML)2164 }21652166 if tt.bindingBody == binding.TOML {2167 assert.Error(t, c.ShouldBindBodyWithYAML(&objYAML))2168 assert.Equal(t, typeYAML{}, objYAML)2169 }2170 }2171}21722173func TestContextShouldBindBodyWithTOML(t *testing.T) {2174 for _, tt := range []struct {2175 name string2176 bindingBody binding.BindingBody2177 body string2178 }{2179 {2180 name: " TOML & JSON-BODY ",2181 bindingBody: binding.JSON,2182 body: `{"foo":"FOO"}`,2183 },2184 {2185 name: " TOML & XML-BODY ",2186 bindingBody: binding.XML,2187 body: `<?xml version="1.0" encoding="UTF-8"?>2188<root>2189<foo>FOO</foo>2190</root>`,2191 },2192 {2193 name: " TOML & YAML-BODY ",2194 bindingBody: binding.YAML,2195 body: `foo: FOO`,2196 },2197 {2198 name: " TOML & TOM-BODY ",2199 bindingBody: binding.TOML,2200 body: `foo = 'FOO'`,2201 },2202 } {2203 t.Logf("testing: %s", tt.name)22042205 w := httptest.NewRecorder()2206 c, _ := CreateTestContext(w)22072208 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString(tt.body))22092210 type typeTOML struct {2211 Foo string `toml:"foo" binding:"required"`2212 }2213 objTOML := typeTOML{}22142215 if tt.bindingBody == binding.JSON {2216 assert.Error(t, c.ShouldBindBodyWithTOML(&objTOML))2217 assert.Equal(t, typeTOML{}, objTOML)2218 }22192220 if tt.bindingBody == binding.XML {2221 assert.Error(t, c.ShouldBindBodyWithTOML(&objTOML))2222 assert.Equal(t, typeTOML{}, objTOML)2223 }22242225 if tt.bindingBody == binding.YAML {2226 assert.Error(t, c.ShouldBindBodyWithTOML(&objTOML))2227 assert.Equal(t, typeTOML{}, objTOML)2228 }22292230 if tt.bindingBody == binding.TOML {2231 assert.NoError(t, c.ShouldBindBodyWithTOML(&objTOML))2232 assert.Equal(t, typeTOML{"FOO"}, objTOML)2233 }2234 }2235}22362237func TestContextGolangContext(t *testing.T) {2238 c, _ := CreateTestContext(httptest.NewRecorder())2239 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))2240 assert.NoError(t, c.Err())
473 unmodified lines
modified:context.go+20 added
772 unmodified lines
773 }774 return bb.BindBody(body, obj)775}776777// ShouldBindBodyWithJSON is a shortcut for c.ShouldBindBodyWith(obj, binding.JSON).778func (c *Context) ShouldBindBodyWithJSON(obj any) error {779 return c.ShouldBindBodyWith(obj, binding.JSON)780}781782// ShouldBindBodyWithXML is a shortcut for c.ShouldBindBodyWith(obj, binding.XML).783func (c *Context) ShouldBindBodyWithXML(obj any) error {784 return c.ShouldBindBodyWith(obj, binding.XML)785}786787// ShouldBindBodyWithYAML is a shortcut for c.ShouldBindBodyWith(obj, binding.YAML).788func (c *Context) ShouldBindBodyWithYAML(obj any) error {789 return c.ShouldBindBodyWith(obj, binding.YAML)790}791792// ShouldBindBodyWithTOML is a shortcut for c.ShouldBindBodyWith(obj, binding.TOML).793func (c *Context) ShouldBindBodyWithTOML(obj any) error {794 return c.ShouldBindBodyWith(obj, binding.TOML)795}796797// ClientIP implements one best effort algorithm to return the real client IP.798// It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not.799// If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-Ip]).800// If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy,
467 unmodified lines