Support default value for controller’s params get
Showing
1 changed file
with
102 additions
and
25 deletions
| ... | @@ -364,66 +364,143 @@ func (c *Controller) ParseForm(obj interface{}) error { | ... | @@ -364,66 +364,143 @@ func (c *Controller) ParseForm(obj interface{}) error { |
| 364 | } | 364 | } |
| 365 | 365 | ||
| 366 | // GetString returns the input value by key string. | 366 | // GetString returns the input value by key string. |
| 367 | func (c *Controller) GetString(key string) string { | 367 | func (c *Controller) GetString(key string, def ...string) string { |
| 368 | return c.Ctx.Input.Query(key) | 368 | var defv string |
| 369 | if len(def) > 0 { | ||
| 370 | defv = def[0] | ||
| 371 | } | ||
| 372 | |||
| 373 | if v := c.Ctx.Input.Query(key); v != "" { | ||
| 374 | return v | ||
| 375 | } else { | ||
| 376 | return defv | ||
| 377 | } | ||
| 369 | } | 378 | } |
| 370 | 379 | ||
| 371 | // GetStrings returns the input string slice by key string. | 380 | // GetStrings returns the input string slice by key string. |
| 372 | // it's designed for multi-value input field such as checkbox(input[type=checkbox]), multi-selection. | 381 | // it's designed for multi-value input field such as checkbox(input[type=checkbox]), multi-selection. |
| 373 | func (c *Controller) GetStrings(key string) []string { | 382 | func (c *Controller) GetStrings(key string, def ...[]string) []string { |
| 383 | var defv []string | ||
| 384 | if len(def) > 0 { | ||
| 385 | defv = def[0] | ||
| 386 | } | ||
| 387 | |||
| 374 | f := c.Input() | 388 | f := c.Input() |
| 375 | if f == nil { | 389 | if f == nil { |
| 376 | return []string{} | 390 | return defv |
| 377 | } | 391 | } |
| 392 | |||
| 378 | vs := f[key] | 393 | vs := f[key] |
| 379 | if len(vs) > 0 { | 394 | if len(vs) > 0 { |
| 380 | return vs | 395 | return vs |
| 396 | } else { | ||
| 397 | return defv | ||
| 381 | } | 398 | } |
| 382 | return []string{} | ||
| 383 | } | 399 | } |
| 384 | 400 | ||
| 385 | // GetInt returns input as an int | 401 | // GetInt returns input as an int |
| 386 | func (c *Controller) GetInt(key string) (int, error) { | 402 | func (c *Controller) GetInt(key string, def ...int) (int, error) { |
| 387 | return strconv.Atoi(c.Ctx.Input.Query(key)) | 403 | var defv int |
| 404 | if len(def) > 0 { | ||
| 405 | defv = def[0] | ||
| 406 | } | ||
| 407 | |||
| 408 | if strv := c.Ctx.Input.Query(key); strv != "" { | ||
| 409 | return strconv.Atoi(strv) | ||
| 410 | } else { | ||
| 411 | return defv, nil | ||
| 412 | } | ||
| 388 | } | 413 | } |
| 389 | 414 | ||
| 390 | // GetInt8 return input as an int8 | 415 | // GetInt8 return input as an int8 |
| 391 | func (c *Controller) GetInt8(key string) (int8, error) { | 416 | func (c *Controller) GetInt8(key string, def ...int8) (int8, error) { |
| 392 | i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 8) | 417 | var defv int8 |
| 393 | i8 := int8(i64) | 418 | if len(def) > 0 { |
| 419 | defv = def[0] | ||
| 420 | } | ||
| 394 | 421 | ||
| 395 | return i8, err | 422 | if strv := c.Ctx.Input.Query(key); strv != "" { |
| 423 | i64, err := strconv.ParseInt(strv, 10, 8) | ||
| 424 | i8 := int8(i64) | ||
| 425 | return i8, err | ||
| 426 | } else { | ||
| 427 | return defv, nil | ||
| 428 | } | ||
| 396 | } | 429 | } |
| 397 | 430 | ||
| 398 | // GetInt16 returns input as an int16 | 431 | // GetInt16 returns input as an int16 |
| 399 | func (c *Controller) GetInt16(key string) (int16, error) { | 432 | func (c *Controller) GetInt16(key string, def ...int16) (int16, error) { |
| 400 | i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 16) | 433 | var defv int16 |
| 401 | i16 := int16(i64) | 434 | if len(def) > 0 { |
| 435 | defv = def[0] | ||
| 436 | } | ||
| 437 | |||
| 438 | if strv := c.Ctx.Input.Query(key); strv != "" { | ||
| 439 | i64, err := strconv.ParseInt(strv, 10, 16) | ||
| 440 | i16 := int16(i64) | ||
| 402 | 441 | ||
| 403 | return i16, err | 442 | return i16, err |
| 443 | } else { | ||
| 444 | return defv, nil | ||
| 445 | } | ||
| 404 | } | 446 | } |
| 405 | 447 | ||
| 406 | // GetInt32 returns input as an int32 | 448 | // GetInt32 returns input as an int32 |
| 407 | func (c *Controller) GetInt32(key string) (int32, error) { | 449 | func (c *Controller) GetInt32(key string, def ...int32) (int32, error) { |
| 408 | i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32) | 450 | var defv int32 |
| 409 | i32 := int32(i64) | 451 | if len(def) > 0 { |
| 452 | defv = def[0] | ||
| 453 | } | ||
| 410 | 454 | ||
| 411 | return i32, err | 455 | if strv := c.Ctx.Input.Query(key); strv != "" { |
| 456 | i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32) | ||
| 457 | i32 := int32(i64) | ||
| 458 | return i32, err | ||
| 459 | } else { | ||
| 460 | return defv, nil | ||
| 461 | } | ||
| 412 | } | 462 | } |
| 413 | 463 | ||
| 414 | // GetInt64 returns input value as int64. | 464 | // GetInt64 returns input value as int64. |
| 415 | func (c *Controller) GetInt64(key string) (int64, error) { | 465 | func (c *Controller) GetInt64(key string, def ...int64) (int64, error) { |
| 416 | return strconv.ParseInt(c.Ctx.Input.Query(key), 10, 64) | 466 | var defv int64 |
| 467 | if len(def) > 0 { | ||
| 468 | defv = def[0] | ||
| 469 | } | ||
| 470 | |||
| 471 | if strv := c.Ctx.Input.Query(key); strv != "" { | ||
| 472 | return strconv.ParseInt(strv, 10, 64) | ||
| 473 | } else { | ||
| 474 | return defv, nil | ||
| 475 | } | ||
| 417 | } | 476 | } |
| 418 | 477 | ||
| 419 | // GetBool returns input value as bool. | 478 | // GetBool returns input value as bool. |
| 420 | func (c *Controller) GetBool(key string) (bool, error) { | 479 | func (c *Controller) GetBool(key string, def ...bool) (bool, error) { |
| 421 | return strconv.ParseBool(c.Ctx.Input.Query(key)) | 480 | var defv bool |
| 481 | if len(def) > 0 { | ||
| 482 | defv = def[0] | ||
| 483 | } | ||
| 484 | |||
| 485 | if strv := c.Ctx.Input.Query(key); strv != "" { | ||
| 486 | return strconv.ParseBool(strv) | ||
| 487 | } else { | ||
| 488 | return defv, nil | ||
| 489 | } | ||
| 422 | } | 490 | } |
| 423 | 491 | ||
| 424 | // GetFloat returns input value as float64. | 492 | // GetFloat returns input value as float64. |
| 425 | func (c *Controller) GetFloat(key string) (float64, error) { | 493 | func (c *Controller) GetFloat(key string, def ...float64) (float64, error) { |
| 426 | return strconv.ParseFloat(c.Ctx.Input.Query(key), 64) | 494 | var defv float64 |
| 495 | if len(def) > 0 { | ||
| 496 | defv = def[0] | ||
| 497 | } | ||
| 498 | |||
| 499 | if strv := c.Ctx.Input.Query(key); strv != "" { | ||
| 500 | return strconv.ParseFloat(c.Ctx.Input.Query(key), 64) | ||
| 501 | } else { | ||
| 502 | return defv, nil | ||
| 503 | } | ||
| 427 | } | 504 | } |
| 428 | 505 | ||
| 429 | // GetFile returns the file data in file upload field named as key. | 506 | // GetFile returns the file data in file upload field named as key. | ... | ... |
-
Please register or sign in to post a comment