Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 1 | package pagination |
| 2 | |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame] | 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
Jon Perritt | 8025197 | 2016-03-09 00:32:30 -0600 | [diff] [blame^] | 6 | |
| 7 | "github.com/gophercloud/gophercloud" |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame] | 8 | ) |
| 9 | |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 10 | // MarkerPage is a stricter Page interface that describes additional functionality required for use with NewMarkerPager. |
| 11 | // For convenience, embed the MarkedPageBase struct. |
| 12 | type MarkerPage interface { |
| 13 | Page |
| 14 | |
Ash Wilson | 7486351 | 2014-09-16 11:45:51 -0400 | [diff] [blame] | 15 | // LastMarker returns the last "marker" value on this page. |
| 16 | LastMarker() (string, error) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | // MarkerPageBase is a page in a collection that's paginated by "limit" and "marker" query parameters. |
| 20 | type MarkerPageBase struct { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 21 | PageResult |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 22 | |
Ash Wilson | 58c4f67 | 2014-09-16 11:50:56 -0400 | [diff] [blame] | 23 | // Owner is a reference to the embedding struct. |
| 24 | Owner MarkerPage |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | // NextPageURL generates the URL for the page of results after this one. |
| 28 | func (current MarkerPageBase) NextPageURL() (string, error) { |
| 29 | currentURL := current.URL |
| 30 | |
Ash Wilson | 58c4f67 | 2014-09-16 11:50:56 -0400 | [diff] [blame] | 31 | mark, err := current.Owner.LastMarker() |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 32 | 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 Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 42 | |
Jon Perritt | 8025197 | 2016-03-09 00:32:30 -0600 | [diff] [blame^] | 43 | // IsEmpty satisifies the IsEmpty method of the Page interface |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame] | 44 | func (current MarkerPageBase) IsEmpty() (bool, error) { |
| 45 | if b, ok := current.Body.([]interface{}); ok { |
| 46 | return len(b) == 0, nil |
| 47 | } |
Jon Perritt | 8025197 | 2016-03-09 00:32:30 -0600 | [diff] [blame^] | 48 | err := gophercloud.ErrUnexpectedType{} |
| 49 | err.Expected = "[]interface{}" |
| 50 | err.Actual = fmt.Sprintf("%v", reflect.TypeOf(current.Body)) |
| 51 | return true, err |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame] | 52 | } |
| 53 | |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 54 | // GetBody returns the linked page's body. This method is needed to satisfy the |
| 55 | // Page interface. |
| 56 | func (current MarkerPageBase) GetBody() interface{} { |
| 57 | return current.Body |
| 58 | } |