-
Notifications
You must be signed in to change notification settings - Fork 365
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
feat: add p/avl/pager #2584
base: master
Are you sure you want to change the base?
feat: add p/avl/pager #2584
Conversation
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2584 +/- ##
==========================================
- Coverage 60.95% 60.95% -0.01%
==========================================
Files 564 564
Lines 75273 75273
==========================================
- Hits 45884 45879 -5
- Misses 26019 26022 +3
- Partials 3370 3372 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
Signed-off-by: moul <[email protected]>
@gnolang/devx @gnolang/berty, since we will be using Pager across all social dApps, please review this PR with a focus on its usage and API. Thank you. (Open to anyone, by the way.) |
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.
This is clean and easy for me to follow. From my POV, I think that this looks good.
I tried the pagination with the following:
Edit examples/gno.land/r/demo/users/users.gno. On line 331 in
In a browser, go to http://127.0.0.1:8888/r/demo/users . It shows:
When I click "2", it doesn't change. |
return doc | ||
} | ||
|
||
func splitPathAndQuery(fullPath string) (string, string) { | ||
parts := strings.SplitN(fullPath, "?", 2) |
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.
When I click on page "2", the URL becomes "http://127.0.0.1:8888/r/demo/users?page=2". But I don't think that the query string with the "?" is passed to Render
.
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.
Right, they are not being passed:
https://gno.land/r/g1hy6zry03hg5d8le9s2w4fxme6236hkgd928dun/echorender:foo/bar?baz=bar
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.
Created #2876 in case is helpful
if pageSize < 1 { | ||
return &Page{ | ||
Items: []Item{}, | ||
PageNumber: 0, | ||
PageSize: 0, | ||
TotalItems: totalItems, | ||
TotalPages: totalPages, | ||
HasPrev: false, | ||
HasNext: false, | ||
Pager: p, | ||
} | ||
} | ||
|
||
if pageNumber < 1 { | ||
return &Page{ | ||
Items: []Item{}, | ||
PageNumber: 0, | ||
PageSize: pageSize, | ||
TotalItems: totalItems, | ||
TotalPages: totalPages, | ||
HasPrev: false, | ||
HasNext: pageNumber < totalPages, | ||
Pager: p, | ||
} | ||
} | ||
|
||
if pageNumber > totalPages { | ||
return &Page{ | ||
Items: []Item{}, | ||
PageNumber: pageNumber, | ||
PageSize: pageSize, | ||
TotalItems: totalItems, | ||
TotalPages: totalPages, | ||
HasPrev: totalPages > 0, | ||
HasNext: false, | ||
Pager: 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.
if pageSize < 1 { | |
return &Page{ | |
Items: []Item{}, | |
PageNumber: 0, | |
PageSize: 0, | |
TotalItems: totalItems, | |
TotalPages: totalPages, | |
HasPrev: false, | |
HasNext: false, | |
Pager: p, | |
} | |
} | |
if pageNumber < 1 { | |
return &Page{ | |
Items: []Item{}, | |
PageNumber: 0, | |
PageSize: pageSize, | |
TotalItems: totalItems, | |
TotalPages: totalPages, | |
HasPrev: false, | |
HasNext: pageNumber < totalPages, | |
Pager: p, | |
} | |
} | |
if pageNumber > totalPages { | |
return &Page{ | |
Items: []Item{}, | |
PageNumber: pageNumber, | |
PageSize: pageSize, | |
TotalItems: totalItems, | |
TotalPages: totalPages, | |
HasPrev: totalPages > 0, | |
HasNext: false, | |
Pager: p, | |
} | |
} | |
page := &Page{ | |
TotalItems: totalItems, | |
TotalPages: totalPages, | |
PageSize: pageSize, | |
Pager: p, | |
} | |
// pages without content | |
if pageSize < 1 { | |
return page | |
} | |
// page number provided is not available | |
if pageNumber < 1 { | |
page.HasNext = pageNumber < totalPages | |
return page | |
} | |
// page number provided is outside the range of total pages | |
if pageNumber > totalPages { | |
page.PageNumber = pageNumber | |
page.HasPrev = totalPages > 0 | |
return page | |
} |
items := []Item{} | ||
p.Tree.ReverseIterateByOffset(startIndex, endIndex-startIndex, func(key string, value interface{}) bool { | ||
items = append(items, Item{Key: key, Value: value}) | ||
return false |
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.
offtopic: a bit misleading. Normally, if you want to continue, you return true, but it is what it is. Example from standard library: https://pkg.go.dev/sync#Map.Range
return &Page{ | ||
Items: items, | ||
PageNumber: pageNumber, | ||
PageSize: pageSize, | ||
TotalItems: totalItems, | ||
TotalPages: totalPages, | ||
HasPrev: pageNumber > 1, | ||
HasNext: pageNumber < totalPages, | ||
Pager: 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.
here, you can add items to previously created page instance.
p/demo/avl/pager
r/demo/users
Hey reviewers, in addition to what you wanted to review, I'm specifically curious if you have any better API/usage ideas.
Example: https://github.com/gnolang/gno/pull/2584/files#diff-8d5cbbe072737a7f288f74adcaaace11cacc3d31264e6a001515fcae824394e2R33
Related with #447, #599, #868