Skip to content

Commit

Permalink
Run swift format
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Nov 23, 2024
1 parent 2b01006 commit 9867c0b
Show file tree
Hide file tree
Showing 83 changed files with 923 additions and 619 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
16 changes: 10 additions & 6 deletions Benchmarks/Benchmarks/Router/RouterBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ 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)
try await next(request, context)
}
}

Expand Down Expand Up @@ -170,12 +170,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
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
3 changes: 2 additions & 1 deletion Sources/Hummingbird/Codable/JSON/JSONCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
//
//===----------------------------------------------------------------------===//

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
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 @@ -55,7 +55,7 @@ public struct URLEncodedFormDecoder: Sendable {

/// The options set on the top-level encoder.
fileprivate var options: _Options {
return _Options(
_Options(
dateDecodingStrategy: self.dateDecodingStrategy,
userInfo: self.userInfo
)
Expand Down Expand Up @@ -99,7 +99,7 @@ private class _URLEncodedFormDecoder: Decoder {

/// Contextual user-provided information for use during encoding.
public var userInfo: [CodingUserInfoKey: Any] {
return self.options.userInfo
self.options.userInfo
}

// MARK: - Initialization
Expand All @@ -126,7 +126,7 @@ private class _URLEncodedFormDecoder: Decoder {
}

func singleValueContainer() throws -> SingleValueDecodingContainer {
return self
self
}

struct KDC<Key: CodingKey>: KeyedDecodingContainerProtocol {
Expand All @@ -143,97 +143,132 @@ private class _URLEncodedFormDecoder: Decoder {
}

func contains(_ key: Key) -> Bool {
return self.container.values[key.stringValue] != nil
self.container.values[key.stringValue] != nil
}

func decodeNil(forKey key: Key) throws -> Bool {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unboxNil(node)
}

func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: Bool.self)
}

func decode(_ type: String.Type, forKey key: Key) throws -> String {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: String.self)
}

func decode(_ type: Double.Type, forKey key: Key) throws -> Double {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: Double.self)
}

func decode(_ type: Float.Type, forKey key: Key) throws -> Float {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: Float.self)
}

func decode(_ type: Int.Type, forKey key: Key) throws -> Int {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: Int.self)
}

func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: Int8.self)
}

func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: Int16.self)
}

func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: Int32.self)
}

func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: Int64.self)
}

func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: UInt.self)
}

func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: UInt8.self)
}

func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: UInt16.self)
}

func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: UInt32.self)
}

func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 {
guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: UInt64.self)
}

func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T: Decodable {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
return try self.decoder.unbox(node, as: T.self)
}

func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey {
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey>
where NestedKey: CodingKey {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
guard case .map(let map) = node else {
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
}
Expand All @@ -245,7 +280,9 @@ private class _URLEncodedFormDecoder: Decoder {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let node = container.values[key.stringValue] else { throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: "")) }
guard let node = container.values[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
}
guard case .array(let array) = node else {
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
}
Expand Down Expand Up @@ -353,7 +390,8 @@ private class _URLEncodedFormDecoder: Decoder {
try self.decodeNextNode()
}

mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey {
mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey>
where NestedKey: CodingKey {
guard !self.isAtEnd else {
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Unkeyed container index out of range"))
}
Expand Down Expand Up @@ -605,13 +643,15 @@ extension _URLEncodedFormDecoder {
func unbox(_ node: URLEncodedFormNode, as type: Data.Type) throws -> Data {
let string = try unbox(node, as: String.self)
guard let data = Data(base64Encoded: string) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Data is not valid Base64."))
throw DecodingError.dataCorrupted(
DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Data is not valid Base64.")
)
}
return data
}

func unbox<T>(_ node: URLEncodedFormNode, as type: T.Type) throws -> T where T: Decodable {
return try self.unbox_(node, as: T.self) as! T
try self.unbox_(node, as: T.self) as! T
}

func unbox_(_ node: URLEncodedFormNode, as type: Decodable.Type) throws -> Any {
Expand All @@ -635,11 +675,11 @@ private struct URLEncodedFormDecodingStorage {
init() {}

/// return the container at the top of the storage
var topContainer: URLEncodedFormNode { return self.containers.last! }
var topContainer: URLEncodedFormNode { self.containers.last! }

/// push a new container onto the storage
mutating func push(container: URLEncodedFormNode) { self.containers.append(container) }

/// pop a container from the storage
@discardableResult mutating func popContainer() -> URLEncodedFormNode { return self.containers.removeLast() }
@discardableResult mutating func popContainer() -> URLEncodedFormNode { self.containers.removeLast() }
}
Loading

0 comments on commit 9867c0b

Please sign in to comment.