Skip to content

Commit

Permalink
Update from Hummingbird Project Template (#26)
Browse files Browse the repository at this point in the history
* Update from hummingbird-project-template 572d468b2cabeca286314c5a35196bd42445c8ef

* Run swift-format

* Remove .swiftformat

---------

Co-authored-by: adam-fowler <adam-fowler@users.noreply.github.com>
Co-authored-by: Adam Fowler <adamfowler71@gmail.com>
  • Loading branch information
3 people authored Nov 28, 2024
1 parent 947b17b commit 502ee6c
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 69 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ concurrency:

jobs:
validate:
runs-on: macOS-latest
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Install Dependencies
run: |
brew install mint
mint install NickLockwood/SwiftFormat@0.53.10 --no-link
- name: run script
run: ./scripts/validate.sh
63 changes: 63 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"version" : 1,
"indentation" : {
"spaces" : 4
},
"tabWidth" : 4,
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"spacesAroundRangeFormationOperators" : false,
"indentConditionalCompilationBlocks" : false,
"indentSwitchCaseLabels" : false,
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : true,
"lineBreakBeforeEachGenericRequirement" : true,
"lineLength" : 150,
"maximumBlankLines" : 1,
"respectsExistingLineBreaks" : true,
"prioritizeKeepingFunctionOutputTogether" : true,
"multiElementCollectionTrailingCommas" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : false,
"AlwaysUseLowerCamelCase" : false,
"AmbiguousTrailingClosureOverload" : true,
"BeginDocumentationCommentWithOneLineSummary" : false,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : false,
"NeverUseForceTry" : false,
"NeverUseImplicitlyUnwrappedOptionals" : false,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoAssignmentInExpressions" : true,
"NoBlockComments" : true,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : false,
"NoParensAroundConditions" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OmitExplicitReturns" : true,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"UseEarlyExits" : false,
"UseExplicitNilCheckInConditions" : false,
"UseLetInEveryBoundCaseVariable" : false,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : false,
"UseSynthesizedInitializer" : false,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : false
}
}
26 changes: 0 additions & 26 deletions .swiftformat

This file was deleted.

2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ The main development branch of the repository is `main`.

### Formatting

We use Nick Lockwood's SwiftFormat for formatting code. PRs will not be accepted if they haven't be formatted. The current version of SwiftFormat we are using is v0.53.10.
We use Apple's swift-format for formatting code. PRs will not be accepted if they haven't be formatted.
26 changes: 16 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ let package = Package(
name: "hummingbird-compression",
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)],
products: [
.library(name: "HummingbirdCompression", targets: ["HummingbirdCompression"]),
.library(name: "HummingbirdCompression", targets: ["HummingbirdCompression"])
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/adam-fowler/compress-nio.git", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.32.1"),
],
targets: [
.target(name: "HummingbirdCompression", dependencies: [
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "NIO", package: "swift-nio"),
.product(name: "CompressNIO", package: "compress-nio"),
]),
.testTarget(name: "HummingbirdCompressionTests", dependencies: [
.byName(name: "HummingbirdCompression"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]),
.target(
name: "HummingbirdCompression",
dependencies: [
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "NIO", package: "swift-nio"),
.product(name: "CompressNIO", package: "compress-nio"),
]
),
.testTarget(
name: "HummingbirdCompressionTests",
dependencies: [
.byName(name: "HummingbirdCompression"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ public struct RequestDecompressionMiddleware<Context: RequestContext>: RouterMid
public func handle(_ request: Request, context: Context, next: (Request, Context) async throws -> Response) async throws -> Response {
if let algorithm = algorithm(from: request.headers[values: .contentEncoding]) {
var request = request
request.body = .init(asyncSequence: DecompressByteBufferSequence(
base: request.body,
algorithm: algorithm,
windowSize: self.windowSize,
logger: context.logger
))
request.body = .init(
asyncSequence: DecompressByteBufferSequence(
base: request.body,
algorithm: algorithm,
windowSize: self.windowSize,
logger: context.logger
)
)
let response = try await next(request, context)
return response
} else {
Expand Down
22 changes: 16 additions & 6 deletions Tests/HummingbirdCompressionTests/CompressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HummingBirdCompressionTests: XCTestCase {
let router = Router()
router.middlewares.add(ResponseCompressionMiddleware())
router.post("/echo") { request, _ -> Response in
return .init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
.init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
}
let app = Application(router: router)
try await app.test(.router) { client in
Expand All @@ -50,7 +50,7 @@ class HummingBirdCompressionTests: XCTestCase {
let router = Router()
router.middlewares.add(ResponseCompressionMiddleware())
router.post("/echo") { request, _ -> Response in
return .init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
.init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
}
let app = Application(router: router)
let buffer = self.randomBuffer(size: 512_000)
Expand Down Expand Up @@ -86,7 +86,12 @@ class HummingBirdCompressionTests: XCTestCase {
group.addTask {
try await app.test(.router) { client in
let testBuffer = buffer.getSlice(at: Int.random(in: 0...256_000), length: Int.random(in: 0...256_000))
try await client.execute(uri: "/echo", method: .post, headers: [.acceptEncoding: "gzip"], body: testBuffer) { response in
try await client.execute(
uri: "/echo",
method: .post,
headers: [.acceptEncoding: "gzip"],
body: testBuffer
) { response in
var body = response.body
let uncompressed: ByteBuffer
if response.headers[.contentEncoding] == "gzip" {
Expand Down Expand Up @@ -162,7 +167,7 @@ class HummingBirdCompressionTests: XCTestCase {
router.middlewares.add(VerifyResponseBodyChunkSize(bufferSize: 256))
router.middlewares.add(ResponseCompressionMiddleware(windowSize: 256))
router.post("/echo") { request, _ -> Response in
return .init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
.init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
}
let app = Application(router: router)
try await app.test(.router) { client in
Expand Down Expand Up @@ -199,7 +204,7 @@ class HummingBirdCompressionTests: XCTestCase {
let router = Router()
router.middlewares.add(RequestDecompressionMiddleware())
router.post("/echo") { request, _ -> Response in
return .init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
.init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
}
let app = Application(router: router)
try await app.test(.router) { client in
Expand Down Expand Up @@ -257,7 +262,12 @@ class HummingBirdCompressionTests: XCTestCase {
group.addTask {
let testBuffer = buffer.getSlice(at: Int.random(in: 0...256_000), length: Int.random(in: 0...256_000))!
let compressedBuffer = try compress(testBuffer)
try await client.execute(uri: "/echo", method: .post, headers: [.contentEncoding: "gzip"], body: compressedBuffer) { response in
try await client.execute(
uri: "/echo",
method: .post,
headers: [.contentEncoding: "gzip"],
body: compressedBuffer
) { response in
XCTAssertEqual(response.body, testBuffer)
}
}
Expand Down
25 changes: 10 additions & 15 deletions scripts/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,16 @@ SWIFT_FORMAT_VERSION=0.53.10
set -eu
here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

which swiftformat > /dev/null 2>&1 || (echo "swiftformat not installed. You can install it using 'brew install swiftformat'" ; exit -1)

function replace_acceptable_years() {
# this needs to replace all acceptable forms with 'YEARS'
sed -e 's/20[12][0-9]-20[12][0-9]/YEARS/' -e 's/20[12][0-9]/YEARS/' -e '/^#!/ d'
}

printf "=> Checking format... "
FIRST_OUT="$(git status --porcelain)"
if [[ -n "${CI-""}" ]]; then
printf "(using v$(mint run NickLockwood/SwiftFormat@"$SWIFT_FORMAT_VERSION" --version)) "
mint run NickLockwood/SwiftFormat@"$SWIFT_FORMAT_VERSION" . > /dev/null 2>&1
else
printf "(using v$(swiftformat --version)) "
swiftformat . > /dev/null 2>&1
fi
git ls-files -z '*.swift' | xargs -0 swift format format --parallel --in-place
git diff --exit-code '*.swift'

SECOND_OUT="$(git status --porcelain)"
if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then
printf "\033[0;31mformatting issues!\033[0m\n"
Expand All @@ -55,29 +49,30 @@ if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then
else
printf "\033[0;32mokay.\033[0m\n"
fi
exit
printf "=> Checking license headers... "
tmp=$(mktemp /tmp/.soto-core-sanity_XXXXXX)

exit 0

for language in swift-or-c; do
declare -a matching_files
declare -a exceptions
expections=( )
matching_files=( -name '*' )
case "$language" in
swift-or-c)
exceptions=( -path '*Sources/INIParser/*' -o -path '*Sources/CSotoExpat/*' -o -path '*Benchmark/.build/*' -o -name Package.swift)
exceptions=( -path '*/Benchmarks/.build/*' -o -name Package.swift)
matching_files=( -name '*.swift' -o -name '*.c' -o -name '*.h' )
cat > "$tmp" <<"EOF"
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird open source project
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) YEARS the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Hummingbird authors
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -89,13 +84,13 @@ EOF
cat > "$tmp" <<"EOF"
##===----------------------------------------------------------------------===##
##
## This source file is part of the Hummingbird open source project
## This source file is part of the Hummingbird server framework project
##
## Copyright (c) YEARS the Hummingbird authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of Hummingbird authors
## See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
##
## SPDX-License-Identifier: Apache-2.0
##
Expand Down

0 comments on commit 502ee6c

Please sign in to comment.