Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
return from AllPages for SinglePageBase
Browse files Browse the repository at this point in the history
  • Loading branch information
jrperritt committed May 2, 2016
1 parent 231898e commit 057373d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions openstack/compute/v2/servers/results_test.go
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"testing"

"github.com/jrperritt/gophercloud/testhelper/client"
"github.com/rackspace/gophercloud"
th "github.com/rackspace/gophercloud/testhelper"
"golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -97,3 +98,14 @@ KSde3I0ybDz7iS2EtceKB7m4C0slYd+oBkm4efuF00rCOKDwpFq45m0=
th.AssertNoErr(t, err)
th.AssertEquals(t, "ruZKK0tqxRfYm5t7lSJq", pwd)
}

func TestListAddressesAllPages(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleAddressListSuccessfully(t)

allPages, err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").AllPages()
th.AssertNoErr(t, err)
_, err = ExtractAddresses(allPages)
th.AssertNoErr(t, err)
}
14 changes: 13 additions & 1 deletion pagination/pager.go
Expand Up @@ -138,6 +138,11 @@ func (p Pager) AllPages() (Page, error) {
// that type.
pageType := reflect.TypeOf(testPage)

// if it's a single page, just return the testPage (first page)
if _, found := pageType.FieldByName("SinglePageBase"); found {
return testPage, nil
}

// Switch on the page body type. Recognized types are `map[string]interface{}`,
// `[]byte`, and `[]interface{}`.
switch testPage.GetBody().(type) {
Expand All @@ -153,7 +158,14 @@ func (p Pager) AllPages() (Page, error) {
key = k
}
}
pagesSlice = append(pagesSlice, b[key].([]interface{})...)
switch keyType := b[key].(type) {
case map[string]interface{}:
pagesSlice = append(pagesSlice, keyType)
case []interface{}:
pagesSlice = append(pagesSlice, b[key].([]interface{})...)
default:
return false, fmt.Errorf("Unsupported page body type: %+v", keyType)
}
return true, nil
})
if err != nil {
Expand Down

0 comments on commit 057373d

Please sign in to comment.