Update context.go

# add A new method for parsing form data array type fields
This commit is contained in:
Farmerx 2018-10-29 09:16:30 +08:00 committed by GitHub
parent c65e5efc9a
commit f23fb234f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -432,6 +432,48 @@ func (c *Context) PostFormMap(key string) map[string]string {
return dicts return dicts
} }
// 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) {