Skip to content

Commit

Permalink
add nullable string cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
2ynn committed Feb 11, 2025
1 parent d9ff2bf commit e1e5ba2
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/pagination/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type PaginationResponse struct {
NumPages int `json:"numPages"`
ResultArray []interface{} `json:"resultArray"`
Next *string `json:"next,omitempty"`
Cursor *string `json:"cursor"`
}

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

func HandleNonNumericCursor(w http.ResponseWriter, r *http.Request) {
limit := 15

queryCursor := r.FormValue("cursor")
var pagination NonNumericCursorRequest
hasBody := true
Expand All @@ -199,11 +202,18 @@ func HandleNonNumericCursor(w http.ResponseWriter, r *http.Request) {
NumPages: 0,
ResultArray: make([]interface{}, 0),
}

var cursorI, _ = hash(cursor)
for i := cursorI + 1; i < total && len(res.ResultArray) < 15; i++ {
for i := cursorI + 1; i < total && len(res.ResultArray) < limit; i++ {
res.ResultArray = append(res.ResultArray, unhash(i))
}

// output cursor to $.cursor in addition to $.resultArray[(@.length-1)]
if len(res.ResultArray) == limit {
cursor, _ := res.ResultArray[len(res.ResultArray)-1].(string)
res.Cursor = &cursor
}

w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(res)
if err != nil {
Expand Down

0 comments on commit e1e5ba2

Please sign in to comment.