mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 01:57:55 +08:00
fix go master branch error about Query and Form
This commit is contained in:
parent
85f3e78abc
commit
681e97d11a
@ -1215,3 +1215,17 @@ func requestWithBody(method, path, body string) (req *http.Request) {
|
||||
req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
|
||||
return
|
||||
}
|
||||
|
||||
func TestResetForm(t *testing.T) {
|
||||
m := make(map[string][]string)
|
||||
m["a"] = []string{"hi"}
|
||||
m["k=v&id=main&id=omit&array[]=first&array[]=second&ids[i]=111&ids[j]=3.14"] = []string{""}
|
||||
|
||||
res := resetForm(m)
|
||||
assert.Equal(t, []string{"hi"}, res["a"])
|
||||
assert.Equal(t, []string{"v"}, res["k"])
|
||||
assert.Equal(t, []string{"main", "omit"}, res["id"])
|
||||
assert.Equal(t, []string{"first", "second"}, res["array[]"])
|
||||
assert.Equal(t, []string{"111"}, res["ids[i]"])
|
||||
assert.Equal(t, []string{"3.14"}, res["ids[j]"])
|
||||
}
|
||||
|
@ -4,7 +4,10 @@
|
||||
|
||||
package binding
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const defaultMemory = 32 * 1024 * 1024
|
||||
|
||||
@ -21,7 +24,7 @@ func (formBinding) Bind(req *http.Request, obj interface{}) error {
|
||||
return err
|
||||
}
|
||||
req.ParseMultipartForm(defaultMemory)
|
||||
if err := mapForm(obj, req.Form); err != nil {
|
||||
if err := mapForm(obj, resetForm(req.Form)); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
@ -54,3 +57,19 @@ func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
||||
|
||||
func resetForm(f map[string][]string) map[string][]string {
|
||||
dicts := make(map[string][]string)
|
||||
for k, v := range f {
|
||||
lists := strings.Split(k, "&")
|
||||
if len(lists) == 1 {
|
||||
dicts[k] = v
|
||||
continue
|
||||
}
|
||||
for _, vv := range lists {
|
||||
p := strings.Split(vv, "=")
|
||||
dicts[p[0]] = append(dicts[p[0]], p[1])
|
||||
}
|
||||
}
|
||||
return dicts
|
||||
}
|
||||
|
21
context.go
21
context.go
@ -354,7 +354,7 @@ func (c *Context) QueryArray(key string) []string {
|
||||
// GetQueryArray returns a slice of strings for a given query key, plus
|
||||
// a boolean value whether at least one value exists for the given key.
|
||||
func (c *Context) GetQueryArray(key string) ([]string, bool) {
|
||||
if values, ok := c.Request.URL.Query()[key]; ok && len(values) > 0 {
|
||||
if values, ok := c.resetQuery()[key]; ok && len(values) > 0 {
|
||||
return values, true
|
||||
}
|
||||
return []string{}, false
|
||||
@ -369,7 +369,7 @@ func (c *Context) QueryMap(key string) map[string]string {
|
||||
// GetQueryMap returns a map for a given query key, plus a boolean value
|
||||
// whether at least one value exists for the given key.
|
||||
func (c *Context) GetQueryMap(key string) (map[string]string, bool) {
|
||||
return c.get(c.Request.URL.Query(), key)
|
||||
return c.get(c.resetQuery(), key)
|
||||
}
|
||||
|
||||
// PostForm returns the specified key from a POST urlencoded form or multipart form
|
||||
@ -448,6 +448,23 @@ func (c *Context) GetPostFormMap(key string) (map[string]string, bool) {
|
||||
return dicts, exist
|
||||
}
|
||||
|
||||
func (c *Context) resetQuery() map[string][]string {
|
||||
dicts := make(map[string][]string)
|
||||
url := c.Request.URL.Query()
|
||||
for k, v := range url {
|
||||
lists := strings.Split(k, "&")
|
||||
if len(lists) == 1 {
|
||||
dicts[k] = v
|
||||
continue
|
||||
}
|
||||
for _, vv := range lists {
|
||||
p := strings.Split(vv, "=")
|
||||
dicts[p[0]] = append(dicts[p[0]], p[1])
|
||||
}
|
||||
}
|
||||
return dicts
|
||||
}
|
||||
|
||||
// get is an internal method and returns a map which satisfy conditions.
|
||||
func (c *Context) get(m map[string][]string, key string) (map[string]string, bool) {
|
||||
dicts := make(map[string]string)
|
||||
|
Loading…
x
Reference in New Issue
Block a user