blob: 52e53bae85034010e8e83ad4ac486ad115798023 [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"
Jon Perritt80251972016-03-09 00:32:30 -06006
7 "github.com/gophercloud/gophercloud"
Jon Perrittc7d828e2016-02-25 03:06:33 -06008)
9
Ash Wilsonc8e68872014-09-16 10:36:56 -040010// MarkerPage is a stricter Page interface that describes additional functionality required for use with NewMarkerPager.
11// For convenience, embed the MarkedPageBase struct.
12type MarkerPage interface {
13 Page
14
Ash Wilson74863512014-09-16 11:45:51 -040015 // LastMarker returns the last "marker" value on this page.
16 LastMarker() (string, error)
Ash Wilsonc8e68872014-09-16 10:36:56 -040017}
18
19// MarkerPageBase is a page in a collection that's paginated by "limit" and "marker" query parameters.
20type MarkerPageBase struct {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040021 PageResult
Ash Wilsonc8e68872014-09-16 10:36:56 -040022
Ash Wilson58c4f672014-09-16 11:50:56 -040023 // Owner is a reference to the embedding struct.
24 Owner MarkerPage
Ash Wilsonc8e68872014-09-16 10:36:56 -040025}
26
27// NextPageURL generates the URL for the page of results after this one.
28func (current MarkerPageBase) NextPageURL() (string, error) {
29 currentURL := current.URL
30
Ash Wilson58c4f672014-09-16 11:50:56 -040031 mark, err := current.Owner.LastMarker()
Ash Wilsonc8e68872014-09-16 10:36:56 -040032 if err != nil {
33 return "", err
34 }
35
36 q := currentURL.Query()
37 q.Set("marker", mark)
38 currentURL.RawQuery = q.Encode()
39
40 return currentURL.String(), nil
41}
Jon Perrittdb319f12015-02-17 19:32:40 -070042
Jon Perritt80251972016-03-09 00:32:30 -060043// IsEmpty satisifies the IsEmpty method of the Page interface
Jon Perrittc7d828e2016-02-25 03:06:33 -060044func (current MarkerPageBase) IsEmpty() (bool, error) {
45 if b, ok := current.Body.([]interface{}); ok {
46 return len(b) == 0, nil
47 }
Jon Perritt80251972016-03-09 00:32:30 -060048 err := gophercloud.ErrUnexpectedType{}
49 err.Expected = "[]interface{}"
50 err.Actual = fmt.Sprintf("%v", reflect.TypeOf(current.Body))
51 return true, err
Jon Perrittc7d828e2016-02-25 03:06:33 -060052}
53
Jon Perrittdb319f12015-02-17 19:32:40 -070054// GetBody returns the linked page's body. This method is needed to satisfy the
55// Page interface.
56func (current MarkerPageBase) GetBody() interface{} {
57 return current.Body
58}