mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 18:22:23 +08:00
remove duplicate code
This commit is contained in:
parent
51386de165
commit
2b1decda0b
50
context.go
50
context.go
@ -370,18 +370,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) {
|
||||||
params := c.Request.URL.Query()
|
return c.get(c.Request.URL.Query(), key)
|
||||||
dicts := make(map[string]string)
|
|
||||||
exist := false
|
|
||||||
for k, v := range params {
|
|
||||||
if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {
|
|
||||||
if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
|
|
||||||
exist = true
|
|
||||||
dicts[k[i+1:][:j]] = v[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dicts, exist
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@ -451,30 +440,31 @@ func (c *Context) GetPostFormMap(key string) (map[string]string, bool) {
|
|||||||
req := c.Request
|
req := c.Request
|
||||||
req.ParseForm()
|
req.ParseForm()
|
||||||
req.ParseMultipartForm(c.engine.MaxMultipartMemory)
|
req.ParseMultipartForm(c.engine.MaxMultipartMemory)
|
||||||
dicts := make(map[string]string)
|
dicts, exist := c.get(req.PostForm, key)
|
||||||
exist := false
|
|
||||||
for k, v := range req.PostForm {
|
// when go version is 1.6, exist is false
|
||||||
if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {
|
|
||||||
if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
|
|
||||||
exist = true
|
|
||||||
dicts[k[i+1:][:j]] = v[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !exist && req.MultipartForm != nil && req.MultipartForm.File != nil {
|
if !exist && req.MultipartForm != nil && req.MultipartForm.File != nil {
|
||||||
for k, v := range req.MultipartForm.Value {
|
dicts, exist = c.get(req.MultipartForm.Value, key)
|
||||||
if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {
|
|
||||||
if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
|
|
||||||
exist = true
|
|
||||||
dicts[k[i+1:][:j]] = v[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return dicts, exist
|
return dicts, exist
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
exist := false
|
||||||
|
for k, v := range m {
|
||||||
|
if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {
|
||||||
|
if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
|
||||||
|
exist = true
|
||||||
|
dicts[k[i+1:][:j]] = v[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dicts, exist
|
||||||
|
}
|
||||||
|
|
||||||
// FormFile returns the first file for the provided form key.
|
// FormFile returns the first file for the provided form key.
|
||||||
func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
|
func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
|
||||||
_, fh, err := c.Request.FormFile(name)
|
_, fh, err := c.Request.FormFile(name)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user