11package page
22
33import (
4+ "github.com/voidint/box/constraints"
45 "github.com/voidint/box/db"
5- "golang.org/x/exp/constraints"
66)
77
8- // Integer 整型类型约束
9- type Integer interface {
10- constraints.Integer
11- }
12-
138// Page 分页结构定义
14- type Page [T any , INT Integer ] struct {
9+ type Page [T any , INT constraints. Unsigned ] struct {
1510 PageNo INT `json:"pageNo"` // 若序列化时想要使用其他的命名风格,建议使用 github.com/json-iterator/go 库中的 extra.SetNamingStrategy() 函数注册自定义命名策略。
1611 PageSize INT `json:"pageSize"`
1712 TotalPages INT `json:"totalPages"`
@@ -20,14 +15,14 @@ type Page[T any, INT Integer] struct {
2015}
2116
2217// Pager 分页接口
23- type Pager [T any , INT Integer ] interface {
18+ type Pager [T any , INT constraints. Unsigned ] interface {
2419 AddRecords (records ... T )
2520 BuildDBPage () * db.Page [INT ]
2621 BuildPage () * Page [T , INT ]
2722}
2823
2924// NewPager 构建一个分页对象
30- func NewPager [T any , INT Integer ](pageNo , pageSize , totalRecords INT ) Pager [T , INT ] {
25+ func NewPager [T any , INT constraints. Unsigned ](pageNo , pageSize , totalRecords INT ) Pager [T , INT ] {
3126 if pageNo <= 0 {
3227 pageNo = 1
3328 }
@@ -52,7 +47,7 @@ func NewPager[T any, INT Integer](pageNo, pageSize, totalRecords INT) Pager[T, I
5247}
5348
5449// pagerImpl 实际的分页对象
55- type pagerImpl [T any , INT Integer ] struct {
50+ type pagerImpl [T any , INT constraints. Unsigned ] struct {
5651 pageNo INT
5752 pageSize INT
5853 totalPages INT
@@ -82,7 +77,7 @@ func (p *pagerImpl[T, INT]) BuildPage() *Page[T, INT] {
8277}
8378
8479// EmptyPage 返回空分页
85- func EmptyPage [T any , INT Integer ](pageNo , pageSize INT ) * Page [T , INT ] {
80+ func EmptyPage [T any , INT constraints. Unsigned ](pageNo , pageSize INT ) * Page [T , INT ] {
8681 return & Page [T , INT ]{
8782 PageNo : pageNo ,
8883 PageSize : pageSize ,
@@ -93,7 +88,7 @@ func EmptyPage[T any, INT Integer](pageNo, pageSize INT) *Page[T, INT] {
9388}
9489
9590// mustCalculateTotalPages 计算总分页数
96- func mustCalculateTotalPages [INT Integer ](pageSize , totalRecords INT ) (totalPages INT ) {
91+ func mustCalculateTotalPages [INT constraints. Unsigned ](pageSize , totalRecords INT ) (totalPages INT ) {
9792 if pageSize <= 0 {
9893 panic ("page size should be positive integer" )
9994 }
0 commit comments