-
Notifications
You must be signed in to change notification settings - Fork 39
/
helpers.go
52 lines (41 loc) · 990 Bytes
/
helpers.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 api
import (
"time"
"github.com/blang/semver/v4"
)
const (
defaultPage uint64 = 1
defaultPerPage uint64 = 10
)
// validatePaginationParams validates the pagination parameters provided,
// setting them to the default values in case they are invalid.
func validatePaginationParams(page, perPage uint64) (uint64, uint64) {
if page < 1 {
page = defaultPage
}
if perPage < 1 {
perPage = defaultPerPage
}
return page, perPage
}
// isTimezoneValid checks if the provided timezone is valid.
func isTimezoneValid(tz string) bool {
if tz == "" {
return false
}
if _, err := time.LoadLocation(tz); err != nil {
return false
}
return true
}
// isValidSemver checks if the provided string represents a valid semver
// version.
func isValidSemver(version string) bool {
if _, err := semver.Make(version); err != nil {
return false
}
return true
}
func sqlPaginate(page, perPage uint64) (uint, uint) {
return uint(perPage), uint(page-1) * uint(perPage)
}