Skip to content
Myr

Merge pull request #570 from andreynering/array

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Implement QueryArray and PostFormArray methods

authored by Bo-Yi Wu · committed by GitHub · parents c3bfd69, f3ff8f8 · Browse files · 2 files+70 added−5 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+70 added−5 removed

  • other:+70 added−5 removed

2 files+70 added−5 removed

modified:context_test.go+32 added
249 unmodified lines
250 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"})254255 values, ok := c.GetQueryArray("array[]")256 assert.True(t, ok)257 assert.Equal(t, "first", values[0])258 assert.Equal(t, "second", values[1])259260 values = c.QueryArray("array[]")261 assert.Equal(t, "first", values[0])262 assert.Equal(t, "second", values[1])263264 values = c.QueryArray("nokey")265 assert.Equal(t, 0, len(values))266267 values = c.QueryArray("both")268 assert.Equal(t, 1, len(values))269 assert.Equal(t, "GET", values[0])270}271272func TestContextPostFormMultipart(t *testing.T) {273 c, _, _ := CreateTestContext()
40 unmodified lines
314 value, ok = c.GetPostForm("nokey")315 assert.False(t, ok)316 assert.Empty(t, value)317 assert.Equal(t, c.DefaultPostForm("nokey", "nothing"), "nothing")318319 values, ok := c.GetPostFormArray("array")320 assert.True(t, ok)321 assert.Equal(t, "first", values[0])322 assert.Equal(t, "second", values[1])323324 values = c.PostFormArray("array")325 assert.Equal(t, "first", values[0])326 assert.Equal(t, "second", values[1])327328 values = c.PostFormArray("nokey")329 assert.Equal(t, 0, len(values))330331 values = c.PostFormArray("foo")332 assert.Equal(t, 1, len(values))333 assert.Equal(t, "bar", values[0])334}335336func TestContextSetCookie(t *testing.T) {337 c, _, _ := CreateTestContext()
401 unmodified lines
modified:context.go+38 added-5 removed
228 unmodified lines
229// ("Manu", true) == c.GetQuery("name")230// ("", false) == c.GetQuery("id")231// ("", true) == c.GetQuery("lastname")232func (c *Context) GetQuery(key string) (string, bool) {233 if values, ok := c.GetQueryArray(key); ok {234 return values[0], ok235 }236 return "", false237}238239// 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.241func (c *Context) QueryArray(key string) []string {242 values, _ := c.GetQueryArray(key)243 return values244}245246// 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.248func (c *Context) GetQueryArray(key string) ([]string, bool) {249 req := c.Request250 if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {235 return values[0], true251 return values, true252 }237 return "", false253 return []string{}, false254}255256// PostForm returns the specified key from a POST urlencoded form or multipart form257// when it exists, otherwise it returns an empty string `("")`.
19 unmodified lines
277// email=mail@example.com --> ("mail@example.com", true) := GetPostForm("email") // set email to "mail@example.com"278// email= --> ("", true) := GetPostForm("email") // set email to ""279// --> ("", false) := GetPostForm("email") // do nothing with email280func (c *Context) GetPostForm(key string) (string, bool) {281 if values, ok := c.GetPostFormArray(key); ok {282 return values[0], ok283 }284 return "", false285}286287// PostFormArray returns a slice of strings for a given form key.288// The length of the slice depends on the number of params with the given key.289func (c *Context) PostFormArray(key string) []string {290 values, _ := c.GetPostFormArray(key)291 return values292}293294// GetPostFormArray returns a slice of strings for a given form key, plus295// a boolean value whether at least one value exists for the given key.296func (c *Context) GetPostFormArray(key string) ([]string, bool) {297 req := c.Request298 req.ParseForm()299 req.ParseMultipartForm(32 << 20) // 32 MB300 if values := req.PostForm[key]; len(values) > 0 {268 return values[0], true301 return values, true302 }303 if req.MultipartForm != nil && req.MultipartForm.File != nil {304 if values := req.MultipartForm.Value[key]; len(values) > 0 {272 return values[0], true305 return values, true306 }307 }275 return "", false308 return []string{}, false309}310311// Bind checks the Content-Type to select a binding engine automatically,312// Depending the "Content-Type" header different bindings are used:
289 unmodified lines