Merge pull request #795 from mvpmvh/context_params
Context params
Showing
2 changed files
with
106 additions
and
2 deletions
| ... | @@ -382,8 +382,37 @@ func (c *Controller) GetStrings(key string) []string { | ... | @@ -382,8 +382,37 @@ func (c *Controller) GetStrings(key string) []string { |
| 382 | return []string{} | 382 | return []string{} |
| 383 | } | 383 | } |
| 384 | 384 | ||
| 385 | // GetInt returns input value as int64. | 385 | // GetInt returns input as an int |
| 386 | func (c *Controller) GetInt(key string) (int64, error) { | 386 | func (c *Controller) GetInt(key string) (int, error) { |
| 387 | return strconv.Atoi(c.Ctx.Input.Query(key)) | ||
| 388 | } | ||
| 389 | |||
| 390 | // GetInt8 return input as an int8 | ||
| 391 | func (c *Controller) GetInt8(key string) (int8, error) { | ||
| 392 | i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 8) | ||
| 393 | i8 := int8(i64) | ||
| 394 | |||
| 395 | return i8, err | ||
| 396 | } | ||
| 397 | |||
| 398 | // GetInt16 returns input as an int16 | ||
| 399 | func (c *Controller) GetInt16(key string) (int16, error) { | ||
| 400 | i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 16) | ||
| 401 | i16 := int16(i64) | ||
| 402 | |||
| 403 | return i16, err | ||
| 404 | } | ||
| 405 | |||
| 406 | // GetInt32 returns input as an int32 | ||
| 407 | func (c *Controller) GetInt32(key string) (int32, error) { | ||
| 408 | i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32) | ||
| 409 | i32 := int32(i64) | ||
| 410 | |||
| 411 | return i32, err | ||
| 412 | } | ||
| 413 | |||
| 414 | // GetInt64 returns input value as int64. | ||
| 415 | func (c *Controller) GetInt64(key string) (int64, error) { | ||
| 387 | return strconv.ParseInt(c.Ctx.Input.Query(key), 10, 64) | 416 | return strconv.ParseInt(c.Ctx.Input.Query(key), 10, 64) |
| 388 | } | 417 | } |
| 389 | 418 | ... | ... |
controller_test.go
0 → 100644
| 1 | // Copyright 2014 beego Author. All Rights Reserved. | ||
| 2 | // | ||
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | // you may not use this file except in compliance with the License. | ||
| 5 | // You may obtain a copy of the License at | ||
| 6 | // | ||
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | // | ||
| 9 | // Unless required by applicable law or agreed to in writing, software | ||
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | // See the License for the specific language governing permissions and | ||
| 13 | // limitations under the License. | ||
| 14 | |||
| 15 | package beego | ||
| 16 | |||
| 17 | import ( | ||
| 18 | "fmt" | ||
| 19 | "github.com/astaxie/beego/context" | ||
| 20 | ) | ||
| 21 | |||
| 22 | func ExampleGetInt() { | ||
| 23 | |||
| 24 | i := &context.BeegoInput{Params: map[string]string{"age": "40"}} | ||
| 25 | ctx := &context.Context{Input: i} | ||
| 26 | ctrlr := Controller{Ctx: ctx} | ||
| 27 | |||
| 28 | val, _ := ctrlr.GetInt("age") | ||
| 29 | fmt.Printf("%T", val) | ||
| 30 | //Output: int | ||
| 31 | } | ||
| 32 | |||
| 33 | func ExampleGetInt8() { | ||
| 34 | |||
| 35 | i := &context.BeegoInput{Params: map[string]string{"age": "40"}} | ||
| 36 | ctx := &context.Context{Input: i} | ||
| 37 | ctrlr := Controller{Ctx: ctx} | ||
| 38 | |||
| 39 | val, _ := ctrlr.GetInt8("age") | ||
| 40 | fmt.Printf("%T", val) | ||
| 41 | //Output: int8 | ||
| 42 | } | ||
| 43 | |||
| 44 | func ExampleGetInt16() { | ||
| 45 | |||
| 46 | i := &context.BeegoInput{Params: map[string]string{"age": "40"}} | ||
| 47 | ctx := &context.Context{Input: i} | ||
| 48 | ctrlr := Controller{Ctx: ctx} | ||
| 49 | |||
| 50 | val, _ := ctrlr.GetInt16("age") | ||
| 51 | fmt.Printf("%T", val) | ||
| 52 | //Output: int16 | ||
| 53 | } | ||
| 54 | |||
| 55 | func ExampleGetInt32() { | ||
| 56 | |||
| 57 | i := &context.BeegoInput{Params: map[string]string{"age": "40"}} | ||
| 58 | ctx := &context.Context{Input: i} | ||
| 59 | ctrlr := Controller{Ctx: ctx} | ||
| 60 | |||
| 61 | val, _ := ctrlr.GetInt32("age") | ||
| 62 | fmt.Printf("%T", val) | ||
| 63 | //Output: int32 | ||
| 64 | } | ||
| 65 | |||
| 66 | func ExampleGetInt64() { | ||
| 67 | |||
| 68 | i := &context.BeegoInput{Params: map[string]string{"age": "40"}} | ||
| 69 | ctx := &context.Context{Input: i} | ||
| 70 | ctrlr := Controller{Ctx: ctx} | ||
| 71 | |||
| 72 | val, _ := ctrlr.GetInt64("age") | ||
| 73 | fmt.Printf("%T", val) | ||
| 74 | //Output: int64 | ||
| 75 | } |
-
Please register or sign in to post a comment