@@ -18,10 +18,27 @@ type CursorRequest struct {
18
18
Cursor int `json:"cursor"`
19
19
}
20
20
21
+ type NonNumericCursorRequest struct {
22
+ Cursor string `json:"cursor"`
23
+ }
24
+
21
25
type PaginationResponse struct {
26
+ << << << < HEAD
22
27
NumPages int `json:"numPages"`
23
28
ResultArray []int `json:"resultArray"`
24
29
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 .)
25
42
}
26
43
27
44
const total = 20
@@ -45,7 +62,7 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
45
62
46
63
res := PaginationResponse {
47
64
NumPages : int (math .Ceil (float64 (total ) / float64 (limit ))),
48
- ResultArray : make ([]int , 0 ),
65
+ ResultArray : make ([]interface {} , 0 ),
49
66
}
50
67
51
68
for i := start ; i < total && len (res .ResultArray ) < limit ; i ++ {
@@ -77,7 +94,7 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
77
94
78
95
res := PaginationResponse {
79
96
NumPages : int (math .Ceil (float64 (total ) / float64 (limit ))),
80
- ResultArray : make ([]int , 0 ),
97
+ ResultArray : make ([]interface {} , 0 ),
81
98
}
82
99
83
100
for i := offset ; i < total && len (res .ResultArray ) < limit ; i ++ {
@@ -104,7 +121,7 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
104
121
105
122
res := PaginationResponse {
106
123
NumPages : 0 ,
107
- ResultArray : make ([]int , 0 ),
124
+ ResultArray : make ([]interface {} , 0 ),
108
125
}
109
126
110
127
for i := cursor + 1 ; i < total && len (res .ResultArray ) < 15 ; i ++ {
@@ -160,6 +177,32 @@ func HandleURL(w http.ResponseWriter, r *http.Request) {
160
177
}
161
178
}
162
179
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
+
163
206
func getValue (queryValue string , hasBody bool , paginationValue int ) int {
164
207
if hasBody {
165
208
return paginationValue
@@ -171,3 +214,11 @@ func getValue(queryValue string, hasBody bool, paginationValue int) int {
171
214
return value
172
215
}
173
216
}
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