-
Notifications
You must be signed in to change notification settings - Fork 6
/
paginated_query.go
86 lines (65 loc) · 1.75 KB
/
paginated_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package soundcloudapi
import (
"encoding/json"
"github.com/pkg/errors"
)
// GetTracks returns any of the items in the PaginatedQuery's collection that match the Track struct type
func (pq *PaginatedQuery) GetTracks() ([]Track, error) {
tracks := make([]Track, 0)
for _, item := range pq.Collection {
track := Track{}
b, err := json.Marshal(item)
if err != nil {
return nil, errors.Wrap(err, "Failed to marshal PaginatedQuery collection item")
}
err = json.Unmarshal(b, &track)
if err != nil {
continue
}
if track.Kind != "track" {
continue
}
tracks = append(tracks, track)
}
return tracks, nil
}
// GetPlaylists returns any of the items in the PaginatedQuery's collection that match the Playlist struct type
func (pq *PaginatedQuery) GetPlaylists() ([]Playlist, error) {
playlists := make([]Playlist, 0)
for _, item := range pq.Collection {
playlist := Playlist{}
b, err := json.Marshal(item)
if err != nil {
return nil, errors.Wrap(err, "Failed to marshal PaginatedQuery collection item")
}
err = json.Unmarshal(b, &playlist)
if err != nil {
continue
}
if playlist.Kind != "playlist" {
continue
}
playlists = append(playlists, playlist)
}
return playlists, nil
}
// GetLikes returns any of the items in the PaginatedQuery's collection that match the Like struct type
func (pq *PaginatedQuery) GetLikes() ([]Like, error) {
likes := make([]Like, 0)
for _, item := range pq.Collection {
like := Like{}
b, err := json.Marshal(item)
if err != nil {
return nil, errors.Wrap(err, "Failed to marshal PaginatedQuery collection item")
}
err = json.Unmarshal(b, &like)
if err != nil {
continue
}
if like.Kind != "like" {
continue
}
likes = append(likes, like)
}
return likes, nil
}