|
| 1 | +package zendesk |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | +) |
| 6 | + |
| 7 | +// PaginationOptions struct represents general pagination options. |
| 8 | +// PageSize specifies the number of items per page, IsCBP indicates if it's cursor-based pagination, |
| 9 | +// SortBy and SortOrder describe how to sort the items in Offset Based Pagination, and Sort describes how to sort items in Cursor Based Pagination. |
| 10 | +type PaginationOptions struct { |
| 11 | + CommonOptions |
| 12 | + PageSize int //default is 100 |
| 13 | + IsCBP bool //default is true |
| 14 | +} |
| 15 | + |
| 16 | +// NewPaginationOptions() returns a pointer to a new PaginationOptions struct with default values (PageSize is 100, IsCBP is true). |
| 17 | +func NewPaginationOptions() *PaginationOptions { |
| 18 | + return &PaginationOptions{ |
| 19 | + PageSize: 100, |
| 20 | + IsCBP: true, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +type CommonOptions struct { |
| 25 | + Active bool `url:"active,omitempty"` |
| 26 | + Role string `url:"role,omitempty"` |
| 27 | + Roles []string `url:"role[],omitempty"` |
| 28 | + PermissionSet int64 `url:"permission_set,omitempty"` |
| 29 | + |
| 30 | + // SortBy can take "assignee", "assignee.name", "created_at", "group", "id", |
| 31 | + // "locale", "requester", "requester.name", "status", "subject", "updated_at" |
| 32 | + SortBy string `url:"sort_by,omitempty"` |
| 33 | + |
| 34 | + // SortOrder can take "asc" or "desc" |
| 35 | + SortOrder string `url:"sort_order,omitempty"` |
| 36 | + Sort string `url:"sort,omitempty"` |
| 37 | + Id int64 |
| 38 | +} |
| 39 | + |
| 40 | +// CBPOptions struct is used to specify options for listing objects in CBP (Cursor Based Pagination). |
| 41 | +// It embeds the CursorPagination struct for pagination and provides an option Sort for sorting the result. |
| 42 | +type CBPOptions struct { |
| 43 | + CursorPagination |
| 44 | + CommonOptions |
| 45 | +} |
| 46 | + |
| 47 | +// OBPOptions struct is used to specify options for listing objects in OBP (Offset Based Pagination). |
| 48 | +// It embeds the PageOptions struct for pagination and provides options for sorting the result; |
| 49 | +// SortBy specifies the field to sort by, and SortOrder specifies the order (either 'asc' or 'desc'). |
| 50 | +type OBPOptions struct { |
| 51 | + PageOptions |
| 52 | + CommonOptions |
| 53 | +} |
| 54 | + |
| 55 | +// ObpFunc defines the signature of the function used to list objects in OBP. |
| 56 | +type ObpFunc[T any] func(ctx context.Context, opts *OBPOptions) ([]T, Page, error) |
| 57 | + |
| 58 | +// CbpFunc defines the signature of the function used to list objects in CBP. |
| 59 | +type CbpFunc[T any] func(ctx context.Context, opts *CBPOptions) ([]T, CursorPaginationMeta, error) |
| 60 | + |
| 61 | +// terator struct provides a convenient and genric way to iterate over pages of objects in either OBP or CBP. |
| 62 | +// It holds state for iteration, including the current page size, a flag indicating more pages, pagination type (OBP or CBP), and sorting options. |
| 63 | +type Iterator[T any] struct { |
| 64 | + CommonOptions |
| 65 | + // generic fields |
| 66 | + pageSize int |
| 67 | + hasMore bool |
| 68 | + isCBP bool |
| 69 | + |
| 70 | + // OBP fields |
| 71 | + pageIndex int |
| 72 | + |
| 73 | + // CBP fields |
| 74 | + pageAfter string |
| 75 | + |
| 76 | + // common fields |
| 77 | + ctx context.Context |
| 78 | + obpFunc ObpFunc[T] |
| 79 | + cbpFunc CbpFunc[T] |
| 80 | +} |
| 81 | + |
| 82 | +// HasMore() returns a boolean indicating whether more pages are available for iteration. |
| 83 | +func (i *Iterator[T]) HasMore() bool { |
| 84 | + return i.hasMore |
| 85 | +} |
| 86 | + |
| 87 | +// GetNext() retrieves the next batch of objects according to the current pagination and sorting options. |
| 88 | +// It updates the state of the iterator for subsequent calls. |
| 89 | +// In case of an error, it sets hasMore to false and returns an error. |
| 90 | +func (i *Iterator[T]) GetNext() ([]T, error) { |
| 91 | + if !i.isCBP { |
| 92 | + obpOps := &OBPOptions{ |
| 93 | + PageOptions: PageOptions{ |
| 94 | + PerPage: i.pageSize, |
| 95 | + Page: i.pageIndex, |
| 96 | + }, |
| 97 | + CommonOptions: i.CommonOptions, |
| 98 | + } |
| 99 | + results, page, err := i.obpFunc(i.ctx, obpOps) |
| 100 | + if err != nil { |
| 101 | + i.hasMore = false |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + i.hasMore = page.HasNext() |
| 105 | + i.pageIndex++ |
| 106 | + return results, nil |
| 107 | + } |
| 108 | + |
| 109 | + cbpOps := &CBPOptions{ |
| 110 | + CursorPagination: CursorPagination{ |
| 111 | + PageSize: i.pageSize, |
| 112 | + PageAfter: i.pageAfter, |
| 113 | + }, |
| 114 | + CommonOptions: i.CommonOptions, |
| 115 | + } |
| 116 | + results, meta, err := i.cbpFunc(i.ctx, cbpOps) |
| 117 | + if err != nil { |
| 118 | + i.hasMore = false |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + i.hasMore = meta.HasMore |
| 122 | + i.pageAfter = meta.AfterCursor |
| 123 | + return results, nil |
| 124 | +} |
0 commit comments