Skip to content
This repository was archived by the owner on Sep 6, 2020. It is now read-only.

Commit 05b366f

Browse files
committed
[sync] support map as query string or post
1 parent befbd3e commit 05b366f

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

context.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,18 @@ func (c *Context) GetQueryArray(key string) ([]string, bool) {
388388
return c.GetQuerys(key)
389389
}
390390

391+
// QueryMap returns a map for a given query key.
392+
func (c *Context) QueryMap(key string) map[string]string {
393+
dicts, _ := c.GetQueryMap(key)
394+
return dicts
395+
}
396+
397+
// GetQueryMap returns a map for a given query key, plus a boolean value
398+
// whether at least one value exists for the given key.
399+
func (c *Context) GetQueryMap(key string) (map[string]string, bool) {
400+
return c.get(c.Request.URL.Query(), key)
401+
}
402+
391403
// PostForm returns the specified key from a POST urlencoded form or multipart form
392404
// when it exists, otherwise it returns an empty string `("")`.
393405
func (c *Context) PostForm(key string) string {
@@ -443,6 +455,42 @@ func (c *Context) GetPostFormArray(key string) ([]string, bool) {
443455
return []string{}, false
444456
}
445457

458+
// PostFormMap returns a map for a given form key.
459+
func (c *Context) PostFormMap(key string) map[string]string {
460+
dicts, _ := c.GetPostFormMap(key)
461+
return dicts
462+
}
463+
464+
// GetPostFormMap returns a map for a given form key, plus a boolean value
465+
// whether at least one value exists for the given key.
466+
func (c *Context) GetPostFormMap(key string) (map[string]string, bool) {
467+
req := c.Request
468+
req.ParseForm()
469+
req.ParseMultipartForm(c.engine.MaxMultipartMemory)
470+
dicts, exist := c.get(req.PostForm, key)
471+
472+
if !exist && req.MultipartForm != nil && req.MultipartForm.File != nil {
473+
dicts, exist = c.get(req.MultipartForm.Value, key)
474+
}
475+
476+
return dicts, exist
477+
}
478+
479+
// get is an internal method and returns a map which satisfy conditions.
480+
func (c *Context) get(m map[string][]string, key string) (map[string]string, bool) {
481+
dicts := make(map[string]string)
482+
exist := false
483+
for k, v := range m {
484+
if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {
485+
if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
486+
exist = true
487+
dicts[k[i+1:][:j]] = v[0]
488+
}
489+
}
490+
}
491+
return dicts, exist
492+
}
493+
446494
// FormFile returns the first file for the provided form key.
447495
func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
448496
_, fh, err := c.Request.FormFile(name)

context_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func createMultipartRequest() *http.Request {
5050
must(mw.WriteField("time_local", "31/12/2016 14:55"))
5151
must(mw.WriteField("time_utc", "31/12/2016 14:55"))
5252
must(mw.WriteField("time_location", "31/12/2016 14:55"))
53+
must(mw.WriteField("names[a]", "thinkerou"))
54+
must(mw.WriteField("names[b]", "tianou"))
5355
req, err := http.NewRequest("POST", "/", body)
5456
must(err)
5557
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
@@ -376,7 +378,8 @@ func TestContextQuery(t *testing.T) {
376378
func TestContextQueryAndPostForm(t *testing.T) {
377379
c, _ := CreateTestContext(httptest.NewRecorder())
378380
body := bytes.NewBufferString("foo=bar&page=11&both=&foo=second")
379-
c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main&id=omit&array[]=first&array[]=second", body)
381+
c.Request, _ = http.NewRequest("POST",
382+
"/?both=GET&id=main&id=omit&array[]=first&array[]=second&ids[a]=hi&ids[b]=3.14", body)
380383
c.Request.Header.Add("Content-Type", MIMEPOSTForm)
381384

382385
assert.Equal(t, "bar", c.DefaultPostForm("foo", "none"))
@@ -462,6 +465,30 @@ func TestContextQueryAndPostForm(t *testing.T) {
462465
values = c.QueryArray("both")
463466
assert.Equal(t, 1, len(values))
464467
assert.Equal(t, "GET", values[0])
468+
469+
dicts, ok := c.GetQueryMap("ids")
470+
assert.True(t, ok)
471+
assert.Equal(t, "hi", dicts["a"])
472+
assert.Equal(t, "3.14", dicts["b"])
473+
474+
dicts, ok = c.GetQueryMap("nokey")
475+
assert.False(t, ok)
476+
assert.Equal(t, 0, len(dicts))
477+
478+
dicts, ok = c.GetQueryMap("both")
479+
assert.False(t, ok)
480+
assert.Equal(t, 0, len(dicts))
481+
482+
dicts, ok = c.GetQueryMap("array")
483+
assert.False(t, ok)
484+
assert.Equal(t, 0, len(dicts))
485+
486+
dicts = c.QueryMap("ids")
487+
assert.Equal(t, "hi", dicts["a"])
488+
assert.Equal(t, "3.14", dicts["b"])
489+
490+
dicts = c.QueryMap("nokey")
491+
assert.Equal(t, 0, len(dicts))
465492
}
466493

467494
func TestContextPostFormMultipart(t *testing.T) {
@@ -538,6 +565,22 @@ func TestContextPostFormMultipart(t *testing.T) {
538565
values = c.PostFormArray("foo")
539566
assert.Equal(t, 1, len(values))
540567
assert.Equal(t, "bar", values[0])
568+
569+
dicts, ok := c.GetPostFormMap("names")
570+
assert.True(t, ok)
571+
assert.Equal(t, "thinkerou", dicts["a"])
572+
assert.Equal(t, "tianou", dicts["b"])
573+
574+
dicts, ok = c.GetPostFormMap("nokey")
575+
assert.False(t, ok)
576+
assert.Equal(t, 0, len(dicts))
577+
578+
dicts = c.PostFormMap("names")
579+
assert.Equal(t, "thinkerou", dicts["a"])
580+
assert.Equal(t, "tianou", dicts["b"])
581+
582+
dicts = c.PostFormMap("nokey")
583+
assert.Equal(t, 0, len(dicts))
541584
}
542585

543586
func TestContextSetCookie(t *testing.T) {

mid/render/render.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
_ Render = YAML{}
2929
_ Render = MsgPack{}
3030
_ Render = Reader{}
31+
_ Render = AsciiJSON{}
3132
)
3233

3334
func writeContentType(w http.ResponseWriter, value []string) {

0 commit comments

Comments
 (0)