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