You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

paginate.go 727B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import "reflect"
  5. // PaginateSlice cut a slice as per pagination options
  6. // if page = 0 it do not paginate
  7. func PaginateSlice(list any, page, pageSize int) any {
  8. if page <= 0 || pageSize <= 0 {
  9. return list
  10. }
  11. if reflect.TypeOf(list).Kind() != reflect.Slice {
  12. return list
  13. }
  14. listValue := reflect.ValueOf(list)
  15. page--
  16. if page*pageSize >= listValue.Len() {
  17. return listValue.Slice(listValue.Len(), listValue.Len()).Interface()
  18. }
  19. listValue = listValue.Slice(page*pageSize, listValue.Len())
  20. if listValue.Len() > pageSize {
  21. return listValue.Slice(0, pageSize).Interface()
  22. }
  23. return listValue.Interface()
  24. }