- go version: 1.9
- gin version (or commit ref): master branch
- operating system:
Description
this image is my http post params . I'm going to use c.GetPostFormMap to parse processes field, but I didn't get the expected results. So I wrote a solution myself, as follows:
// 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
}
this image is my parse result .
Screenshots
Comment From: rafimuhammad01
hi @thinkerou @farmerx I created pull request for implementing this feature and also the unit test. Please review it. Thank you! :beer: