Skip to content

Commit 619e04f

Browse files
authored
Merge pull request #16 from go-labx/feature/query_enhance
feat: added methods to retrieve query parameters as different data types, such as strings, integers, and floats.
2 parents 73bf3a8 + 98bb82f commit 619e04f

File tree

3 files changed

+487
-0
lines changed

3 files changed

+487
-0
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Changelog
22

3+
## [0.7.0] - Oct 13, 2023
4+
5+
### Added
6+
7+
- `ctx.QueryString(key string) string`: Returns the value of a given query parameter as a string.
8+
- `ctx.QueryInt(key string) (int, error)`: Returns the value of a given query parameter as an int.
9+
- `ctx.QueryUInt(key string) (uint, error)`: Returns the value of a given query parameter as a uint.
10+
- `ctx.QueryInt8(key string) (int8, error)`: Returns the value of a given query parameter as an int8.
11+
- `ctx.QueryUInt8(key string) (uint8, error)`: Returns the value of a given query parameter as a uint8.
12+
- `ctx.QueryInt32(key string) (int32, error)`: Returns the value of a given query parameter as an int32.
13+
- `ctx.QueryUInt32(key string) (uint32, error)`: Returns the value of a given query parameter as a uint32.
14+
- `ctx.QueryInt64(key string) (int64, error)`: Returns the value of a given query parameter as an int64.
15+
- `ctx.QueryUInt64(key string) (uint64, error)`: Returns the value of a given query parameter as a uint64.
16+
- `ctx.QueryFloat32(key string) (float32, error)`: Returns the value of a given query parameter as a float32.
17+
- `ctx.QueryFloat64(key string) (float64, error)`: Returns the value of a given query parameter as a float64.
18+
319
## [0.6.0] - Sep 22, 2023
420

521
### Added

context.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,111 @@ func (c *Context) Query(key string) string {
197197
return c.req.query(key)
198198
}
199199

200+
// QueryString returns the value of a given query parameter as a string.
201+
func (c *Context) QueryString(key string) string {
202+
return c.req.query(key)
203+
}
204+
205+
// QueryInt returns the value of a given query parameter as an int.
206+
func (c *Context) QueryInt(key string) (int, error) {
207+
str := c.req.query(key)
208+
value, err := strconv.Atoi(str)
209+
if err != nil {
210+
return 0, err
211+
}
212+
return value, nil
213+
}
214+
215+
// QueryUInt returns the value of a given query parameter as a uint.
216+
func (c *Context) QueryUInt(key string) (uint, error) {
217+
str := c.req.query(key)
218+
value, err := strconv.ParseUint(str, 10, 32)
219+
if err != nil {
220+
return 0, err
221+
}
222+
return uint(value), nil
223+
}
224+
225+
// QueryInt8 returns the value of a given query parameter as an int8.
226+
func (c *Context) QueryInt8(key string) (int8, error) {
227+
str := c.req.query(key)
228+
value, err := strconv.ParseInt(str, 10, 8)
229+
if err != nil {
230+
return 0, err
231+
}
232+
return int8(value), nil
233+
}
234+
235+
// QueryUInt8 returns the value of a given query parameter as a uint8.
236+
func (c *Context) QueryUInt8(key string) (uint8, error) {
237+
str := c.req.query(key)
238+
value, err := strconv.ParseUint(str, 10, 8)
239+
if err != nil {
240+
return 0, err
241+
}
242+
return uint8(value), nil
243+
}
244+
245+
// QueryInt32 returns the value of a given query parameter as an int32.
246+
func (c *Context) QueryInt32(key string) (int32, error) {
247+
str := c.req.query(key)
248+
value, err := strconv.ParseInt(str, 10, 32)
249+
if err != nil {
250+
return 0, err
251+
}
252+
return int32(value), nil
253+
}
254+
255+
// QueryUInt32 returns the value of a given query parameter as a uint32.
256+
func (c *Context) QueryUInt32(key string) (uint32, error) {
257+
str := c.req.query(key)
258+
value, err := strconv.ParseUint(str, 10, 32)
259+
if err != nil {
260+
return 0, err
261+
}
262+
return uint32(value), nil
263+
}
264+
265+
// QueryInt64 returns the value of a given query parameter as an int64.
266+
func (c *Context) QueryInt64(key string) (int64, error) {
267+
str := c.req.query(key)
268+
value, err := strconv.ParseInt(str, 10, 64)
269+
if err != nil {
270+
return 0, err
271+
}
272+
return value, nil
273+
}
274+
275+
// QueryUInt64 returns the value of a given query parameter as a uint64.
276+
func (c *Context) QueryUInt64(key string) (uint64, error) {
277+
str := c.req.query(key)
278+
value, err := strconv.ParseUint(str, 10, 64)
279+
if err != nil {
280+
return 0, err
281+
}
282+
return value, nil
283+
}
284+
285+
// QueryFloat32 returns the value of a given query parameter as a float32.
286+
func (c *Context) QueryFloat32(key string) (float32, error) {
287+
str := c.req.query(key)
288+
value, err := strconv.ParseFloat(str, 32)
289+
if err != nil {
290+
return 0, err
291+
}
292+
return float32(value), nil
293+
}
294+
295+
// QueryFloat64 returns the value of a given query parameter as a float64.
296+
func (c *Context) QueryFloat64(key string) (float64, error) {
297+
str := c.req.query(key)
298+
value, err := strconv.ParseFloat(str, 64)
299+
if err != nil {
300+
return 0, err
301+
}
302+
return value, nil
303+
}
304+
200305
// Queries returns all query parameters for the req.
201306
func (c *Context) Queries() map[string][]string {
202307
return c.req.queries()

0 commit comments

Comments
 (0)