Skip to content

Pagination util (feature request) #1935

@g4s8

Description

@g4s8

Hi. I frequently use this library in my projects. When working with list operations, I'm writing a lot of similar code, like this:

opt := new(github.ListOptions)

for {
	res, rsp, err := cli.SomeListOperations(args, opts)
        // handle error
	if rsp.NextPage == 0 {
		break
	}
        // process rsp
	opts.Page = rsp.NextPage
}

In one project, I created pagination util to refactor such code into:

opt := new(github.ListOptions)
pagination := new(pagination)

for pagination.moveNext(opts) {
	res, rsp, err := cli.SomeListOperations(args, opts)
        // handle error
	pagination.update(rsp)
        // process rsp
}

The source code is available here: https://github.com/g4s8/gitstrap/blob/master/internal/gitstrap/pagination.go

This util automatically update ListOptions next page from accumulated Response page properties, and return true if next page exist to use it in for loop.

I think that such kind of tool is more convenient than manual operations on response and page options, what do you think? If you agree, I'm ready to submit a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions