From 9035b1ea41b08040035677da398f41af96d75207 Mon Sep 17 00:00:00 2001 From: rawmind0 Date: Mon, 10 Feb 2020 11:26:04 +0100 Subject: [PATCH] Added ListAll function on types_template.go to depaginate results on collection --- generator/type_template.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/generator/type_template.go b/generator/type_template.go index 2a640dcf..7bf11ddc 100644 --- a/generator/type_template.go +++ b/generator/type_template.go @@ -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) @@ -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{}