gitea源码

paginator.go 941B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package internal
  4. import (
  5. "math"
  6. "code.gitea.io/gitea/models/db"
  7. )
  8. // ParsePaginator parses a db.Paginator into a skip and limit
  9. func ParsePaginator(paginator *db.ListOptions, maxNums ...int) (int, int) {
  10. // Use a very large number to indicate no limit
  11. unlimited := math.MaxInt32
  12. if len(maxNums) > 0 {
  13. // Some indexer engines have a limit on the page size, respect that
  14. unlimited = maxNums[0]
  15. }
  16. if paginator == nil || paginator.IsListAll() {
  17. // It shouldn't happen. In actual usage scenarios, there should not be requests to search all.
  18. // But if it does happen, respect it and return "unlimited".
  19. // And it's also useful for testing.
  20. return 0, unlimited
  21. }
  22. if paginator.PageSize == 0 {
  23. // Do not return any results when searching, it's used to get the total count only.
  24. return 0, 0
  25. }
  26. return paginator.GetSkipTake()
  27. }