mirror of
https://github.com/hanzo-fi/go-libs.git
synced 2026-10-30 20:53:44 +00:00
* refactor: move all packages under pkg/ for v5 Move all 13 top-level Go packages into pkg/ for a cleaner repository structure. All internal import paths updated from github.com/formancehq/go-libs/v5/<pkg> to github.com/formancehq/go-libs/v5/pkg/<pkg>. Also includes cleanup: - Fix .golangci.yml stale v4 CircuitBreaker path - Replace deprecated str.Contains with stdlib slices.Contains - Update TODO(v4) markers to TODO - Delete stale coverage.txt - Fix pgtesting timeout not being propagated to docker pool - Update docs/ARCHITECTURE.md to reflect new structure Co-Authored-By: Claude Opus 4.6 <[email protected]> * chore: remove compatibility aliases, unused params, and stub files - Remove unused type aliases (Authenticator, KeySet) from authnfx - Remove unused `debug` parameter from S3ModuleFromFlags - Delete comment-only stub files in messaging/publish/circuit/ Co-Authored-By: Claude Opus 4.6 <[email protected]> * chore: rename cli.go to flags.go for consistency Standardize naming across all packages: cli.go -> flags.go since these files all serve the same purpose (flag definitions). Co-Authored-By: Claude Opus 4.6 <[email protected]> * chore: rename * chore(deps): update all dependencies Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: resolve package name mismatches in generated mocks Generated mock files had stale package names from before the pkg/ move, causing import cycles and build failures with --build-tags it. Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.6 <[email protected]>
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package paginate
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
func usingOffset[Q any, T any](ctx context.Context, sb *bun.SelectQuery, query OffsetPaginatedQuery[Q], builders ...func(query *bun.SelectQuery) *bun.SelectQuery) (*Cursor[T], error) {
|
|
|
|
ret := make([]T, 0)
|
|
|
|
sb.Model(&ret)
|
|
for _, builder := range builders {
|
|
sb = sb.Apply(builder)
|
|
}
|
|
|
|
if query.Offset > 0 {
|
|
sb = sb.Offset(int(query.Offset))
|
|
}
|
|
|
|
if query.PageSize > 0 {
|
|
sb = sb.Limit(int(query.PageSize) + 1)
|
|
}
|
|
|
|
if err := sb.Scan(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var previous, next *OffsetPaginatedQuery[Q]
|
|
|
|
// Page with transactions before
|
|
if query.Offset > 0 {
|
|
cp := query
|
|
offset := int(query.Offset) - int(query.PageSize)
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
cp.Offset = uint64(offset)
|
|
previous = &cp
|
|
}
|
|
|
|
// Page with transactions after
|
|
if query.PageSize != 0 && len(ret) > int(query.PageSize) {
|
|
cp := query
|
|
cp.Offset = query.Offset + query.PageSize
|
|
next = &cp
|
|
ret = ret[:len(ret)-1]
|
|
}
|
|
|
|
return &Cursor[T]{
|
|
PageSize: int(query.PageSize),
|
|
HasMore: next != nil,
|
|
Previous: previous.EncodeAsCursor(),
|
|
Next: next.EncodeAsCursor(),
|
|
Data: ret,
|
|
}, nil
|
|
}
|
|
|
|
func UsingOffset[Q any, T any](ctx context.Context, sb *bun.SelectQuery, query OffsetPaginatedQuery[Q], builders ...func(query *bun.SelectQuery) *bun.SelectQuery) (*Cursor[T], error) {
|
|
return usingOffset[Q, T](ctx, sb, query, builders...)
|
|
}
|