-
-
Notifications
You must be signed in to change notification settings - Fork 178
Add page size limit #715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add page size limit #715
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bef7ac7
Add support for per page limit to application and request
7787896
Refactor naming, add comments
87d4221
Extend tests
f8f7ab8
Fix test workflow
450bf5f
Implement PR feedback
dedb766
Adjust tests to non-throwing page limit handling
bdd0068
Introduce PageLimit type, improve tests
fc754e2
Implement PR feedback
55523ba
Rename maxPerPage to pageSizeLimit
89039b0
Add ability to trigger test action manually
siemensikkema a878536
Merge branch 'vapor:main' into feature/per-page-limit
siemensikkema 64f7aa0
Update test action and fine-tune dependency
siemensikkema File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,33 @@ | ||
| name: test | ||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: | ||
| - master | ||
| on: { pull_request: {} } | ||
|
|
||
| jobs: | ||
| linux: | ||
| getcidata: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| environments: ${{ steps.output.outputs.environments }} | ||
| steps: | ||
| - id: output | ||
| run: | | ||
| envblob="$(curl -fsSL https://raw.githubusercontent.com/vapor/ci/main/pr-environments.json | jq -cMj '.')" | ||
| echo "::set-output name=environments::${envblob}" | ||
|
|
||
| test-fluent: | ||
| needs: getcidata | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| image: | ||
| - swift:5.2-xenial | ||
| - swift:5.2-bionic | ||
| - swiftlang/swift:nightly-5.2-xenial | ||
| - swiftlang/swift:nightly-5.2-bionic | ||
| - swiftlang/swift:nightly-5.3-xenial | ||
| - swiftlang/swift:nightly-5.3-bionic | ||
| - swiftlang/swift:nightly-master-xenial | ||
| - swiftlang/swift:nightly-master-bionic | ||
| - swiftlang/swift:nightly-master-focal | ||
| - swiftlang/swift:nightly-master-centos8 | ||
| - swiftlang/swift:nightly-master-amazonlinux2 | ||
| container: ${{ matrix.image }} | ||
| env: | ||
| LOG_LEVEL: info | ||
| steps: | ||
| - name: Checkout Fluent | ||
| uses: actions/checkout@v2 | ||
| - name: Run base tests with Thread Sanitizer | ||
| run: swift test --enable-test-discovery --sanitize=thread | ||
| macOS: | ||
| env: | ||
| LOG_LEVEL: info | ||
| runs-on: macos-latest | ||
| steps: | ||
| - name: Select latest available Xcode | ||
| uses: maxim-lobanov/[email protected] | ||
| env: ${{ fromJSON(needs.getcidata.outputs.environments) }} | ||
| runs-on: ${{ matrix.env.os }} | ||
| container: ${{ matrix.env.image }} | ||
| steps: | ||
| - name: Select toolchain | ||
| uses: maxim-lobanov/[email protected] | ||
| with: | ||
| xcode-version: latest | ||
| - name: Checkout Fluent | ||
| xcode-version: ${{ matrix.env.toolchain }} | ||
| if: ${{ matrix.env.toolchain != '' }} | ||
| - name: Check out Vapor | ||
| uses: actions/checkout@v2 | ||
| - name: Run base Fluent tests with Thread Sanitizer | ||
| run: swift test --enable-test-discovery --sanitize=thread | ||
| - name: Run tests with Thread Sanitizer | ||
| timeout-minutes: 30 | ||
| run: swift test --enable-test-discovery --sanitize=thread |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import FluentKit | ||
| import Vapor | ||
|
|
||
| struct RequestPaginationKey: StorageKey { | ||
| typealias Value = RequestPagination | ||
| } | ||
|
|
||
| struct RequestPagination { | ||
| let pageSizeLimit: PageLimit? | ||
| } | ||
|
|
||
| struct AppPaginationKey: StorageKey { | ||
| typealias Value = AppPagination | ||
| } | ||
|
|
||
| struct AppPagination { | ||
| let pageSizeLimit: Int? | ||
| } | ||
|
|
||
| extension Request.Fluent { | ||
| public var pagination: Pagination { | ||
| .init(fluent: self) | ||
| } | ||
|
|
||
| public struct Pagination { | ||
| let fluent: Request.Fluent | ||
| } | ||
| } | ||
|
|
||
| extension Request.Fluent.Pagination { | ||
| /// The maximum amount of elements per page. The default is `nil`. | ||
| public var pageSizeLimit: PageLimit? { | ||
| get { | ||
| storage[RequestPaginationKey.self]?.pageSizeLimit | ||
| } | ||
| nonmutating set { | ||
| storage[RequestPaginationKey.self] = .init(pageSizeLimit: newValue) | ||
| } | ||
| } | ||
|
|
||
| var storage: Storage { | ||
| get { | ||
| self.fluent.request.storage | ||
| } | ||
| nonmutating set { | ||
| self.fluent.request.storage = newValue | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension Application.Fluent.Pagination { | ||
| /// The maximum amount of elements per page. The default is `nil`. | ||
| public var pageSizeLimit: Int? { | ||
| get { | ||
| storage[AppPaginationKey.self]?.pageSizeLimit | ||
| } | ||
| nonmutating set { | ||
| storage[AppPaginationKey.self] = .init(pageSizeLimit: newValue) | ||
| } | ||
| } | ||
|
|
||
| var storage: Storage { | ||
| get { | ||
| self.fluent.application.storage | ||
| } | ||
| nonmutating set { | ||
| self.fluent.application.storage = newValue | ||
| } | ||
| } | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import Foundation | ||
|
|
||
| public struct PageLimit { | ||
| public let value: Int? | ||
|
|
||
| public static var noLimit: PageLimit { | ||
| .init(value: nil) | ||
| } | ||
| } | ||
|
|
||
| extension PageLimit { | ||
| public init(_ value: Int) { | ||
| self.value = value | ||
| } | ||
| } | ||
|
|
||
| extension PageLimit: ExpressibleByIntegerLiteral { | ||
| public init(integerLiteral value: IntegerLiteralType) { | ||
| self.value = value | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gwynne do we need to wrap this in a lock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a global setting, set once during startup, I'd avoid diving into that too much.