default IsEmpty func for Pages
diff --git a/pagination/linked.go b/pagination/linked.go
index e9bd8de..431464b 100644
--- a/pagination/linked.go
+++ b/pagination/linked.go
@@ -1,6 +1,9 @@
package pagination
-import "fmt"
+import (
+ "fmt"
+ "reflect"
+)
// LinkedPageBase may be embedded to implement a page that provides navigational "Next" and "Previous" links within its result.
type LinkedPageBase struct {
@@ -60,6 +63,13 @@
}
}
+func (current LinkedPageBase) IsEmpty() (bool, error) {
+ if b, ok := current.Body.([]interface{}); ok {
+ return len(b) == 0, nil
+ }
+ return true, fmt.Errorf("Error while checking if LinkedPageBase was empty: expected []interface type for Body bot got %+v", reflect.TypeOf(current.Body))
+}
+
// GetBody returns the linked page's body. This method is needed to satisfy the
// Page interface.
func (current LinkedPageBase) GetBody() interface{} {
diff --git a/pagination/linked_test.go b/pagination/linked_test.go
index 2ee5aba..67e6e3c 100644
--- a/pagination/linked_test.go
+++ b/pagination/linked_test.go
@@ -6,7 +6,6 @@
"reflect"
"testing"
- "github.com/mitchellh/mapstructure"
"github.com/gophercloud/gophercloud/testhelper"
)
@@ -18,23 +17,15 @@
func (r LinkedPageResult) IsEmpty() (bool, error) {
is, err := ExtractLinkedInts(r)
- if err != nil {
- return true, nil
- }
- return len(is) == 0, nil
+ return len(is) == 0, err
}
-func ExtractLinkedInts(page Page) ([]int, error) {
- var response struct {
- Ints []int `mapstructure:"ints"`
+func ExtractLinkedInts(r Page) ([]int, error) {
+ var s struct {
+ Ints []int `json:"ints"`
}
-
- err := mapstructure.Decode(page.(LinkedPageResult).Body, &response)
- if err != nil {
- return nil, err
- }
-
- return response.Ints, nil
+ err := (r.(LinkedPageResult)).ExtractInto(&s)
+ return s.Ints, err
}
func createLinked(t *testing.T) Pager {
diff --git a/pagination/marker.go b/pagination/marker.go
index f355afc..26b9d97 100644
--- a/pagination/marker.go
+++ b/pagination/marker.go
@@ -1,5 +1,10 @@
package pagination
+import (
+ "fmt"
+ "reflect"
+)
+
// MarkerPage is a stricter Page interface that describes additional functionality required for use with NewMarkerPager.
// For convenience, embed the MarkedPageBase struct.
type MarkerPage interface {
@@ -33,6 +38,13 @@
return currentURL.String(), nil
}
+func (current MarkerPageBase) IsEmpty() (bool, error) {
+ if b, ok := current.Body.([]interface{}); ok {
+ return len(b) == 0, nil
+ }
+ return true, fmt.Errorf("Error while checking if MarkerPageBase was empty: expected []interface type for Body bot got %+v", reflect.TypeOf(current.Body))
+}
+
// GetBody returns the linked page's body. This method is needed to satisfy the
// Page interface.
func (current MarkerPageBase) GetBody() interface{} {
diff --git a/pagination/single.go b/pagination/single.go
index f78d4ab..2be2ff6 100644
--- a/pagination/single.go
+++ b/pagination/single.go
@@ -1,5 +1,10 @@
package pagination
+import (
+ "fmt"
+ "reflect"
+)
+
// SinglePageBase may be embedded in a Page that contains all of the results from an operation at once.
type SinglePageBase PageResult
@@ -8,6 +13,13 @@
return "", nil
}
+func (current SinglePageBase) IsEmpty() (bool, error) {
+ if b, ok := current.Body.([]interface{}); ok {
+ return len(b) == 0, nil
+ }
+ return true, fmt.Errorf("Error while checking if SinglePageBase was empty: expected []interface type for Body bot got %+v", reflect.TypeOf(current.Body))
+}
+
// GetBody returns the single page's body. This method is needed to satisfy the
// Page interface.
func (current SinglePageBase) GetBody() interface{} {
diff --git a/pagination/single_test.go b/pagination/single_test.go
index b5874cc..2a9466c 100644
--- a/pagination/single_test.go
+++ b/pagination/single_test.go
@@ -5,7 +5,6 @@
"net/http"
"testing"
- "github.com/mitchellh/mapstructure"
"github.com/gophercloud/gophercloud/testhelper"
)
@@ -23,17 +22,12 @@
return len(is) == 0, nil
}
-func ExtractSingleInts(page Page) ([]int, error) {
- var response struct {
- Ints []int `mapstructure:"ints"`
+func ExtractSingleInts(r Page) ([]int, error) {
+ var s struct {
+ Ints []int `json:"ints"`
}
-
- err := mapstructure.Decode(page.(SinglePageResult).Body, &response)
- if err != nil {
- return nil, err
- }
-
- return response.Ints, nil
+ err := (r.(SinglePageResult)).ExtractInto(&s)
+ return s.Ints, err
}
func setupSinglePaged() Pager {