-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparams.go
52 lines (46 loc) · 1.37 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
package jasco
import (
"fmt"
"github.com/gocraft/web"
"strconv"
)
// PathParams contains parameters embedded in the URL supported by gocraft/web
type PathParams struct {
req *web.Request
}
// String returns a string value corresponding to key. When the key doesn't
// exist, it returns defaultValue.
func (p *PathParams) String(key string, defaultValue string) string {
s, ok := p.req.PathParams[key]
if !ok {
return defaultValue
}
return s
}
// RequiredString return a string value corresponding to the key. When the key
// doesn't exist, it returns an error.
func (p *PathParams) RequiredString(key string) (string, error) {
s, ok := p.req.PathParams[key]
if !ok {
return "", fmt.Errorf("path parameter '%v' doesn't exist", key)
}
return s, nil
}
// Int returns a positive integer value correspoding to the key as uint64. When
// the key doesn't exist, it returns defaultValue. It returns an error if it
// cannot parse the path string parameter as an integer or the value is greater
// than or equal to 2^63.
func (p *PathParams) Int(key string, defaultValue uint64) (uint64, error) {
s, err := p.RequiredString(key)
if err != nil {
return defaultValue, nil
}
return strconv.ParseUint(s, 10, 63)
}
func (p *PathParams) RequiredInt(key string) (uint64, error) {
s, err := p.RequiredString(key)
if err != nil {
return 0, err
}
return strconv.ParseUint(s, 10, 63)
}