paging.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package api
  5. import "github.com/influxdata/influxdb-client-go/v2/domain"
  6. // PagingOption is the function type for applying paging option
  7. type PagingOption func(p *Paging)
  8. // Paging holds pagination parameters for various Get* functions of InfluxDB 2 API
  9. // Not the all options are usable for some Get* functions
  10. type Paging struct {
  11. // Starting offset for returning items
  12. // Default 0.
  13. offset domain.Offset
  14. // Maximum number of items returned.
  15. // Default 0 - not applied
  16. limit domain.Limit
  17. // What field should be used for sorting
  18. sortBy string
  19. // Changes sorting direction
  20. descending domain.Descending
  21. // The last resource ID from which to seek from (but not including).
  22. // This is to be used instead of `offset`.
  23. after domain.After
  24. }
  25. // defaultPagingOptions returns default paging options: offset 0, limit 0 (not applied), default sorting, ascending
  26. func defaultPaging() *Paging {
  27. return &Paging{limit: 0, offset: 0, sortBy: "", descending: false, after: ""}
  28. }
  29. // PagingWithLimit sets limit option - maximum number of items returned.
  30. func PagingWithLimit(limit int) PagingOption {
  31. return func(p *Paging) {
  32. p.limit = domain.Limit(limit)
  33. }
  34. }
  35. // PagingWithOffset set starting offset for returning items. Default 0.
  36. func PagingWithOffset(offset int) PagingOption {
  37. return func(p *Paging) {
  38. p.offset = domain.Offset(offset)
  39. }
  40. }
  41. // PagingWithSortBy sets field name which should be used for sorting
  42. func PagingWithSortBy(sortBy string) PagingOption {
  43. return func(p *Paging) {
  44. p.sortBy = sortBy
  45. }
  46. }
  47. // PagingWithDescending changes sorting direction
  48. func PagingWithDescending(descending bool) PagingOption {
  49. return func(p *Paging) {
  50. p.descending = domain.Descending(descending)
  51. }
  52. }
  53. // PagingWithAfter set after option - the last resource ID from which to seek from (but not including).
  54. // This is to be used instead of `offset`.
  55. func PagingWithAfter(after string) PagingOption {
  56. return func(p *Paging) {
  57. p.after = domain.After(after)
  58. }
  59. }