Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 1 | package pagination |
| 2 | |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 3 | // MarkerPage is a stricter Page interface that describes additional functionality required for use with NewMarkerPager. |
| 4 | // For convenience, embed the MarkedPageBase struct. |
| 5 | type MarkerPage interface { |
| 6 | Page |
| 7 | |
Ash Wilson | 7486351 | 2014-09-16 11:45:51 -0400 | [diff] [blame] | 8 | // LastMarker returns the last "marker" value on this page. |
| 9 | LastMarker() (string, error) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 10 | } |
| 11 | |
| 12 | // MarkerPageBase is a page in a collection that's paginated by "limit" and "marker" query parameters. |
| 13 | type MarkerPageBase struct { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame^] | 14 | PageResult |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 15 | |
Ash Wilson | 58c4f67 | 2014-09-16 11:50:56 -0400 | [diff] [blame] | 16 | // Owner is a reference to the embedding struct. |
| 17 | Owner MarkerPage |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | // NextPageURL generates the URL for the page of results after this one. |
| 21 | func (current MarkerPageBase) NextPageURL() (string, error) { |
| 22 | currentURL := current.URL |
| 23 | |
Ash Wilson | 58c4f67 | 2014-09-16 11:50:56 -0400 | [diff] [blame] | 24 | mark, err := current.Owner.LastMarker() |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 25 | if err != nil { |
| 26 | return "", err |
| 27 | } |
| 28 | |
| 29 | q := currentURL.Query() |
| 30 | q.Set("marker", mark) |
| 31 | currentURL.RawQuery = q.Encode() |
| 32 | |
| 33 | return currentURL.String(), nil |
| 34 | } |