Add pagination utilities and move Hooks to contract#21
Draft
ConsoleTVs wants to merge 8 commits into
Draft
Conversation
Add offset-based and cursor-based pagination support: - Page[T] struct with NewPage constructor (computes LastPage, clamps inputs) - Cursor[T] struct with NewCursor (built-in base64-JSON encoding) and NewCursorWith (custom CursorEncoder interface) - CursorValue/CursorValueWith for decoding cursor strings - Request helpers: Pagination, PaginationWith, CursorPagination, CursorPaginationWith (query param parsing with defaults and clamping) - CursorEncoder mock for testing 100% test coverage on all new code.
Move the Hooks implementation from framework to contract as a concrete struct, mirroring the Session pattern. Only one meaningful implementation exists, so the interface indirection added no value. - contract/hooks.go: interface replaced with mutex-protected struct - contract/request/hooks.go: returns *contract.Hooks instead of interface - framework/hooks_writer.go: embeds *contract.Hooks - framework/handler.go: uses contract.NewHooks() - Deleted contract/mock/hooks.go (use contract.NewHooks() in tests) - Deleted framework/hooks.go and framework/hooks_test.go (moved to contract) BREAKING CHANGE: contract.Hooks is now a concrete *Hooks struct. Type assertions change from .(contract.Hooks) to .(*contract.Hooks). request.Hooks() and request.TryHooks() now return *contract.Hooks.
Replace CursorExtractor interface and CursorExtractorFunc with a plain encode func(T) (string, error) parameter on NewCursor. Remove CursorEncoder interface, NewCursorWith, CursorValue, and CursorValueWith. New API: - NewCursor[T](items, perPage, hasNext, hasPrev, encode func(T) (string, error)) - MarshalCursor[V](value) — base64-JSON encode helper - UnmarshalCursor[V](cursor) — base64-JSON decode helper The encode function gives full control at the call site without requiring methods on domain types or hidden encoding behavior.
Add usage examples in doc comments for Paginate, CursorPaginate, CursorEncode, and CursorDecode. Rename functions from NewPage/NewCursor to Paginate/CursorPaginate and MarshalCursor/UnmarshalCursor to CursorEncode/CursorDecode for better API clarity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Pagination
Page[T]struct andNewPageconstructor for offset-based pagination (computesLastPage, clamps inputs)Cursor[T]struct withNewCursor(built-in base64-JSON) andNewCursorWith(customCursorEncoder) for cursor-based paginationCursorValueandCursorValueWithfor decoding cursor stringsPagination,PaginationWith,CursorPagination,CursorPaginationWithCursorEncoderMockfor testingHooks refactor
Hooksfrom interface (contract) + struct (framework) to concrete struct in contractSessionpattern — single meaningful implementation, no driver neededcontract/mock/hooks.go(no longer needed — usecontract.NewHooks()in tests)request.Hooks/request.TryHooksto return*contract.Hooksframework/hooks_writer.gonow embeds*contract.HooksBreaking Changes
contract.Hooksis now a struct, not an interfaceWhat changed:
Hooksis a concrete*contract.Hookseverywhere instead of thecontract.Hooksinterface.Migration:
Hooksinterface implementations withcontract.NewHooks().(contract.Hooks)→.(*contract.Hooks)request.Hooks(r)andrequest.TryHooks(r)now return*contract.HooksWhy: Only one meaningful implementation exists. Matches the
Sessionpattern.