@@ -18,10 +18,22 @@ 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 {
22
- NumPages int `json:"numPages"`
23
- ResultArray []int `json:"resultArray"`
24
- Next * string `json:"next,omitempty"`
26
+ NumPages int `json:"numPages"`
27
+ ResultArray []interface {} `json:"resultArray"`
28
+ Next * string `json:"next,omitempty"`
29
+ }
30
+
31
+ // Insecure reversable hashing for string cursors
32
+ func hash (s string ) (int , error ) {
33
+ return strconv .Atoi (s )
34
+ }
35
+ func unhash (h int ) string {
36
+ return strconv .Itoa (h )
25
37
}
26
38
27
39
const total = 20
@@ -45,7 +57,7 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
45
57
46
58
res := PaginationResponse {
47
59
NumPages : int (math .Ceil (float64 (total ) / float64 (limit ))),
48
- ResultArray : make ([]int , 0 ),
60
+ ResultArray : make ([]interface {} , 0 ),
49
61
}
50
62
51
63
for i := start ; i < total && len (res .ResultArray ) < limit ; i ++ {
@@ -77,7 +89,7 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
77
89
78
90
res := PaginationResponse {
79
91
NumPages : int (math .Ceil (float64 (total ) / float64 (limit ))),
80
- ResultArray : make ([]int , 0 ),
92
+ ResultArray : make ([]interface {} , 0 ),
81
93
}
82
94
83
95
for i := offset ; i < total && len (res .ResultArray ) < limit ; i ++ {
@@ -104,7 +116,7 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
104
116
105
117
res := PaginationResponse {
106
118
NumPages : 0 ,
107
- ResultArray : make ([]int , 0 ),
119
+ ResultArray : make ([]interface {} , 0 ),
108
120
}
109
121
110
122
for i := cursor + 1 ; i < total && len (res .ResultArray ) < 15 ; i ++ {
@@ -134,7 +146,7 @@ func HandleURL(w http.ResponseWriter, r *http.Request) {
134
146
135
147
res := PaginationResponse {
136
148
NumPages : 0 ,
137
- ResultArray : make ([]int , 0 ),
149
+ ResultArray : make ([]interface {} , 0 ),
138
150
}
139
151
140
152
if attempts > 1 {
@@ -160,6 +172,31 @@ func HandleURL(w http.ResponseWriter, r *http.Request) {
160
172
}
161
173
}
162
174
175
+ func HandleNonNumericCursor (w http.ResponseWriter , r * http.Request ) {
176
+ queryCursor := r .FormValue ("cursor" )
177
+ var pagination NonNumericCursorRequest
178
+ hasBody := true
179
+ if err := json .NewDecoder (r .Body ).Decode (& pagination ); err != nil {
180
+ hasBody = false
181
+ }
182
+ cursor := getNonNumericValue (queryCursor , hasBody , pagination .Cursor )
183
+
184
+ res := PaginationResponse {
185
+ NumPages : 0 ,
186
+ ResultArray : make ([]interface {}, 0 ),
187
+ }
188
+ var cursorI , _ = hash (cursor )
189
+ for i := cursorI + 1 ; i < total && len (res .ResultArray ) < 15 ; i ++ {
190
+ res .ResultArray = append (res .ResultArray , unhash (i ))
191
+ }
192
+
193
+ w .Header ().Set ("Content-Type" , "application/json" )
194
+ err := json .NewEncoder (w ).Encode (res )
195
+ if err != nil {
196
+ w .WriteHeader (500 )
197
+ }
198
+ }
199
+
163
200
func getValue (queryValue string , hasBody bool , paginationValue int ) int {
164
201
if hasBody {
165
202
return paginationValue
@@ -171,3 +208,15 @@ func getValue(queryValue string, hasBody bool, paginationValue int) int {
171
208
return value
172
209
}
173
210
}
211
+
212
+ func getNonNumericValue (queryValue string , hasBody bool , paginationValue string ) string {
213
+ if hasBody {
214
+ return paginationValue
215
+ } else {
216
+ if queryValue == "" {
217
+ return "-1"
218
+ } else {
219
+ return queryValue
220
+ }
221
+ }
222
+ }
0 commit comments