-
Notifications
You must be signed in to change notification settings - Fork 4
/
web-helper-param.go
147 lines (131 loc) · 3.51 KB
/
web-helper-param.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package Figo
import (
"time"
"net/http"
"github.com/go-martini/martini"
"github.com/quexer/utee"
"strings"
"strconv"
)
type ParamHelper struct {
context map[string]string
param martini.Params
}
func (p *ParamHelper) Float64(name string) float64 {
r, pure := wp_func_float64(p.param, name)
p.context[name] = pure
return r
}
func (p *ParamHelper) Bool(name string) bool {
r, pure := wp_func_Bool(p.param, name)
p.context[name] = pure
return r
}
func (p *ParamHelper) Int(name string, defaultVs ...int) int {
r, pure := wp_func_Int(p.param, name)
p.context[name] = pure
return r
}
func (p *ParamHelper) Int64(name string) int64 {
r, pure := wp_func_Int64(p.param, name)
p.context[name] = pure
return r
}
func (p *ParamHelper) Uint64(name string) uint64 {
r, pure := wp_func_Uint64(p.param, name)
p.context[name] = pure
return r
}
func (p *ParamHelper) Time(name, format string) time.Time {
r, pure := wp_func_time(p.param, name, format)
p.context[name] = pure
return r
}
func (p *ParamHelper) TimeLoc(name, format string, loc *time.Location) time.Time {
r, pure := wp_func_time_loc(p.param, name, format, loc)
p.context[name] = pure
return r
}
func (p *ParamHelper) String(name string) string {
r, pure := wp_func_string(p.param, name)
p.context[name] = pure
return r
}
func (p *ParamHelper) IntArr(name, separate string) []int {
r, pure := wp_func_IntArray(p.param, name, separate)
p.context[name] = pure
return r
}
func (p *ParamHelper) Params()map[string]string{
return p.context
}
func Mid_helper_param(c martini.Context, param martini.Params, w http.ResponseWriter) {
c.Map(&ParamHelper{
param: param,
context: make(map[string]string),
})
}
func wp_func_string(param martini.Params, name string) (result string, pure string) {
pure = param[name]
return strings.TrimSpace(pure), pure
}
func wp_func_float64(param martini.Params, name string) (result float64, pure string) {
pure = param[name]
v, err := strconv.ParseFloat(pure, 64)
utee.Chk(err)
return v, pure
}
func wp_func_Bool(param martini.Params, name string) (result bool, pure string) {
pure = param[name]
v, err := strconv.ParseBool(pure)
utee.Chk(err)
return v, pure
}
func wp_func_Int(param martini.Params, name string, defaultVs ...int) (result int, pure string) {
pure = param[name]
v, err := strconv.ParseInt(pure, 10, 32)
if err != nil && len(defaultVs) > 0 {
return defaultVs[0], ""
}
utee.Chk(err)
return int(v), pure
}
func wp_func_Int64(param martini.Params, name string) (result int64, pure string) {
pure = param[name]
v, err := strconv.ParseInt(pure, 10, 64)
utee.Chk(err)
return v, pure
}
func wp_func_Uint64(param martini.Params, name string) (result uint64, pure string) {
pure = param[name]
v, err := TpUint64(pure)
utee.Chk(err)
return v, pure
}
func wp_func_time(param martini.Params, name, format string) (result time.Time, pure string) {
pure = param[name]
t, err := time.Parse(format, pure)
utee.Chk(err)
return t, pure
}
func wp_func_time_loc(param martini.Params, name, format string, loc *time.Location) (result time.Time, pure string) {
pure = param[name]
t, err := time.ParseInLocation(format, pure, loc)
utee.Chk(err)
return t, pure
}
func wp_func_IntArray(param martini.Params, name, separate string) (result []int, pure string) {
pure = param[name]
sv := strings.TrimSpace(pure)
svs := strings.Split(sv, separate)
ivs := make([]int, 0)
for _, v := range svs {
if v == "" {
continue
}
if iv, err := strconv.ParseInt(v, 10, 32); err != nil {
ivs = append(ivs, int(iv))
}
}
return ivs, pure
}