Skip to content

Commit

Permalink
Formatted files
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Nov 23, 2024
1 parent f53d3bd commit 7386a7a
Show file tree
Hide file tree
Showing 110 changed files with 1,804 additions and 818 deletions.
8 changes: 4 additions & 4 deletions Benchmarks/Benchmarks/Router/Benchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import Hummingbird

let benchmarks = {
Benchmark.defaultConfiguration = .init(
metrics: ProcessInfo.processInfo.environment["CI"] != nil ?
[
metrics: ProcessInfo.processInfo.environment["CI"] != nil
? [
.instructions,
.mallocCountTotal,
] :
[
]
: [
.cpuTotal,
.instructions,
.mallocCountTotal,
Expand Down
28 changes: 20 additions & 8 deletions Benchmarks/Benchmarks/Router/RouterBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ extension Benchmark {
request: HTTPRequest,
writeBody: (@Sendable (ByteBufferWriter) async throws -> Void)? = nil,
createRouter: @escaping @Sendable () async throws -> ResponderBuilder
) where ResponderBuilder.Responder.Context: RequestContext, ResponderBuilder.Responder.Context.Source == BenchmarkRequestContextSource {
)
where
ResponderBuilder.Responder.Context: RequestContext,
ResponderBuilder.Responder.Context.Source == BenchmarkRequestContextSource
{
self.init(name, configuration: configuration) { benchmark in
let responder = try await createRouter().buildResponder()

Expand Down Expand Up @@ -105,8 +109,12 @@ extension Benchmark {
}

struct EmptyMiddleware<Context>: RouterMiddleware {
func handle(_ request: Request, context: Context, next: (Request, Context) async throws -> Response) async throws -> Response {
return try await next(request, context)
func handle(
_ request: Request,
context: Context,
next: (Request, Context) async throws -> Response
) async throws -> Response {
try await next(request, context)
}
}

Expand Down Expand Up @@ -170,12 +178,16 @@ func routerBenchmarks() {
} createRouter: {
let router = Router(context: BasicBenchmarkContext.self)
router.post { request, _ in
Response(status: .ok, headers: [:], body: .init { writer in
for try await buffer in request.body {
try await writer.write(buffer)
Response(
status: .ok,
headers: [:],
body: .init { writer in
for try await buffer in request.body {
try await writer.write(buffer)
}
try await writer.finish(nil)
}
try await writer.finish(nil)
})
)
}
return router
}
Expand Down
5 changes: 4 additions & 1 deletion Benchmarks/Benchmarks/Router/TrieRouterBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func trieRouterBenchmarks() {
}
} setup: {
let trieBuilder = RouterPathTrieBuilder<String>()
trieBuilder.addEntry("/api/v1/a/very/long/path/with/lots/of/segments", value: "/api/v1/a/very/long/path/with/lots/of/segments")
trieBuilder.addEntry(
"/api/v1/a/very/long/path/with/lots/of/segments",
value: "/api/v1/a/very/long/path/with/lots/of/segments"
)
trieBuilder.addEntry("/api/v1/users/:id/profile", value: "/api/v1/users/:id/profile")
trie3 = RouterTrie(base: trieBuilder)
}
Expand Down
4 changes: 2 additions & 2 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ let package = Package(
],
path: "Benchmarks/Router",
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark"),
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
]
),
)
]
)
34 changes: 19 additions & 15 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,26 @@ let package = Package(
swiftSettings: [.enableExperimentalFeature("StrictConcurrency=complete")]
),
// test targets
.testTarget(name: "HummingbirdTests", dependencies: [
.byName(name: "Hummingbird"),
.byName(name: "HummingbirdTLS"),
.byName(name: "HummingbirdHTTP2"),
.byName(name: "HummingbirdTesting"),
.byName(name: "HummingbirdRouter"),
]),
.testTarget(name: "HummingbirdRouterTests", dependencies: [
.byName(name: "HummingbirdRouter"),
.byName(name: "HummingbirdTesting"),
]),
.testTarget(
name: "HummingbirdTests",
dependencies: [
.byName(name: "Hummingbird"),
.byName(name: "HummingbirdTLS"),
.byName(name: "HummingbirdHTTP2"),
.byName(name: "HummingbirdTesting"),
.byName(name: "HummingbirdRouter"),
]
),
.testTarget(
name: "HummingbirdRouterTests",
dependencies: [
.byName(name: "HummingbirdRouter"),
.byName(name: "HummingbirdTesting"),
]
),
.testTarget(
name: "HummingbirdCoreTests",
dependencies:
[
dependencies: [
.byName(name: "HummingbirdCore"),
.byName(name: "HummingbirdTLS"),
.byName(name: "HummingbirdTesting"),
Expand All @@ -147,8 +152,7 @@ let package = Package(
),
.testTarget(
name: "HummingbirdHTTP2Tests",
dependencies:
[
dependencies: [
.byName(name: "HummingbirdCore"),
.byName(name: "HummingbirdHTTP2"),
.byName(name: "HummingbirdTesting"),
Expand Down
5 changes: 3 additions & 2 deletions Sources/Hummingbird/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ extension ApplicationProtocol {
/// try await app.runService()
/// ```
/// Editing the application setup after calling `runService` will produce undefined behaviour.
public struct Application<Responder: HTTPResponder>: ApplicationProtocol where Responder.Context: InitializableFromSource<ApplicationRequestContextSource> {
public struct Application<Responder: HTTPResponder>: ApplicationProtocol
where Responder.Context: InitializableFromSource<ApplicationRequestContextSource> {
// MARK: Member variables

/// event loop group used by application
Expand Down Expand Up @@ -280,7 +281,7 @@ public struct Application<Responder: HTTPResponder>: ApplicationProtocol where R
}

public func buildResponder() async throws -> Responder {
return self.responder
self.responder
}

public func onServerRunning(_ channel: Channel) async {
Expand Down
12 changes: 9 additions & 3 deletions Sources/Hummingbird/Codable/JSON/JSONCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
//
//===----------------------------------------------------------------------===//

import NIOFoundationCompat

import struct Foundation.Date
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder
import NIOFoundationCompat

extension JSONEncoder: ResponseEncoder {
/// Extend JSONEncoder to support encoding `Response`'s. Sets body and header values
/// - Parameters:
/// - value: Value to encode
/// - request: Request used to generate response
/// - context: Request context
public func encode(_ value: some Encodable, from request: Request, context: some RequestContext) throws -> Response {
public func encode(_ value: some Encodable, from request: Request, context: some RequestContext) throws -> Response
{
let data = try self.encode(value)
let buffer = ByteBuffer(data: data)
return Response(
Expand All @@ -43,7 +45,11 @@ extension JSONDecoder: RequestDecoder {
/// - type: Type to decode
/// - request: Request to decode from
/// - context: Request context
public func decode<T: Decodable>(_ type: T.Type, from request: Request, context: some RequestContext) async throws -> T {
public func decode<T: Decodable>(
_ type: T.Type,
from request: Request,
context: some RequestContext
) async throws -> T {
let buffer = try await request.body.collect(upTo: context.maxUploadSize)
return try self.decode(T.self, from: buffer)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Hummingbird/Codable/ResponseEncodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public protocol ResponseCodable: ResponseEncodable, Decodable {}
/// Extend ResponseEncodable to conform to ResponseGenerator
extension ResponseEncodable {
public func response(from request: Request, context: some RequestContext) throws -> Response {
return try context.responseEncoder.encode(self, from: request, context: context)
try context.responseEncoder.encode(self, from: request, context: context)
}
}

Expand All @@ -34,7 +34,7 @@ extension Array: ResponseGenerator where Element: Encodable {}
/// Extend Array to conform to ResponseEncodable
extension Array: ResponseEncodable where Element: Encodable {
public func response(from request: Request, context: some RequestContext) throws -> Response {
return try context.responseEncoder.encode(self, from: request, context: context)
try context.responseEncoder.encode(self, from: request, context: context)
}
}

Expand All @@ -44,6 +44,6 @@ extension Dictionary: ResponseGenerator where Key: Encodable, Value: Encodable {
/// Extend Array to conform to ResponseEncodable
extension Dictionary: ResponseEncodable where Key: Encodable, Value: Encodable {
public func response(from request: Request, context: some RequestContext) throws -> Response {
return try context.responseEncoder.encode(self, from: request, context: context)
try context.responseEncoder.encode(self, from: request, context: context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ extension URLEncodedFormEncoder: ResponseEncoder {
/// - value: Value to encode
/// - request: Request used to generate response
/// - context: Request context
public func encode(_ value: some Encodable, from request: Request, context: some RequestContext) throws -> Response {
public func encode(_ value: some Encodable, from request: Request, context: some RequestContext) throws -> Response
{
let string = try self.encode(value)
let buffer = ByteBuffer(string: string)
return Response(
Expand All @@ -38,7 +39,11 @@ extension URLEncodedFormDecoder: RequestDecoder {
/// - type: Type to decode
/// - request: Request to decode from
/// - context: Request context
public func decode<T: Decodable>(_ type: T.Type, from request: Request, context: some RequestContext) async throws -> T {
public func decode<T: Decodable>(
_ type: T.Type,
from request: Request,
context: some RequestContext
) async throws -> T {
let buffer = try await request.body.collect(upTo: context.maxUploadSize)
let string = String(buffer: buffer)
return try self.decode(T.self, from: string)
Expand Down
Loading

0 comments on commit 7386a7a

Please sign in to comment.