Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 1 | package pagination |
| 2 | |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 3 | import "fmt" |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 4 | |
| 5 | // LinkedPageBase may be embedded to implement a page that provides navigational "Next" and "Previous" links within its result. |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 6 | type LinkedPageBase struct { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 7 | PageResult |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 8 | |
| 9 | // LinkPath lists the keys that should be traversed within a response to arrive at the "next" pointer. |
| 10 | // If any link along the path is missing, an empty URL will be returned. |
| 11 | // If any link results in an unexpected value type, an error will be returned. |
| 12 | // When left as "nil", []string{"links", "next"} will be used as a default. |
| 13 | LinkPath []string |
| 14 | } |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 15 | |
| 16 | // NextPageURL extracts the pagination structure from a JSON response and returns the "next" link, if one is present. |
| 17 | // It assumes that the links are available in a "links" element of the top-level response object. |
| 18 | // If this is not the case, override NextPageURL on your result type. |
| 19 | func (current LinkedPageBase) NextPageURL() (string, error) { |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 20 | var path []string |
| 21 | var key string |
| 22 | |
| 23 | if current.LinkPath == nil { |
| 24 | path = []string{"links", "next"} |
| 25 | } else { |
| 26 | path = current.LinkPath |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 27 | } |
| 28 | |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 29 | submap, ok := current.Body.(map[string]interface{}) |
| 30 | if !ok { |
| 31 | return "", fmt.Errorf("Expected an object, but was %#v", current.Body) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 32 | } |
| 33 | |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 34 | for { |
| 35 | key, path = path[0], path[1:len(path)] |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 36 | |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 37 | value, ok := submap[key] |
| 38 | if !ok { |
| 39 | return "", nil |
| 40 | } |
| 41 | |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 42 | if len(path) > 0 { |
| 43 | submap, ok = value.(map[string]interface{}) |
| 44 | if !ok { |
| 45 | return "", fmt.Errorf("Expected an object, but was %#v", value) |
| 46 | } |
| 47 | } else { |
| 48 | if value == nil { |
| 49 | // Actual null element. |
| 50 | return "", nil |
| 51 | } |
| 52 | |
| 53 | url, ok := value.(string) |
| 54 | if !ok { |
| 55 | return "", fmt.Errorf("Expected a string, but was %#v", value) |
| 56 | } |
| 57 | |
| 58 | return url, nil |
| 59 | } |
| 60 | } |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 61 | } |