-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Improve pagination #96
Conversation
- first pass complete - Limit and Offset ListParams obtained by api/handlers' List functions/methods using `ListParamsFromRequest()` - `params_test.go` added, handles a few cases of invalid limits and offsets - partially complete - added support for accepting an 'orderBy' request query param just now but have not yet updated core/params_test and api/handlers functions to use this value yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lots of feedback that's almost all style oriented :)
api/handlers/datasets.go
Outdated
// args := &core.ListParams{ | ||
// Limit: lp.Limit, | ||
// Offset: lp.Offset, | ||
// OrderBy: "created", //TODO: should there be a global default orderby? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re global default orderby, I'd say no, let's have implementations decide their default order, and have the "default" order passed in be empty string: ""
, which is the default value created by a new struct
api/handlers/datasets.go
Outdated
// TODO: need to update util.WritePageResponse to take a | ||
// core.ListParams rather than a util.Page struct | ||
// for time being I added an empty util.Page struct | ||
if err := util.WritePageResponse(w, res, r, util.Page{}); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copying my slack comments here:
generally, I’d say that things going back to the
api_util
package should return types defined there. we should write a method on ListParams:func (l ListParams) AsPage() util.Page
then in practice we can use something likeutil.WritePageResponse(w, res, r, args.AsPage())
core/params.go
Outdated
@@ -11,3 +26,34 @@ type ListParams struct { | |||
Limit int | |||
Offset int | |||
} | |||
|
|||
// ListParamsFromRequest extracts and returns a ListParams struct | |||
// given a pointer to an http request *r |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just for style purposes, I try to keep comments clear without referring to argument names (b/c often the code changes & people are bad at updating comments. I would write this as:
// ListParamsFromRequest extracts ListParams from an http.Request pointer
core/params.go
Outdated
func ListParamsFromRequest(r *http.Request) ListParams { | ||
var lp ListParams | ||
var pageIndex int | ||
// Limit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for these, should be clear from the code. I'll find the article that talks about implicit inline subroutines via comments & send it over 😄
core/params_test.go
Outdated
|
||
func ListParamsEqual(a, b ListParams) error { | ||
if a.Limit != b.Limit { | ||
return fmt.Errorf("ListParams.Limit fields not equal: '%v' != '%v'", a.Limit, b.Limit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should use %d
, for all integer values in fmt.Printf
functions. see: https://godoc.org/fmt#hdr-Printing.
core/params_test.go
Outdated
return fmt.Errorf("ListParams.Offset fields not equal: '%v' != '%v'", a.Offset, b.Offset) | ||
} | ||
if a.OrderBy != b.OrderBy { | ||
return fmt.Errorf("ListParams.OrderBy fields not equal: '%v' != '%v'", a.OrderBy, b.OrderBy) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should use %s
for writing formatted strings, see above
core/params.go
Outdated
) | ||
|
||
const DEFAULT_PAGE_SIZE = 100 | ||
const DEFAULT_LIST_ORDERING = "created" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as mentioned above, let's nix this & just have empty string be default list ordering. Implementations will have to specify what the default order is.
core/params.go
Outdated
"created": true, | ||
//"modified": true, | ||
//"name": true, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very good use of a map for validation! But again, I think we have to leave it up to implementers to decide what a valid order argument is and isn't. Maintaining that list here will frustrate us in the long run
core/params_test.go
Outdated
res ListParams | ||
expected string | ||
}{ | ||
// [0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personal style preference, if we're adding index hints as comments, I'd do it inline as a preceding multiline comment, or lose them entirely. People forget to update these & then they become a source of confusion.
- cleaned up and deleted commented-out commented out lines added in previous pagination commit - added ListParams `Page()` method - updated core/params and core/params to no longer assume a default value of 'created' -
- cleaned up and deleted commented-out lines that were added in previous pagination commit - added ListParams `Page()` method - updated core/params and core/params to no longer assume a default value of 'created'
…to improve_pagination
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a quick convention change to bring the "page" allocation inline. I don't think readers of this code need to have p declared as it's own var
api/handlers/datasets.go
Outdated
// core.ListParams rather than a util.Page struct | ||
// for time being I added an empty util.Page struct | ||
if err := util.WritePageResponse(w, res, r, util.Page{}); err != nil { | ||
p = args.Page() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p is undefined here.
api/handlers/datasets.go
Outdated
// for time being I added an empty util.Page struct | ||
if err := util.WritePageResponse(w, res, r, util.Page{}); err != nil { | ||
p = args.Page() | ||
if err := util.WritePageResponse(w, res, r, p); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd just change this to read util.WritePageResponse(w, res, r, args.Page())
api/handlers/peers.go
Outdated
// core.ListParams rather than a util.Page struct | ||
// for time being I added an empty util.Page struct | ||
util.WritePageResponse(w, res, r, util.Page{}) | ||
util.WritePageResponse(w, res, r, p) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto here: util.WritePageResponse(w, res, r, args.Page)
…inline expression
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🇲🇴 🐦 🍰
ListParamsFromRequest
function to core/params.goListParamsFromRequest()
instead ofPageFromRequest()
in