Skip to content

Commit 76b8698

Browse files
committed
add endpoint to support non numeric cursor.
1 parent 8edcb0d commit 76b8698

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func main() {
3535
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
3636
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
3737
r.HandleFunc("/pagination/url", pagination.HandleURL).Methods(http.MethodGet)
38+
r.HandleFunc("/pagination/cursor_non_numeric", pagination.HandleNonNumericCursor).Methods(http.MethodGet)
3839
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet, http.MethodPost)
3940
r.HandleFunc("/retries/after", retries.HandleRetries).Methods(http.MethodGet)
4041
r.HandleFunc("/errors/{status_code}", errors.HandleErrors).Methods(http.MethodGet)

internal/pagination/service.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,27 @@ type CursorRequest struct {
1818
Cursor int `json:"cursor"`
1919
}
2020

21+
type NonNumericCursorRequest struct {
22+
Cursor string `json:"cursor"`
23+
}
24+
2125
type PaginationResponse struct {
26+
<<<<<<< HEAD
2227
NumPages int `json:"numPages"`
2328
ResultArray []int `json:"resultArray"`
2429
Next *string `json:"next,omitempty"`
30+
=======
31+
NumPages int `json:"numPages"`
32+
ResultArray []interface{} `json:"resultArray"`
33+
}
34+
35+
// Insecure reversable hashing for string cursors
36+
func hash(s string) (int, error) {
37+
return strconv.Atoi(s)
38+
}
39+
func unhash(h int) string {
40+
return strconv.Itoa(h)
41+
>>>>>>> cf9ad59 (add endpoint to support non numeric cursor.)
2542
}
2643

2744
const total = 20
@@ -45,7 +62,7 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
4562

4663
res := PaginationResponse{
4764
NumPages: int(math.Ceil(float64(total) / float64(limit))),
48-
ResultArray: make([]int, 0),
65+
ResultArray: make([]interface{}, 0),
4966
}
5067

5168
for i := start; i < total && len(res.ResultArray) < limit; i++ {
@@ -77,7 +94,7 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
7794

7895
res := PaginationResponse{
7996
NumPages: int(math.Ceil(float64(total) / float64(limit))),
80-
ResultArray: make([]int, 0),
97+
ResultArray: make([]interface{}, 0),
8198
}
8299

83100
for i := offset; i < total && len(res.ResultArray) < limit; i++ {
@@ -104,7 +121,7 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
104121

105122
res := PaginationResponse{
106123
NumPages: 0,
107-
ResultArray: make([]int, 0),
124+
ResultArray: make([]interface{}, 0),
108125
}
109126

110127
for i := cursor + 1; i < total && len(res.ResultArray) < 15; i++ {
@@ -160,6 +177,32 @@ func HandleURL(w http.ResponseWriter, r *http.Request) {
160177
}
161178
}
162179

180+
func HandleNonNumericCursor(w http.ResponseWriter, r *http.Request) {
181+
queryCursor := r.FormValue("cursor")
182+
var pagination NonNumericCursorRequest
183+
hasBody := true
184+
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
185+
hasBody = false
186+
}
187+
188+
cursor := getNonNumericValue(queryCursor, hasBody, pagination.Cursor)
189+
190+
res := PaginationResponse{
191+
NumPages: 0,
192+
ResultArray: make([]interface{}, 0),
193+
}
194+
var cursorI, _ = hash(cursor)
195+
for i := cursorI + 1; i < total && len(res.ResultArray) < 15; i++ {
196+
res.ResultArray = append(res.ResultArray, unhash(i))
197+
}
198+
199+
w.Header().Set("Content-Type", "application/json")
200+
err := json.NewEncoder(w).Encode(res)
201+
if err != nil {
202+
w.WriteHeader(500)
203+
}
204+
}
205+
163206
func getValue(queryValue string, hasBody bool, paginationValue int) int {
164207
if hasBody {
165208
return paginationValue
@@ -171,3 +214,11 @@ func getValue(queryValue string, hasBody bool, paginationValue int) int {
171214
return value
172215
}
173216
}
217+
218+
func getNonNumericValue(queryValue string, hasBody bool, paginationValue string) string {
219+
if hasBody {
220+
return paginationValue
221+
} else {
222+
return queryValue
223+
}
224+
}

0 commit comments

Comments
 (0)