...
1
20
21 package pagination
22
23 import (
24 "net/http"
25 "strconv"
26 )
27
28
29 func Parse(r *http.Request, defaultLimit, defaultOffset, maxLimit int) (int, int) {
30 var offset, limit int
31
32 if offsetParam := r.URL.Query().Get("offset"); offsetParam == "" {
33 offset = defaultOffset
34 } else {
35 if offset64, err := strconv.ParseInt(offsetParam, 10, 64); err != nil {
36 offset = defaultOffset
37 } else {
38 offset = int(offset64)
39 }
40 }
41
42 if limitParam := r.URL.Query().Get("limit"); limitParam == "" {
43 limit = defaultLimit
44 } else {
45 if limit64, err := strconv.ParseInt(limitParam, 10, 64); err != nil {
46 limit = defaultLimit
47 } else {
48 limit = int(limit64)
49 }
50 }
51
52 if limit > maxLimit {
53 limit = maxLimit
54 }
55
56 if limit < 0 {
57 limit = 0
58 }
59
60 if offset < 0 {
61 offset = 0
62 }
63
64 return limit, offset
65 }
66
View as plain text