aboutsummaryrefslogtreecommitdiff
path: root/util/util.go
blob: f3845c9411fce7420edfc9601722c0507bd401dd (plain)
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
package util

import (
	"errors"
	"strconv"

	"github.com/julienschmidt/httprouter"
)

func ParseNumberFromParams(name string, p httprouter.Params, unsigned bool) (int, error) {
	snum := p.ByName(name)
	num, err := strconv.Atoi(snum)
	if err != nil {
		return 0, err
	}

	if unsigned && num < 0 {
		return 0, errors.New("number is negative")
	}
	return num, err
}

func ParseIDFromParams(p httprouter.Params) (int, error) {
	return ParseNumberFromParams("id", p, true)
}