Implement QueryArray and PostArray methods
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Nering · committed by Andrey Nering · parent 4a6bc4a · Browse files · 2 files·+73 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+73 added−0 removed
- other:+73 added−0 removed
Provenance
- Commit
9366e33- Tree
31e6ed6- Author
Andrey Nering <andrey.nering@gmail.com>- Committer
Andrey Nering <andrey.nering@gmail.com>- Parent
4a6bc4a
2 files changed+73 added−0 removed
modified:context_test.go+32 added
249 unmodified lines250 assert.Equal(t, obj.ID, "main")251 assert.Equal(t, obj.Page, 11)252 assert.Equal(t, obj.Both, "")253 assert.Equal(t, obj.Array, []string{"first", "second"})254+255+ values, ok := c.GetQueryArray("array[]")256+ assert.True(t, ok)257+ assert.Equal(t, "first", values[0])258+ assert.Equal(t, "second", values[1])259+260+ values = c.QueryArray("array[]")261+ assert.Equal(t, "first", values[0])262+ assert.Equal(t, "second", values[1])263+264+ values = c.QueryArray("nokey")265+ assert.Equal(t, 0, len(values))266+267+ values = c.QueryArray("both")268+ assert.Equal(t, 1, len(values))269+ assert.Equal(t, "GET", values[0])270 }271 272 func TestContextPostFormMultipart(t *testing.T) {273 c, _, _ := CreateTestContext()40 unmodified lines314 value, ok = c.GetPostForm("nokey")315 assert.False(t, ok)316 assert.Empty(t, value)317 assert.Equal(t, c.DefaultPostForm("nokey", "nothing"), "nothing")318+319+ values, ok := c.GetPostFormArray("array")320+ assert.True(t, ok)321+ assert.Equal(t, "first", values[0])322+ assert.Equal(t, "second", values[1])323+324+ values = c.PostFormArray("array")325+ assert.Equal(t, "first", values[0])326+ assert.Equal(t, "second", values[1])327+328+ values = c.PostFormArray("nokey")329+ assert.Equal(t, 0, len(values))330+331+ values = c.PostFormArray("foo")332+ assert.Equal(t, 1, len(values))333+ assert.Equal(t, "bar", values[0])334 }335 336 func TestContextSetCookie(t *testing.T) {337 c, _, _ := CreateTestContext()392 unmodified linesmodified:context.go+41 added
234 unmodified lines235 }236 return "", false237 }238 239+// QueryArray returns a slice of strings for a given query key.240+// The length of the slice depends on the number of params with the given key.241+func (c *Context) QueryArray(key string) []string {242+ values, _ := c.GetQueryArray(key)243+ return values244+}245+246+// GetQueryArray returns a slice of strings for a given query key, plus247+// a boolean value whether at least one value exists for the given key.248+func (c *Context) GetQueryArray(key string) ([]string, bool) {249+ req := c.Request250+ if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {251+ return values, true252+ }253+ return []string{}, false254+}255+256 // PostForm returns the specified key from a POST urlencoded form or multipart form257 // when it exists, otherwise it returns an empty string `("")`.258 func (c *Context) PostForm(key string) string {259 value, _ := c.GetPostForm(key)30 unmodified lines290 }291 return "", false292 }293 294+// PostFormArray returns a slice of strings for a given form key.295+// The length of the slice depends on the number of params with the given key.296+func (c *Context) PostFormArray(key string) []string {297+ values, _ := c.GetPostFormArray(key)298+ return values299+}300+301+// GetPostFormArray returns a slice of strings for a given form key, plus302+// a boolean value whether at least one value exists for the given key.303+func (c *Context) GetPostFormArray(key string) ([]string, bool) {304+ req := c.Request305+ req.ParseForm()306+ req.ParseMultipartForm(32 << 20) // 32 MB307+ if values := req.PostForm[key]; len(values) > 0 {308+ return values, true309+ }310+ if req.MultipartForm != nil && req.MultipartForm.File != nil {311+ if values := req.MultipartForm.Value[key]; len(values) > 0 {312+ return values, true313+ }314+ }315+ return []string{}, false316+}317+318 // Bind checks the Content-Type to select a binding engine automatically,319 // Depending the "Content-Type" header different bindings are used:320 // "application/json" --> JSON binding321 // "application/xml" --> XML binding282 unmodified lines