c4c9a50c by astaxie

fix #1081

1 parent a311d712
...@@ -406,106 +406,90 @@ func (c *Controller) GetStrings(key string, def ...[]string) []string { ...@@ -406,106 +406,90 @@ func (c *Controller) GetStrings(key string, def ...[]string) []string {
406 406
407 // GetInt returns input as an int or the default value while it's present and input is blank 407 // GetInt returns input as an int or the default value while it's present and input is blank
408 func (c *Controller) GetInt(key string, def ...int) (int, error) { 408 func (c *Controller) GetInt(key string, def ...int) (int, error) {
409 var defv int
410 if len(def) > 0 {
411 defv = def[0]
412 }
413
414 if strv := c.Ctx.Input.Query(key); strv != "" { 409 if strv := c.Ctx.Input.Query(key); strv != "" {
415 return strconv.Atoi(strv) 410 return strconv.Atoi(strv)
411 } else if len(def) > 0 {
412 return def[0], nil
416 } else { 413 } else {
417 return defv, nil 414 return strconv.Atoi(strv)
418 } 415 }
419 } 416 }
420 417
421 // GetInt8 return input as an int8 or the default value while it's present and input is blank 418 // GetInt8 return input as an int8 or the default value while it's present and input is blank
422 func (c *Controller) GetInt8(key string, def ...int8) (int8, error) { 419 func (c *Controller) GetInt8(key string, def ...int8) (int8, error) {
423 var defv int8
424 if len(def) > 0 {
425 defv = def[0]
426 }
427
428 if strv := c.Ctx.Input.Query(key); strv != "" { 420 if strv := c.Ctx.Input.Query(key); strv != "" {
429 i64, err := strconv.ParseInt(strv, 10, 8) 421 i64, err := strconv.ParseInt(strv, 10, 8)
430 i8 := int8(i64) 422 i8 := int8(i64)
431 return i8, err 423 return i8, err
424 } else if len(def) > 0 {
425 return def[0], nil
432 } else { 426 } else {
433 return defv, nil 427 i64, err := strconv.ParseInt(strv, 10, 8)
428 i8 := int8(i64)
429 return i8, err
434 } 430 }
435 } 431 }
436 432
437 // GetInt16 returns input as an int16 or the default value while it's present and input is blank 433 // GetInt16 returns input as an int16 or the default value while it's present and input is blank
438 func (c *Controller) GetInt16(key string, def ...int16) (int16, error) { 434 func (c *Controller) GetInt16(key string, def ...int16) (int16, error) {
439 var defv int16
440 if len(def) > 0 {
441 defv = def[0]
442 }
443
444 if strv := c.Ctx.Input.Query(key); strv != "" { 435 if strv := c.Ctx.Input.Query(key); strv != "" {
445 i64, err := strconv.ParseInt(strv, 10, 16) 436 i64, err := strconv.ParseInt(strv, 10, 16)
446 i16 := int16(i64) 437 i16 := int16(i64)
447
448 return i16, err 438 return i16, err
439 } else if len(def) > 0 {
440 return def[0], nil
449 } else { 441 } else {
450 return defv, nil 442 i64, err := strconv.ParseInt(strv, 10, 16)
443 i16 := int16(i64)
444 return i16, err
451 } 445 }
452 } 446 }
453 447
454 // GetInt32 returns input as an int32 or the default value while it's present and input is blank 448 // GetInt32 returns input as an int32 or the default value while it's present and input is blank
455 func (c *Controller) GetInt32(key string, def ...int32) (int32, error) { 449 func (c *Controller) GetInt32(key string, def ...int32) (int32, error) {
456 var defv int32
457 if len(def) > 0 {
458 defv = def[0]
459 }
460
461 if strv := c.Ctx.Input.Query(key); strv != "" { 450 if strv := c.Ctx.Input.Query(key); strv != "" {
462 i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32) 451 i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32)
463 i32 := int32(i64) 452 i32 := int32(i64)
464 return i32, err 453 return i32, err
454 } else if len(def) > 0 {
455 return def[0], nil
465 } else { 456 } else {
466 return defv, nil 457 i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32)
458 i32 := int32(i64)
459 return i32, err
467 } 460 }
468 } 461 }
469 462
470 // GetInt64 returns input value as int64 or the default value while it's present and input is blank. 463 // GetInt64 returns input value as int64 or the default value while it's present and input is blank.
471 func (c *Controller) GetInt64(key string, def ...int64) (int64, error) { 464 func (c *Controller) GetInt64(key string, def ...int64) (int64, error) {
472 var defv int64
473 if len(def) > 0 {
474 defv = def[0]
475 }
476
477 if strv := c.Ctx.Input.Query(key); strv != "" { 465 if strv := c.Ctx.Input.Query(key); strv != "" {
478 return strconv.ParseInt(strv, 10, 64) 466 return strconv.ParseInt(strv, 10, 64)
467 } else if len(def) > 0 {
468 return def[0], nil
479 } else { 469 } else {
480 return defv, nil 470 return strconv.ParseInt(strv, 10, 64)
481 } 471 }
482 } 472 }
483 473
484 // GetBool returns input value as bool or the default value while it's present and input is blank. 474 // GetBool returns input value as bool or the default value while it's present and input is blank.
485 func (c *Controller) GetBool(key string, def ...bool) (bool, error) { 475 func (c *Controller) GetBool(key string, def ...bool) (bool, error) {
486 var defv bool
487 if len(def) > 0 {
488 defv = def[0]
489 }
490
491 if strv := c.Ctx.Input.Query(key); strv != "" { 476 if strv := c.Ctx.Input.Query(key); strv != "" {
492 return strconv.ParseBool(strv) 477 return strconv.ParseBool(strv)
478 } else if len(def) > 0 {
479 return def[0], nil
493 } else { 480 } else {
494 return defv, nil 481 return strconv.ParseBool(strv)
495 } 482 }
496 } 483 }
497 484
498 // GetFloat returns input value as float64 or the default value while it's present and input is blank. 485 // GetFloat returns input value as float64 or the default value while it's present and input is blank.
499 func (c *Controller) GetFloat(key string, def ...float64) (float64, error) { 486 func (c *Controller) GetFloat(key string, def ...float64) (float64, error) {
500 var defv float64
501 if len(def) > 0 {
502 defv = def[0]
503 }
504
505 if strv := c.Ctx.Input.Query(key); strv != "" { 487 if strv := c.Ctx.Input.Query(key); strv != "" {
506 return strconv.ParseFloat(c.Ctx.Input.Query(key), 64) 488 return strconv.ParseFloat(strv, 64)
489 } else if len(def) > 0 {
490 return def[0], nil
507 } else { 491 } else {
508 return defv, nil 492 return strconv.ParseFloat(strv, 64)
509 } 493 }
510 } 494 }
511 495
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!