diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/util.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..f3845c9 --- /dev/null +++ b/util/util.go | |||
@@ -0,0 +1,25 @@ | |||
1 | package util | ||
2 | |||
3 | import ( | ||
4 | "errors" | ||
5 | "strconv" | ||
6 | |||
7 | "github.com/julienschmidt/httprouter" | ||
8 | ) | ||
9 | |||
10 | func ParseNumberFromParams(name string, p httprouter.Params, unsigned bool) (int, error) { | ||
11 | snum := p.ByName(name) | ||
12 | num, err := strconv.Atoi(snum) | ||
13 | if err != nil { | ||
14 | return 0, err | ||
15 | } | ||
16 | |||
17 | if unsigned && num < 0 { | ||
18 | return 0, errors.New("number is negative") | ||
19 | } | ||
20 | return num, err | ||
21 | } | ||
22 | |||
23 | func ParseIDFromParams(p httprouter.Params) (int, error) { | ||
24 | return ParseNumberFromParams("id", p, true) | ||
25 | } | ||