Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Decode functionality for paging results #20

Merged
merged 3 commits into from
Nov 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions facebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,56 @@ func TestGraphError(t *testing.T) {
t.Logf("facebook error. [e:%v] [message:%v] [type:%v] [code:%v] [subcode:%v]", err, fbErr.Message, fbErr.Type, fbErr.Code, fbErr.ErrorSubcode)
}

type FacebookFriend struct {
Id string `facebook:",required"`
Name string `facebook:",required"`
}

type FacebookFriends struct {
Friends []FacebookFriend `facebook:"data,required"`
}

func TestPagingResultDecode(t *testing.T) {
res := Result{
"data": []interface{}{
map[string]interface{}{
"name": "friend 1",
"id": "1",
},
map[string]interface{}{
"name": "friend 2",
"id": "2",
},
},
"paging": map[string]interface{}{
"next": "https://graph.facebook.com/...",
},
}
paging, err := newPagingResult(nil, res)
if err != nil {
t.Fatalf("cannot create paging result. [e:%v]", err)
}
var friends FacebookFriends
if err := paging.Decode(&friends); err != nil {
t.Fatalf("cannot decode paging result. [e:%v]", err)
}
if len(friends.Friends) != 2 {
t.Fatalf("expect to have 2 friends. [len:%v]", len(friends.Friends))
}
if friends.Friends[0].Name != "friend 1" {
t.Fatalf("expect name to be 'friend 1'. [name:%v]", friends.Friends[0].Name)
}
if friends.Friends[0].Id != "1" {
t.Fatalf("expect id to be '1'. [id:%v]", friends.Friends[0].Id)
}
if friends.Friends[1].Name != "friend 2" {
t.Fatalf("expect name to be 'friend 2'. [name:%v]", friends.Friends[1].Name)
}
if friends.Friends[1].Id != "2" {
t.Fatalf("expect id to be '2'. [id:%v]", friends.Friends[1].Id)
}
}

func TestPagingResult(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
Expand Down
8 changes: 8 additions & 0 deletions paging_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ func (pr *PagingResult) Data() []Result {
return pr.paging.Data
}

// Decodes the current full result to a struct. See Result#Decode.
func (pr *PagingResult) Decode(v interface{}) (err error) {
res := Result{
"data": pr.Data(),
}
return res.Decode(v)
}

// Read previous page.
func (pr *PagingResult) Previous() (noMore bool, err error) {
if !pr.HasPrevious() {
Expand Down