mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 20:22:20 +08:00
Implement parsing data array type and unit test
This commit is contained in:
parent
2c9e5fe47a
commit
82a5042b32
42
context.go
42
context.go
@ -550,6 +550,48 @@ func (c *Context) PostFormMap(key string) (dicts map[string]string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetPostFormArrayMap returns a map for a given form key, plus a boolean value
|
||||||
|
// whether at least one value exists for the given key.
|
||||||
|
func (c *Context) GetPostFormArrayMap(key string) ([]map[string]string, bool) {
|
||||||
|
req := c.Request
|
||||||
|
req.ParseForm()
|
||||||
|
req.ParseMultipartForm(c.engine.MaxMultipartMemory)
|
||||||
|
dicts, exist := c.getArrayMap(req.PostForm, key)
|
||||||
|
if !exist && req.MultipartForm != nil && req.MultipartForm.File != nil {
|
||||||
|
dicts, exist = c.getArrayMap(req.MultipartForm.Value, key)
|
||||||
|
}
|
||||||
|
return dicts, exist
|
||||||
|
}
|
||||||
|
|
||||||
|
// get is an internal method and returns a map which satisfy conditions.
|
||||||
|
func (c *Context) getArrayMap(m map[string][]string, key string) ([]map[string]string, bool) {
|
||||||
|
dicts := make(map[string]map[string]string)
|
||||||
|
exist := false
|
||||||
|
for k, v := range m {
|
||||||
|
if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key { // 确定name[1]key
|
||||||
|
num := ``
|
||||||
|
if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
|
||||||
|
num = k[i+1:][:j]
|
||||||
|
}
|
||||||
|
if j := strings.IndexByte(k[i+1:], '['); j >= 1 {
|
||||||
|
exist = true
|
||||||
|
field := k[i+1:][j+1 : len(k[i+1:])-1]
|
||||||
|
if dict, ok := dicts[num]; ok {
|
||||||
|
dict[field] = v[0]
|
||||||
|
dicts[num] = dict
|
||||||
|
} else {
|
||||||
|
dicts[num] = map[string]string{field: v[0]}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arr := make([]map[string]string, 0)
|
||||||
|
for _, value := range dicts {
|
||||||
|
arr = append(arr, value)
|
||||||
|
}
|
||||||
|
return arr, exist
|
||||||
|
}
|
||||||
|
|
||||||
// GetPostFormMap returns a map for a given form key, plus a boolean value
|
// GetPostFormMap returns a map for a given form 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) GetPostFormMap(key string) (map[string]string, bool) {
|
func (c *Context) GetPostFormMap(key string) (map[string]string, bool) {
|
||||||
|
@ -56,6 +56,9 @@ func createMultipartRequest() *http.Request {
|
|||||||
must(mw.WriteField("time_location", "31/12/2016 14:55"))
|
must(mw.WriteField("time_location", "31/12/2016 14:55"))
|
||||||
must(mw.WriteField("names[a]", "thinkerou"))
|
must(mw.WriteField("names[a]", "thinkerou"))
|
||||||
must(mw.WriteField("names[b]", "tianou"))
|
must(mw.WriteField("names[b]", "tianou"))
|
||||||
|
must(mw.WriteField("array_map[0][a]", "test a"))
|
||||||
|
must(mw.WriteField("array_map[0][b]", "test b"))
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "/", body)
|
req, err := http.NewRequest("POST", "/", body)
|
||||||
must(err)
|
must(err)
|
||||||
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
||||||
@ -609,6 +612,15 @@ func TestContextPostFormMultipart(t *testing.T) {
|
|||||||
|
|
||||||
dicts = c.PostFormMap("nokey")
|
dicts = c.PostFormMap("nokey")
|
||||||
assert.Equal(t, 0, len(dicts))
|
assert.Equal(t, 0, len(dicts))
|
||||||
|
|
||||||
|
arrayOfDict, ok := c.GetPostFormArrayMap("array_map")
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, "test a", arrayOfDict[0]["a"])
|
||||||
|
assert.Equal(t, "test b", arrayOfDict[0]["b"])
|
||||||
|
|
||||||
|
arrayOfDict, ok = c.GetPostFormArrayMap("array_map_fail")
|
||||||
|
assert.False(t, ok)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextSetCookie(t *testing.T) {
|
func TestContextSetCookie(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user