blob: 26b9d978f863e35736a83c2e0ed99acab12c8384 [file] [log] [blame]
Ash Wilsonc8e68872014-09-16 10:36:56 -04001package pagination
2
Jon Perrittc7d828e2016-02-25 03:06:33 -06003import (
4 "fmt"
5 "reflect"
6)
7
Ash Wilsonc8e68872014-09-16 10:36:56 -04008// MarkerPage is a stricter Page interface that describes additional functionality required for use with NewMarkerPager.
9// For convenience, embed the MarkedPageBase struct.
10type MarkerPage interface {
11 Page
12
Ash Wilson74863512014-09-16 11:45:51 -040013 // LastMarker returns the last "marker" value on this page.
14 LastMarker() (string, error)
Ash Wilsonc8e68872014-09-16 10:36:56 -040015}
16
17// MarkerPageBase is a page in a collection that's paginated by "limit" and "marker" query parameters.
18type MarkerPageBase struct {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040019 PageResult
Ash Wilsonc8e68872014-09-16 10:36:56 -040020
Ash Wilson58c4f672014-09-16 11:50:56 -040021 // Owner is a reference to the embedding struct.
22 Owner MarkerPage
Ash Wilsonc8e68872014-09-16 10:36:56 -040023}
24
25// NextPageURL generates the URL for the page of results after this one.
26func (current MarkerPageBase) NextPageURL() (string, error) {
27 currentURL := current.URL
28
Ash Wilson58c4f672014-09-16 11:50:56 -040029 mark, err := current.Owner.LastMarker()
Ash Wilsonc8e68872014-09-16 10:36:56 -040030 if err != nil {
31 return "", err
32 }
33
34 q := currentURL.Query()
35 q.Set("marker", mark)
36 currentURL.RawQuery = q.Encode()
37
38 return currentURL.String(), nil
39}
Jon Perrittdb319f12015-02-17 19:32:40 -070040
Jon Perrittc7d828e2016-02-25 03:06:33 -060041func (current MarkerPageBase) IsEmpty() (bool, error) {
42 if b, ok := current.Body.([]interface{}); ok {
43 return len(b) == 0, nil
44 }
45 return true, fmt.Errorf("Error while checking if MarkerPageBase was empty: expected []interface type for Body bot got %+v", reflect.TypeOf(current.Body))
46}
47
Jon Perrittdb319f12015-02-17 19:32:40 -070048// GetBody returns the linked page's body. This method is needed to satisfy the
49// Page interface.
50func (current MarkerPageBase) GetBody() interface{} {
51 return current.Body
52}