Skip to content
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

Merged
merged 6 commits into from
Nov 7, 2017
Merged

fix: Improve pagination #96

merged 6 commits into from
Nov 7, 2017

Conversation

osterbit
Copy link
Contributor

@osterbit osterbit commented Nov 6, 2017

  • first pass:
    • added a ListParamsFromRequest function to core/params.go
      • (also added default page size, default ordering to core/params.go)
    • added core/params_test.go
    • updated api/handlers' List functions/methods to use ListParamsFromRequest() instead of PageFromRequest() in
      • api/handlers/datasets
      • api/handlers/peers
      • api/handlers/queries

Thomas Osterbind added 2 commits November 6, 2017 17:12
- 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
@ghost ghost assigned osterbit Nov 6, 2017
@ghost ghost added the in progress label Nov 6, 2017
Copy link
Member

@b5 b5 left a 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 :)

// args := &core.ListParams{
// Limit: lp.Limit,
// Offset: lp.Offset,
// OrderBy: "created", //TODO: should there be a global default orderby?
Copy link
Member

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

// 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 {
Copy link
Member

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 like util.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
Copy link
Member

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
Copy link
Member

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 😄


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)
Copy link
Member

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.

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)
Copy link
Member

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"
Copy link
Member

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,
}
Copy link
Member

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

res ListParams
expected string
}{
// [0]
Copy link
Member

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.

Thomas Osterbind added 3 commits November 7, 2017 09:48
- 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'
Copy link
Member

@b5 b5 left a 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

// 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()
Copy link
Member

Choose a reason for hiding this comment

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

p is undefined here.

// 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 {
Copy link
Member

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())

// 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)
Copy link
Member

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)

Copy link
Member

@b5 b5 left a comment

Choose a reason for hiding this comment

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

🇲🇴 🐦 🍰

@osterbit osterbit merged commit 4d5647b into master Nov 7, 2017
@ghost ghost removed the in progress label Nov 7, 2017
@osterbit osterbit deleted the improve_pagination branch November 7, 2017 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants