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

feat(examples): add p/moul/pageable #3414

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
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
225 changes: 19 additions & 206 deletions examples/gno.land/p/demo/avl/pager/pager.gno
Original file line number Diff line number Diff line change
@@ -1,223 +1,36 @@
package pager

import (
"math"
"net/url"
"strconv"

"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
"gno.land/p/moul/pageable"
)

// Pager is a struct that holds the AVL tree and pagination parameters.
type Pager struct {
Tree avl.ITree
PageQueryParam string
SizeQueryParam string
DefaultPageSize int
Reversed bool
}

// Page represents a single page of results.
type Page struct {
Items []Item
PageNumber int
PageSize int
TotalItems int
TotalPages int
HasPrev bool
HasNext bool
Pager *Pager // Reference to the parent Pager
}

// Item represents a key-value pair in the AVL tree.
type Item struct {
Key string
Value interface{}
}

// NewPager creates a new Pager with default values.
func NewPager(tree avl.ITree, defaultPageSize int, reversed bool) *Pager {
return &Pager{
Tree: tree,
PageQueryParam: "page",
SizeQueryParam: "size",
DefaultPageSize: defaultPageSize,
Reversed: reversed,
}
func NewPager(tree avl.ITree, defaultPageSize int, reversed bool) *pageable.Pager {
wrappedTree := NewAVLWrapper(tree)
return pageable.NewPager(wrappedTree, defaultPageSize, reversed)
}

// GetPage retrieves a page of results from the AVL tree.
func (p *Pager) GetPage(pageNumber int) *Page {
return p.GetPageWithSize(pageNumber, p.DefaultPageSize)
// AVLWrapper adapts an avl.ITree to implement Pageable
type AVLWrapper struct {
tree avl.ITree
}

func (p *Pager) GetPageWithSize(pageNumber, pageSize int) *Page {
totalItems := p.Tree.Size()
totalPages := int(math.Ceil(float64(totalItems) / float64(pageSize)))

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 = totalPages > 0
return page
}

// page number provided is outside the range of total pages
if pageNumber > totalPages {
page.PageNumber = pageNumber
page.HasPrev = pageNumber > 0
return page
}

startIndex := (pageNumber - 1) * pageSize
endIndex := startIndex + pageSize
if endIndex > totalItems {
endIndex = totalItems
}

items := []Item{}

if p.Reversed {
p.Tree.ReverseIterateByOffset(startIndex, endIndex-startIndex, func(key string, value interface{}) bool {
items = append(items, Item{Key: key, Value: value})
return false
})
} else {
p.Tree.IterateByOffset(startIndex, endIndex-startIndex, func(key string, value interface{}) bool {
items = append(items, Item{Key: key, Value: value})
return false
})
}

page.Items = items
page.PageNumber = pageNumber
page.HasPrev = pageNumber > 1
page.HasNext = pageNumber < totalPages
return page
func NewAVLWrapper(tree avl.ITree) *AVLWrapper {
return &AVLWrapper{tree: tree}
}

func (p *Pager) MustGetPageByPath(rawURL string) *Page {
page, err := p.GetPageByPath(rawURL)
if err != nil {
panic("invalid path")
}
return page
func (w *AVLWrapper) Size() int {
return w.tree.Size()
}

// GetPageByPath retrieves a page of results based on the query parameters in the URL path.
func (p *Pager) GetPageByPath(rawURL string) (*Page, error) {
pageNumber, pageSize, err := p.ParseQuery(rawURL)
if err != nil {
return nil, err
}
return p.GetPageWithSize(pageNumber, pageSize), nil
}

// Picker generates the Markdown UI for the page Picker
func (p *Page) Picker() string {
pageNumber := p.PageNumber
pageNumber = max(pageNumber, 1)

if p.TotalPages <= 1 {
return ""
}

md := ""

if p.HasPrev {
// Always show the first page link
md += ufmt.Sprintf("[%d](?%s=%d) | ", 1, p.Pager.PageQueryParam, 1)

// Before
if p.PageNumber > 4 {
md += "… | "
}

if p.PageNumber > 3 {
md += ufmt.Sprintf("[%d](?%s=%d) | ", p.PageNumber-2, p.Pager.PageQueryParam, p.PageNumber-2)
}

if p.PageNumber > 2 {
md += ufmt.Sprintf("[%d](?%s=%d) | ", p.PageNumber-1, p.Pager.PageQueryParam, p.PageNumber-1)
}
}

if p.PageNumber > 0 && p.PageNumber <= p.TotalPages {
// Current page
md += ufmt.Sprintf("**%d**", p.PageNumber)
} else {
md += ufmt.Sprintf("_%d_", p.PageNumber)
}

if p.HasNext {
md += " | "

if p.PageNumber < p.TotalPages-1 {
md += ufmt.Sprintf("[%d](?%s=%d) | ", p.PageNumber+1, p.Pager.PageQueryParam, p.PageNumber+1)
}

if p.PageNumber < p.TotalPages-2 {
md += ufmt.Sprintf("[%d](?%s=%d) | ", p.PageNumber+2, p.Pager.PageQueryParam, p.PageNumber+2)
}

if p.PageNumber < p.TotalPages-3 {
md += "… | "
}

// Always show the last page link
md += ufmt.Sprintf("[%d](?%s=%d)", p.TotalPages, p.Pager.PageQueryParam, p.TotalPages)
}

return md
}

// ParseQuery parses the URL to extract the page number and page size.
func (p *Pager) ParseQuery(rawURL string) (int, int, error) {
u, err := url.Parse(rawURL)
if err != nil {
return 1, p.DefaultPageSize, err
}

query := u.Query()
pageNumber := 1
pageSize := p.DefaultPageSize

if p.PageQueryParam != "" {
if pageStr := query.Get(p.PageQueryParam); pageStr != "" {
pageNumber, err = strconv.Atoi(pageStr)
if err != nil || pageNumber < 1 {
pageNumber = 1
}
}
}

if p.SizeQueryParam != "" {
if sizeStr := query.Get(p.SizeQueryParam); sizeStr != "" {
pageSize, err = strconv.Atoi(sizeStr)
if err != nil || pageSize < 1 {
pageSize = p.DefaultPageSize
}
}
}

return pageNumber, pageSize, nil
}

func max(a, b int) int {
if a > b {
return a
func (w *AVLWrapper) IterateByOffset(offset int, count int, cb func(index interface{}, value interface{}) bool) bool {
if count < 0 {
return w.tree.ReverseIterateByOffset(offset, count, func(key string, value interface{}) bool {
return cb(key, value)
})
}
return b
return w.tree.IterateByOffset(offset, count, func(key string, value interface{}) bool {
return cb(key, value)
})
}
37 changes: 19 additions & 18 deletions examples/gno.land/p/demo/avl/pager/pager_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gno.land/p/demo/uassert"
"gno.land/p/demo/ufmt"
"gno.land/p/demo/urequire"
"gno.land/p/moul/pageable"
)

func TestPager_GetPage(t *testing.T) {
Expand All @@ -26,15 +27,15 @@ func TestPager_GetPage(t *testing.T) {
tests := []struct {
pageNumber int
pageSize int
expected []Item
expected []pageable.Item
}{
{1, 2, []Item{{Key: "a", Value: 1}, {Key: "b", Value: 2}}},
{2, 2, []Item{{Key: "c", Value: 3}, {Key: "d", Value: 4}}},
{3, 2, []Item{{Key: "e", Value: 5}}},
{1, 3, []Item{{Key: "a", Value: 1}, {Key: "b", Value: 2}, {Key: "c", Value: 3}}},
{2, 3, []Item{{Key: "d", Value: 4}, {Key: "e", Value: 5}}},
{1, 5, []Item{{Key: "a", Value: 1}, {Key: "b", Value: 2}, {Key: "c", Value: 3}, {Key: "d", Value: 4}, {Key: "e", Value: 5}}},
{2, 5, []Item{}},
{1, 2, []pageable.Item{{Index: "a", Value: 1}, {Index: "b", Value: 2}}},
{2, 2, []pageable.Item{{Index: "c", Value: 3}, {Index: "d", Value: 4}}},
{3, 2, []pageable.Item{{Index: "e", Value: 5}}},
{1, 3, []pageable.Item{{Index: "a", Value: 1}, {Index: "b", Value: 2}, {Index: "c", Value: 3}}},
{2, 3, []pageable.Item{{Index: "d", Value: 4}, {Index: "e", Value: 5}}},
{1, 5, []pageable.Item{{Index: "a", Value: 1}, {Index: "b", Value: 2}, {Index: "c", Value: 3}, {Index: "d", Value: 4}, {Index: "e", Value: 5}}},
{2, 5, []pageable.Item{}},
}

for _, tt := range tests {
Expand All @@ -43,7 +44,7 @@ func TestPager_GetPage(t *testing.T) {
uassert.Equal(t, len(tt.expected), len(page.Items))

for i, item := range page.Items {
uassert.Equal(t, tt.expected[i].Key, item.Key)
uassert.Equal(t, tt.expected[i].Index, item.Index)
uassert.Equal(t, tt.expected[i].Value, item.Value)
}
}
Expand All @@ -57,15 +58,15 @@ func TestPager_GetPage(t *testing.T) {
tests := []struct {
pageNumber int
pageSize int
expected []Item
expected []pageable.Item
}{
{1, 2, []Item{{Key: "e", Value: 5}, {Key: "d", Value: 4}}},
{2, 2, []Item{{Key: "c", Value: 3}, {Key: "b", Value: 2}}},
{3, 2, []Item{{Key: "a", Value: 1}}},
{1, 3, []Item{{Key: "e", Value: 5}, {Key: "d", Value: 4}, {Key: "c", Value: 3}}},
{2, 3, []Item{{Key: "b", Value: 2}, {Key: "a", Value: 1}}},
{1, 5, []Item{{Key: "e", Value: 5}, {Key: "d", Value: 4}, {Key: "c", Value: 3}, {Key: "b", Value: 2}, {Key: "a", Value: 1}}},
{2, 5, []Item{}},
{1, 2, []pageable.Item{{Index: "e", Value: 5}, {Index: "d", Value: 4}}},
{2, 2, []pageable.Item{{Index: "c", Value: 3}, {Index: "b", Value: 2}}},
{3, 2, []pageable.Item{{Index: "a", Value: 1}}},
{1, 3, []pageable.Item{{Index: "e", Value: 5}, {Index: "d", Value: 4}, {Index: "c", Value: 3}}},
{2, 3, []pageable.Item{{Index: "b", Value: 2}, {Index: "a", Value: 1}}},
{1, 5, []pageable.Item{{Index: "e", Value: 5}, {Index: "d", Value: 4}, {Index: "c", Value: 3}, {Index: "b", Value: 2}, {Index: "a", Value: 1}}},
{2, 5, []pageable.Item{}},
}

for _, tt := range tests {
Expand All @@ -74,7 +75,7 @@ func TestPager_GetPage(t *testing.T) {
uassert.Equal(t, len(tt.expected), len(page.Items))

for i, item := range page.Items {
uassert.Equal(t, tt.expected[i].Key, item.Key)
uassert.Equal(t, tt.expected[i].Index, item.Index)
uassert.Equal(t, tt.expected[i].Value, item.Value)
}
}
Expand Down
Loading
Loading