This is by no means done, and needs more tests, docs and bugfixes, but push it early so we have a common base to work on. Based on work by Martin Sehnoutka. Signed-off-by: Tom Gundersen <teg@jklm.no>
35 lines
646 B
Go
35 lines
646 B
Go
package weldr
|
|
|
|
import (
|
|
"errors"
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
func parseOffsetAndLimit(query url.Values) (uint, uint, error) {
|
|
var offset, limit uint64 = 0, 20
|
|
var err error
|
|
|
|
if v := query.Get("offset"); v != "" {
|
|
offset, err = strconv.ParseUint(v, 10, 64)
|
|
if err != nil {
|
|
return 0, 0, errors.New("invalid value for 'offset': " + err.Error())
|
|
}
|
|
}
|
|
|
|
if v := query.Get("limit"); v != "" {
|
|
limit, err = strconv.ParseUint(v, 10, 64)
|
|
if err != nil {
|
|
return 0, 0, errors.New("invalid value for 'limit': " + err.Error())
|
|
}
|
|
}
|
|
|
|
return uint(offset), uint(limit), nil
|
|
}
|
|
|
|
func min(a, b uint) uint {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|