You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.1 KiB

  1. // This package parse a http request from datatables (jQuery plugin) to a friendly structure
  2. // More details in https://github.com/saulortega/datatables
  3. // import "github.com/saulortega/datatables"
  4. package datatables
  5. import (
  6. "errors"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. )
  11. //Parameter to string
  12. func ptos(r *http.Request, p string) (string, error) {
  13. var s = strings.TrimSpace(r.FormValue(p))
  14. var e error
  15. if _, exte := r.Form[p]; !exte {
  16. e = errors.New("«" + p + "» parameter not received")
  17. }
  18. return s, e
  19. }
  20. //Parameter to string; error on empty
  21. func ptosNoEmpty(r *http.Request, p string) (string, error) {
  22. s, e := ptos(r, p)
  23. if s == "" && e == nil {
  24. e = errors.New("«" + p + "» parameter empty")
  25. }
  26. return s, e
  27. }
  28. //Parameter to int
  29. func ptoi(r *http.Request, p string) (int, error) {
  30. var i int
  31. s, e := ptosNoEmpty(r, p)
  32. if e != nil {
  33. return i, e
  34. }
  35. i, e = strconv.Atoi(s)
  36. return i, e
  37. }
  38. //Parameter to bool
  39. func ptob(r *http.Request, p string) (bool, error) {
  40. var b bool
  41. s, e := ptosNoEmpty(r, p)
  42. if e != nil {
  43. return b, e
  44. }
  45. b, e = strconv.ParseBool(s)
  46. return b, e
  47. }