Skip to content

Commit 8ff1825

Browse files
authored
Merge pull request #20 from speakeasy-api/chore/nullable-string-cursor
Cursor Pagination: add nullable string cursor with omitempty=false
2 parents d9ff2bf + e1e5ba2 commit 8ff1825

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

internal/pagination/service.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type PaginationResponse struct {
2626
NumPages int `json:"numPages"`
2727
ResultArray []interface{} `json:"resultArray"`
2828
Next *string `json:"next,omitempty"`
29+
Cursor *string `json:"cursor"`
2930
}
3031

3132
type PageInfo struct {
@@ -187,6 +188,8 @@ func HandleURL(w http.ResponseWriter, r *http.Request) {
187188
}
188189

189190
func HandleNonNumericCursor(w http.ResponseWriter, r *http.Request) {
191+
limit := 15
192+
190193
queryCursor := r.FormValue("cursor")
191194
var pagination NonNumericCursorRequest
192195
hasBody := true
@@ -199,11 +202,18 @@ func HandleNonNumericCursor(w http.ResponseWriter, r *http.Request) {
199202
NumPages: 0,
200203
ResultArray: make([]interface{}, 0),
201204
}
205+
202206
var cursorI, _ = hash(cursor)
203-
for i := cursorI + 1; i < total && len(res.ResultArray) < 15; i++ {
207+
for i := cursorI + 1; i < total && len(res.ResultArray) < limit; i++ {
204208
res.ResultArray = append(res.ResultArray, unhash(i))
205209
}
206210

211+
// output cursor to $.cursor in addition to $.resultArray[(@.length-1)]
212+
if len(res.ResultArray) == limit {
213+
cursor, _ := res.ResultArray[len(res.ResultArray)-1].(string)
214+
res.Cursor = &cursor
215+
}
216+
207217
w.Header().Set("Content-Type", "application/json")
208218
err := json.NewEncoder(w).Encode(res)
209219
if err != nil {

0 commit comments

Comments
 (0)