Skip to content

Commit

Permalink
Merge pull request #343 from rawmind0/pagination
Browse files Browse the repository at this point in the history
Added ListAll function on types_template.go to depaginate results on collection
  • Loading branch information
dramich authored Feb 10, 2020
2 parents 99f434a + 9035b1e commit 40f8d46
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions generator/type_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type {{.schema.CodeName}}Client struct {
type {{.schema.CodeName}}Operations interface {
List(opts *types.ListOpts) (*{{.schema.CodeName}}Collection, error)
ListAll(opts *types.ListOpts) (*{{.schema.CodeName}}Collection, error)
Create(opts *{{.schema.CodeName}}) (*{{.schema.CodeName}}, error)
Update(existing *{{.schema.CodeName}}, updates interface{}) (*{{.schema.CodeName}}, error)
Replace(existing *{{.schema.CodeName}}) (*{{.schema.CodeName}}, error)
Expand Down Expand Up @@ -98,6 +99,23 @@ func (c *{{.schema.CodeName}}Client) List(opts *types.ListOpts) (*{{.schema.Code
return resp, err
}
func (c *{{.schema.CodeName}}Client) ListAll(opts *types.ListOpts) (*{{.schema.CodeName}}Collection, error) {
resp := &{{.schema.CodeName}}Collection{}
resp, err := c.List(opts)
if err != nil {
return resp, err
}
data := resp.Data
for resp, err = resp.Next(); resp != nil && err == nil; resp, err = resp.Next() {
data = append(data, resp.Data...)
}
if err != nil {
return resp, err
}
resp.Data = data
return resp, err
}
func (cc *{{.schema.CodeName}}Collection) Next() (*{{.schema.CodeName}}Collection, error) {
if cc != nil && cc.Pagination != nil && cc.Pagination.Next != "" {
resp := &{{.schema.CodeName}}Collection{}
Expand Down

0 comments on commit 40f8d46

Please sign in to comment.