fix go master branch error about Query and Form

This commit is contained in:
thinkerou 2018-08-26 14:34:25 +08:00
parent 85f3e78abc
commit 681e97d11a
3 changed files with 54 additions and 4 deletions

View File

@ -1215,3 +1215,17 @@ func requestWithBody(method, path, body string) (req *http.Request) {
req, _ = http.NewRequest(method, path, bytes.NewBufferString(body)) req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
return 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]"])
}

View File

@ -4,7 +4,10 @@
package binding package binding
import "net/http" import (
"net/http"
"strings"
)
const defaultMemory = 32 * 1024 * 1024 const defaultMemory = 32 * 1024 * 1024
@ -21,7 +24,7 @@ func (formBinding) Bind(req *http.Request, obj interface{}) error {
return err return err
} }
req.ParseMultipartForm(defaultMemory) req.ParseMultipartForm(defaultMemory)
if err := mapForm(obj, req.Form); err != nil { if err := mapForm(obj, resetForm(req.Form)); err != nil {
return err return err
} }
return validate(obj) return validate(obj)
@ -54,3 +57,19 @@ func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
} }
return validate(obj) 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
}

View File

@ -354,7 +354,7 @@ func (c *Context) QueryArray(key string) []string {
// GetQueryArray returns a slice of strings for a given query key, plus // 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. // a boolean value whether at least one value exists for the given key.
func (c *Context) GetQueryArray(key string) ([]string, bool) { 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 values, true
} }
return []string{}, false 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 // GetQueryMap returns a map for a given query key, plus a boolean value
// whether at least one value exists for the given key. // whether at least one value exists for the given key.
func (c *Context) GetQueryMap(key string) (map[string]string, bool) { 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 // 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 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. // 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) { func (c *Context) get(m map[string][]string, key string) (map[string]string, bool) {
dicts := make(map[string]string) dicts := make(map[string]string)