Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 178 additions & 3 deletions app/controlplane/api/controlplane/v1/pagination.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions app/controlplane/api/controlplane/v1/pagination.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ message CursorPaginationRequest {
(buf.validate.field).ignore = IGNORE_IF_UNPOPULATED
];
}

// OffsetPaginationRequest is used to paginate the results
message OffsetPaginationRequest {
// The (zero-based) offset of the first item returned in the collection.
int32 page = 1 [(buf.validate.field).int32.gte = 1];
// The maximum number of entries to return. If the value exceeds the maximum, then the maximum value will be used.
int32 page_size = 2 [(buf.validate.field).int32.gt = 0, (buf.validate.field).int32.lte = 100];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it mandatory? why don't we have a default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message itself is not required, only if you provide it. The only default values are in the biz layer. I can add the default values here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been searching and I couldn't find a way to specify a default value on proto3, apparently it was an option of proto2. I have found this resource https://stackoverflow.com/questions/33222551/why-are-there-no-custom-default-values-in-proto3

In any case, rest assure there are default values if the pagination is not provided a default pagination object is created:

	// Initialize the pagination options, with default values
	paginationOpts := pagination.NewDefaultOffsetPaginationOpts()

	// Override the pagination options if they are provided
	if req.GetPagination() != nil {
		paginationOpts, err = pagination.NewOffsetPaginationOpts(
			int(req.GetPagination().GetPage()),
			int(req.GetPagination().GetPageSize()),
		)
		if err != nil {
			return nil, handleUseCaseErr(err, s.log)
		}
	}

}

// OffsetPaginationResponse is used to return the pagination information
message OffsetPaginationResponse {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity, is this a common response type for this kind of pagination?

last_page could be calculated by total_count / page_size >= page?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on where you search there is a different response. It seems the most common pattern is:

  • page: current page
  • page_size: the number of results per page
  • total_count: total number of results

I've additionally added the last_page so clients don't need to that exact calculation that you're doing. Instead, another common element is to return the amount of pages. I will remove the last_page and add a total_pages.

// The current page number
int32 page = 1;
// The number of results per page
int32 page_size = 2;
// The total number of results
int32 total_count = 3;
// The total number of pages
int32 total_pages = 4;
}
Loading
Loading