{"id":23159164,"url":"https://github.com/uptutu/pagination-go","last_synced_at":"2025-04-04T18:42:06.385Z","repository":{"id":110611043,"uuid":"486065351","full_name":"uptutu/pagination-go","owner":"uptutu","description":"A unified and convenient paginated request-response processing tool. | 统一方便的分页请求和响应处理工具。","archived":false,"fork":false,"pushed_at":"2022-05-07T15:45:28.000Z","size":18,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T03:47:56.450Z","etag":null,"topics":["pagination","paginator"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uptutu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-27T06:00:14.000Z","updated_at":"2022-07-13T07:41:24.000Z","dependencies_parsed_at":"2023-07-22T22:15:54.089Z","dependency_job_id":null,"html_url":"https://github.com/uptutu/pagination-go","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptutu%2Fpagination-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptutu%2Fpagination-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptutu%2Fpagination-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptutu%2Fpagination-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uptutu","download_url":"https://codeload.github.com/uptutu/pagination-go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234844,"owners_count":20905852,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pagination","paginator"],"created_at":"2024-12-17T22:32:19.672Z","updated_at":"2025-04-04T18:42:06.354Z","avatar_url":"https://github.com/uptutu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pagination\n\n简单的分页工具，只要结构体符合下面的要求，就可以使用该工具解析并填充。\n\n1. 被解析的数据必须是结构体\n    1. 结构体必须含有可导出的:\n       1. int 类型： `PageNum` or `Num`\n       2. int 类型： `PageSize` or `Size`\n       3. string 类型： `OrderBy`\n       4. bool 类型 `IsDescending` or `Descending` \n       5. string 类型: `Qeury` or `SearchKey` 字段\n    \n       比如说这样\n       ```go\n        type testRequest struct {\n             PageNum      int\n             PageSize     int\n             OrderBy      string\n             IsDescending bool\n             KeyWords     string\n             SearchKey    string\n             CustomField  string\n       }\n       ```\n    2. 这些必须字段可以成为一个新的结构体被名在 `Page` 或 `Pagination` 的字段下\n    比如说这样：\n      ```go\n      type SearchDialogCasesRequest struct {\n\t          Page *PaginationRequest\n      }\n   \n      // pagination.pb.go\n       type PaginationRequest struct {\n\t          state         protoimpl.MessageState\n\t          sizeCache     protoimpl.SizeCache\n\t          unknownFields protoimpl.UnknownFields\n\t\t\t  \n              PageNum      int64  `protobuf:\"varint,1,opt,name=page_num,json=pageNum,proto3\" json:\"page_num,omitempty\"`\n\t          PageSize     int64  `protobuf:\"varint,2,opt,name=page_size,json=pageSize,proto3\" json:\"page_size,omitempty\"`\n\t          OrderBy      string `protobuf:\"bytes,3,opt,name=order_by,json=orderBy,proto3\" json:\"order_by,omitempty\"`\n\t          IsDescending bool   `protobuf:\"varint,4,opt,name=is_descending,json=isDescending,proto3\" json:\"is_descending,omitempty\"`\n\t          SearchKey    string `protobuf:\"bytes,5,opt,name=search_key,json=searchKey,proto3\" json:\"search_key,omitempty\"`\n     }\n      ```\n3. 被导出的数据必须是结构体\n    1. 被导出的结构体中必须含有 `PageNum`， `PageSize` 和 `Total` 字段\n\n## Usage example\n\n```go\nimport \"github.com/uptutu/pagination\"\n\n// 自定义请求结构体\ntype ListSubscribeEntitiesRequest {\n   PageNum      int64\n   PageSize     int64\n   OrderBy      string\n   IsDescending bool\n   Query    string\n   MyData       interface{}\n}\n\ntype ListSubscribeEntitiesResponse {\n   Total    int64\n   PageNum  int64\n   LastPage int64\n   PageSize int64\n   // 自己的数据\n   Data     string\n}\n\nfunc (s *SubscribeService) ListSubscribeEntities(ctx context.Context, req *pb.ListSubscribeEntitiesRequest) (*pb.ListSubscribeEntitiesResponse, error) {\n   page, err := pagination.Parse(req)\n   \n   //输出类似于 {Num:10 Size:50 OrderBy:test IsDescending:true SearchKey:\"search\"}\n   fmt.Println(page)\n   \n   // returned Bool 判读是否需要分页\n   // return page_num \u003e 0 \u0026\u0026 page_size \u003e 0 \n   page.Required()\n   \n   // 返回限制个数（page_size）\n   page.Limit()\n   \n   // 返回分页请求下标（page_num-1 * page_size）\n   page.Offset()\n   \n   // 是否倒序\n   page.IsDescending\n   \n   // 搜索关键字\n   page.Query\n   \n   // 排序字段\n   page.OrderBy\n   \n   // 获取总数\n   count := db.Find(data).Count()\n   \n   // 请务必手动填充总数\n   page.SetTotal(count)\n   \n   resp := \u0026ListSubscribeEntitiesResponse{}\n   resp.Data = \"一些数据\"\n   \n   // 此后 resp 的分页信息将会自动被填充\n   page.FillResponse(resp)\n}\n\n```\n\n## func Required\n\nUsed to determine if the paging request passed meets the paging needs\n\nJudgement conditions：\n\n```go\nfunc (p Page) Required() bool {\n    return p.Num \u003e 0 \u0026\u0026 p.Size \u003e 0\n}\n```\n\n## func Limit\n\nif no limit set this will return the default value.\n\n```go\nfunc (p Page) Limit() int32 {\n   if p.Size != 0 {\n    return uint32(p.Size)\n   }\n   \n   return uint32(p.defaultSize)\n}\n```\n\n## func Offset\n\ncount the offset of the current page\n\n```go\nfunc (p Page) Offset() int32 {\n   if p.Num \u003c= 0 {\n    return 0\n   }\n   return uint32((p.Num - 1) * p.Size)\n}\n```\n\n## func FillResponse\n\nAutomatic padding of paginated data to match paginated responsive design.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuptutu%2Fpagination-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuptutu%2Fpagination-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuptutu%2Fpagination-go/lists"}