This repository was archived by the owner on Mar 29, 2025. It is now read-only.
forked from contentful-labs/contentful-go
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcollection.go
More file actions
201 lines (148 loc) · 4.76 KB
/
Copy pathcollection.go
File metadata and controls
201 lines (148 loc) · 4.76 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package contentful
import (
"bytes"
"encoding/json"
"net/http"
)
// CollectionOptions holds init options
type CollectionOptions struct {
Limit uint16
}
// Collection model
type Collection struct {
Query
c *Client
req *http.Request
page uint16
Sys *Sys `json:"sys"`
Total int `json:"total"`
Skip int `json:"skip"`
Limit int `json:"limit"`
Items []interface{} `json:"items"`
Includes interface{} `json:"includes"`
}
// NewCollection initializes a new collection
func NewCollection(options *CollectionOptions) *Collection {
query := NewQuery()
query.Order("sys.createdAt", true)
if options.Limit > 0 {
query.Limit(options.Limit)
}
return &Collection{
Query: *query,
page: 1,
}
}
// Next makes the col.req
func (col *Collection) Next() (*Collection, error) {
// setup query params
skip := uint16(col.Limit) * (col.page - 1)
col.Query.Skip(skip)
// override request query
col.req.URL.RawQuery = col.Query.String()
// makes api call
err := col.c.do(col.req, col)
if err != nil {
return nil, err
}
col.page++
return col, nil
}
// ToSpace cast Items to Space model
func (col *Collection) ToSpace() []*Space {
var spaces []*Space
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&spaces)
return spaces
}
// ToWebhook cast Items to Webhook model
func (col *Collection) ToWebhook() []*Webhook {
var webhooks []*Webhook
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&webhooks)
return webhooks
}
// ToOrganization cast Items to Organization model
func (col *Collection) ToOrganization() []*Organization {
var organization []*Organization
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&organization)
return organization
}
// ToEntrySnapshot cast Items to Snapshot model for entries
func (col *Collection) ToEntrySnapshot() []*EntrySnapshot {
var snapshot []*EntrySnapshot
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&snapshot)
return snapshot
}
// ToContentTypeSnapshot cast Items to Snapshot model for content types
func (col *Collection) ToContentTypeSnapshot() []*ContentTypeSnapshot {
var snapshot []*ContentTypeSnapshot
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&snapshot)
return snapshot
}
// ToAccessToken cast Items to AccessToken model
func (col *Collection) ToAccessToken() []*AccessToken {
var accessTokens []*AccessToken
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&accessTokens)
return accessTokens
}
// ToEntryTask cast Items to EntryTask model
func (col *Collection) ToEntryTask() []*EntryTask {
var entryTasks []*EntryTask
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&entryTasks)
return entryTasks
}
// ToScheduledAction cast Items to ScheduledAction model
func (col *Collection) ToScheduledAction() []*ScheduledAction {
var scheduledActions []*ScheduledAction
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&scheduledActions)
return scheduledActions
}
// ToEditorInterface cast Items to EditorInterface model
func (col *Collection) ToEditorInterface() []*EditorInterface {
var editorInterface []*EditorInterface
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&editorInterface)
return editorInterface
}
// ToExtension cast Items to Extension model
func (col *Collection) ToExtension() []*Extension {
var extension []*Extension
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&extension)
return extension
}
// ToWebhookCall cast Items to WebhookCall model
func (col *Collection) ToWebhookCall() []*WebhookCall {
var webhookCall []*WebhookCall
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&webhookCall)
return webhookCall
}
// ToUsage cast Items to Usage model
func (col *Collection) ToUsage() []*Usage {
var usage []*Usage
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&usage)
return usage
}
// ToMembership cast Items to Membership model
func (col *Collection) ToMembership() []*Membership {
var membership []*Membership
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&membership)
return membership
}
// ToRole cast Items to Role model
func (col *Collection) ToRole() []*Role {
var role []*Role
byteArray, _ := json.Marshal(col.Items)
_ = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&role)
return role
}