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" |
| 6 | ) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 7 | |
| 8 | // 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] | 9 | type LinkedPageBase struct { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 10 | PageResult |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 11 | |
| 12 | // LinkPath lists the keys that should be traversed within a response to arrive at the "next" pointer. |
| 13 | // If any link along the path is missing, an empty URL will be returned. |
| 14 | // If any link results in an unexpected value type, an error will be returned. |
| 15 | // When left as "nil", []string{"links", "next"} will be used as a default. |
| 16 | LinkPath []string |
| 17 | } |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 18 | |
| 19 | // NextPageURL extracts the pagination structure from a JSON response and returns the "next" link, if one is present. |
| 20 | // It assumes that the links are available in a "links" element of the top-level response object. |
| 21 | // If this is not the case, override NextPageURL on your result type. |
| 22 | func (current LinkedPageBase) NextPageURL() (string, error) { |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 23 | var path []string |
| 24 | var key string |
| 25 | |
| 26 | if current.LinkPath == nil { |
| 27 | path = []string{"links", "next"} |
| 28 | } else { |
| 29 | path = current.LinkPath |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 32 | submap, ok := current.Body.(map[string]interface{}) |
| 33 | if !ok { |
| 34 | return "", fmt.Errorf("Expected an object, but was %#v", current.Body) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 37 | for { |
| 38 | key, path = path[0], path[1:len(path)] |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 39 | |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 40 | value, ok := submap[key] |
| 41 | if !ok { |
| 42 | return "", nil |
| 43 | } |
| 44 | |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 45 | if len(path) > 0 { |
| 46 | submap, ok = value.(map[string]interface{}) |
| 47 | if !ok { |
| 48 | return "", fmt.Errorf("Expected an object, but was %#v", value) |
| 49 | } |
| 50 | } else { |
| 51 | if value == nil { |
| 52 | // Actual null element. |
| 53 | return "", nil |
| 54 | } |
| 55 | |
| 56 | url, ok := value.(string) |
| 57 | if !ok { |
| 58 | return "", fmt.Errorf("Expected a string, but was %#v", value) |
| 59 | } |
| 60 | |
| 61 | return url, nil |
| 62 | } |
| 63 | } |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 64 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 65 | |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame^] | 66 | func (current LinkedPageBase) IsEmpty() (bool, error) { |
| 67 | if b, ok := current.Body.([]interface{}); ok { |
| 68 | return len(b) == 0, nil |
| 69 | } |
| 70 | return true, fmt.Errorf("Error while checking if LinkedPageBase was empty: expected []interface type for Body bot got %+v", reflect.TypeOf(current.Body)) |
| 71 | } |
| 72 | |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 73 | // GetBody returns the linked page's body. This method is needed to satisfy the |
| 74 | // Page interface. |
| 75 | func (current LinkedPageBase) GetBody() interface{} { |
| 76 | return current.Body |
| 77 | } |