/
params.go
163 lines (145 loc) · 4.1 KB
/
params.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2016 zxfonline@sina.com. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
import (
"fmt"
. "misc/strutil"
)
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamU64(key string, def ...uint64) uint64 {
if v, ok := ctx.Params[key]; ok {
return Stou64(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamU32(key string, def ...uint32) uint32 {
if v, ok := ctx.Params[key]; ok {
return Stou32(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamU16(key string, def ...uint16) uint16 {
if v, ok := ctx.Params[key]; ok {
return Stou16(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamU8(key string, def ...uint8) uint8 {
if v, ok := ctx.Params[key]; ok {
return Stou8(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamU(key string, def ...uint) uint {
if v, ok := ctx.Params[key]; ok {
return Stou(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamI64(key string, def ...int64) int64 {
if v, ok := ctx.Params[key]; ok {
return Stoi64(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamI32(key string, def ...int32) int32 {
if v, ok := ctx.Params[key]; ok {
return Stoi32(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamI16(key string, def ...int16) int16 {
if v, ok := ctx.Params[key]; ok {
return Stoi16(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamI8(key string, def ...int8) int8 {
if v, ok := ctx.Params[key]; ok {
return Stoi8(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamI(key string, def ...int) int {
if v, ok := ctx.Params[key]; ok {
return Stoi(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamBol(key string, def ...bool) bool {
if v, ok := ctx.Params[key]; ok {
return StoBol(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamF32(key string, def ...float32) float32 {
if v, ok := ctx.Params[key]; ok {
return Stof32(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamF64(key string, def ...float64) float64 {
if v, ok := ctx.Params[key]; ok {
return Stof64(v, def...)
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
//没找到并且没有默认值则 panic抛错
func (ctx *Context) ParamStr(key string, def ...string) string {
if v, ok := ctx.Params[key]; ok {
return v
} else if len(def) > 0 {
return def[0]
}
panic(fmt.Errorf("no found param:%v", key))
}
// Param returns router param by a given key.没找到并且没有默认值则返回""字符串
func (ctx *Context) Param(key string, def ...string) string {
var defv string
if len(def) > 0 {
defv = def[0]
}
if v, ok := ctx.Params[key]; ok {
return v
}
return defv
}