From 681e97d11a767be4884d513af023a2a9662bb706 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Sun, 26 Aug 2018 14:34:25 +0800 Subject: [PATCH] fix go master branch error about Query and Form --- binding/binding_test.go | 14 ++++++++++++++ binding/form.go | 23 +++++++++++++++++++++-- context.go | 21 +++++++++++++++++++-- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/binding/binding_test.go b/binding/binding_test.go index efe87669..9198e49b 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -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]"]) +} diff --git a/binding/form.go b/binding/form.go index 0be59660..d1c1530d 100644 --- a/binding/form.go +++ b/binding/form.go @@ -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 +} diff --git a/context.go b/context.go index 063c72f0..7aa0380a 100644 --- a/context.go +++ b/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)