-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add additional result builder capabilities. (#584)
* Add additional result builder capabilities. - Adds support for conditional if-else statement syntax in result builders - Adds support for conditional if statement syntax in result builders - Adds support for for loop statement syntax in result builders - Adds support for optional unwrapping statement syntax in result builders. - Adds tests for validating the additional supported syntax. - Runs script for updating contributors. * Consolidate Conditional/Empty into Optional Middleware. * Hide result builder helper types from documentation. --------- Co-authored-by: Adam Fowler <[email protected]>
- Loading branch information
1 parent
dd3d571
commit ac00a22
Showing
6 changed files
with
298 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -16,8 +16,10 @@ needs to be listed here. | |
- Brian Michel <[email protected]> | ||
- Callum Todd <[email protected]> | ||
- Chris <[email protected]> | ||
- Connor Ricks <[email protected]> | ||
- Derek Clarkson <[email protected]> | ||
- Florion <[email protected]> | ||
- Garrett Moseke <[email protected]> | ||
- Iceman <[email protected]> | ||
- Joannis Orlandos <[email protected]> | ||
- Jonathan Pulfer <[email protected]> | ||
|
@@ -32,6 +34,7 @@ needs to be listed here. | |
- 0xpablo <[email protected]> | ||
- Michael Stegeman <[email protected]> | ||
- Ronald Mannak <[email protected]> | ||
- Carl Downing <[email protected]> | ||
- Nicholas Trienens <[email protected]> | ||
- Mahdi Bahrami <[email protected]> | ||
- Joseph Heck <[email protected]> | ||
|
@@ -40,8 +43,10 @@ needs to be listed here. | |
- Gao Lei <[email protected]> | ||
- Kanz <[email protected]> | ||
- Kia Abdi <[email protected]> | ||
- Michael <[email protected]> | ||
- Michael Critz <[email protected]> | ||
- Patrick Heneise <[email protected]> | ||
- xavgru12 <[email protected]> | ||
- Ádám Rocska <[email protected]> | ||
|
||
**Updating this list** | ||
|
This file contains 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
50 changes: 50 additions & 0 deletions
50
Sources/Hummingbird/Middleware/MiddlewareModule/_Middleware2.swift
This file contains 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,50 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Hummingbird server framework project | ||
// | ||
// Copyright (c) 2024 the Hummingbird authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// A middleware that composes two other middlewares. | ||
/// | ||
/// The two provided middlewares will be chained together, with `M0` first executing, followed by `M1`. | ||
/// | ||
/// You won't typically construct this middleware directly, but instead will use result builder syntax. | ||
/// | ||
/// ```swift | ||
/// router.addMiddleware { | ||
/// MiddlewareOne() | ||
/// MiddlewareTwo() | ||
/// } | ||
/// ``` | ||
@_documentation(visibility: internal) | ||
public struct _Middleware2<M0: MiddlewareProtocol, M1: MiddlewareProtocol>: MiddlewareProtocol where M0.Input == M1.Input, M0.Context == M1.Context, M0.Output == M1.Output { | ||
public typealias Input = M0.Input | ||
public typealias Output = M0.Output | ||
public typealias Context = M0.Context | ||
|
||
@usableFromInline let m0: M0 | ||
@usableFromInline let m1: M1 | ||
|
||
@inlinable | ||
public init(_ m0: M0, _ m1: M1) { | ||
self.m0 = m0 | ||
self.m1 = m1 | ||
} | ||
|
||
@inlinable | ||
public func handle(_ input: M0.Input, context: M0.Context, next: (M0.Input, M0.Context) async throws -> M0.Output) async throws -> M0.Output { | ||
try await self.m0.handle(input, context: context) { input, context in | ||
try await self.m1.handle(input, context: context, next: next) | ||
} | ||
} | ||
} | ||
|
||
extension _Middleware2: RouterMiddleware where M0.Input == Request, M0.Output == Response {} |
48 changes: 48 additions & 0 deletions
48
Sources/Hummingbird/Middleware/MiddlewareModule/_OptionalMiddleware.swift
This file contains 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,48 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Hummingbird server framework project | ||
// | ||
// Copyright (c) 2024 the Hummingbird authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// A middleware that can handle an optional middleware. | ||
/// | ||
/// This middleware is useful for situations where you want to optionally unwrap a middleware. | ||
/// | ||
/// You won't typically construct this middleware directly, but instead will use standard `if`-`else` | ||
/// statements in a parser builder to automatically build conditional middleware: | ||
/// | ||
/// ```swift | ||
/// router.addMiddleware { | ||
/// if let middleware { | ||
/// middleware | ||
/// } | ||
/// ... | ||
/// } | ||
/// ``` | ||
@_documentation(visibility: internal) | ||
public struct _OptionalMiddleware<M0: MiddlewareProtocol>: MiddlewareProtocol { | ||
public typealias Input = M0.Input | ||
public typealias Output = M0.Output | ||
public typealias Context = M0.Context | ||
|
||
public let middleware: M0? | ||
|
||
@inlinable | ||
public func handle(_ input: M0.Input, context: M0.Context, next: (M0.Input, M0.Context) async throws -> M0.Output) async throws -> M0.Output { | ||
guard let middleware else { | ||
return try await next(input, context) | ||
} | ||
|
||
return try await middleware.handle(input, context: context, next: next) | ||
} | ||
} | ||
|
||
extension _OptionalMiddleware: RouterMiddleware where M0.Input == Request, M0.Output == Response {} |
52 changes: 52 additions & 0 deletions
52
Sources/Hummingbird/Middleware/MiddlewareModule/_SpreadMiddleware.swift
This file contains 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,52 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Hummingbird server framework project | ||
// | ||
// Copyright (c) 2024 the Hummingbird authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// A middleware that can handle an array of middleware. | ||
/// | ||
/// This middleware is useful for situations where you want to compose an array of middleware together. | ||
/// | ||
/// You won't typically construct this middleware directly, but instead will use standard `for` loop | ||
/// statements in a result builder to automatically build spread middleware: | ||
/// | ||
/// ```swift | ||
/// router.addMiddleware { | ||
/// for logger in loggers { | ||
/// LoggingMiddleware(logger: logger) | ||
/// } | ||
/// } | ||
/// ``` | ||
@_documentation(visibility: internal) | ||
public struct _SpreadMiddleware<M0: MiddlewareProtocol>: MiddlewareProtocol { | ||
public typealias Input = M0.Input | ||
public typealias Output = M0.Output | ||
public typealias Context = M0.Context | ||
|
||
let middlewares: [M0] | ||
|
||
public func handle(_ input: Input, context: Context, next: (Input, Context) async throws -> Output) async throws -> Output { | ||
return try await handle(middlewares: self.middlewares, input: input, context: context, next: next) | ||
|
||
func handle(middlewares: some Collection<M0>, input: Input, context: Context, next: (Input, Context) async throws -> Output) async throws -> Output { | ||
guard let current = middlewares.first else { | ||
return try await next(input, context) | ||
} | ||
|
||
return try await current.handle(input, context: context, next: { input, context in | ||
try await handle(middlewares: middlewares.dropFirst(), input: input, context: context, next: next) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
extension _SpreadMiddleware: RouterMiddleware where M0.Input == Request, M0.Output == Response {} |
This file contains 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