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