blob: 431464b8fdd67eeb0ccec4f0dc6834c073889a7b [file] [log] [blame]
Ash Wilsonc8e68872014-09-16 10:36:56 -04001package pagination
2
Jon Perrittc7d828e2016-02-25 03:06:33 -06003import (
4 "fmt"
5 "reflect"
6)
Ash Wilsonc8e68872014-09-16 10:36:56 -04007
8// LinkedPageBase may be embedded to implement a page that provides navigational "Next" and "Previous" links within its result.
Ash Wilsonfc55c822014-09-25 13:18:16 -04009type LinkedPageBase struct {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040010 PageResult
Ash Wilsonfc55c822014-09-25 13:18:16 -040011
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 Wilsonc8e68872014-09-16 10:36:56 -040018
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.
22func (current LinkedPageBase) NextPageURL() (string, error) {
Ash Wilsonfc55c822014-09-25 13:18:16 -040023 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 Wilsonc8e68872014-09-16 10:36:56 -040030 }
31
Ash Wilsonfc55c822014-09-25 13:18:16 -040032 submap, ok := current.Body.(map[string]interface{})
33 if !ok {
34 return "", fmt.Errorf("Expected an object, but was %#v", current.Body)
Ash Wilsonc8e68872014-09-16 10:36:56 -040035 }
36
Ash Wilsonfc55c822014-09-25 13:18:16 -040037 for {
38 key, path = path[0], path[1:len(path)]
Ash Wilsonc8e68872014-09-16 10:36:56 -040039
Ash Wilsonfc55c822014-09-25 13:18:16 -040040 value, ok := submap[key]
41 if !ok {
42 return "", nil
43 }
44
Ash Wilsonfc55c822014-09-25 13:18:16 -040045 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 Wilsonc8e68872014-09-16 10:36:56 -040064}
Jon Perrittdb319f12015-02-17 19:32:40 -070065
Jon Perrittc7d828e2016-02-25 03:06:33 -060066func (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 Perrittdb319f12015-02-17 19:32:40 -070073// GetBody returns the linked page's body. This method is needed to satisfy the
74// Page interface.
75func (current LinkedPageBase) GetBody() interface{} {
76 return current.Body
77}