Skip to content

Commit 1b0bd09

Browse files
committed
support route parameters
1 parent bf70de4 commit 1b0bd09

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

Macros/Tests/HTTPHandlerMacroTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ struct HTTPHandlerMacroTests {
4646
#expect(
4747
try await handler.handleRequest(.make(path: "/accepted")).statusCode == .accepted
4848
)
49+
#expect(
50+
try await handler.handleRequest(.make(path: "/account/fish")).bodyText == "fish"
51+
)
4952
#expect(
5053
try await handler.handleRequest(.make(path: "/teapot")).statusCode == .teapot
5154
)
@@ -80,6 +83,12 @@ private struct MacroHandler {
8083
HTTPResponse(statusCode: .accepted)
8184
}
8285

86+
@HTTPRoute("/account/:id")
87+
func accountID(_ req: HTTPRequest) async -> HTTPResponse {
88+
let id = req.routeParameters["id"] ?? ""
89+
return HTTPResponse(statusCode: .ok, body: id.data(using: .utf8)!)
90+
}
91+
8392
@HTTPRoute("/teapot", statusCode: .teapot)
8493
func getTeapot() throws { }
8594

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let package = Package(
1515
),
1616
],
1717
dependencies: [
18-
.package(url: "https://github.com/swhitty/FlyingFox.git", from: "0.22.0"),
18+
.package(url: "https://github.com/swhitty/FlyingFox.git", branch: "main"),
1919
.package(url: "https://github.com/swiftlang/swift-syntax", "510.0.0"..<"602.0.0")
2020
],
2121
targets: [

Plugins/Sources/HTTPHandlerMacro.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public enum HTTPHandlerMacro: MemberMacro {
4343
) throws -> [DeclSyntax] {
4444
let memberList = declaration.memberBlock.members
4545

46-
let routes = memberList.compactMap { member -> RouteDecl? in
46+
var routes = memberList.compactMap { member -> RouteDecl? in
4747
guard let funcDecl = FunctionDecl.make(from: member),
4848
let routeAtt = funcDecl.attribute(name: "HTTPRoute") ?? funcDecl.attribute(name: "JSONRoute") else {
4949
return nil
@@ -67,6 +67,10 @@ public enum HTTPHandlerMacro: MemberMacro {
6767
context.diagnoseWarning(for: node, "No HTTPRoute found")
6868
}
6969

70+
for i in routes.indices {
71+
routes[i].idx = i
72+
}
73+
7074
let routeDecl: DeclSyntax = """
7175
func handleRequest(_ request: HTTPRequest) async throws -> HTTPResponse {
7276
\(raw: routes.map(\.routeSyntax).joined(separator: "\n"))
@@ -103,6 +107,7 @@ private extension HTTPHandlerMacro {
103107
var isJSON: Bool
104108
var encoder: String
105109
var decoder: String
110+
var idx: Int = 0
106111

107112
var routeSyntax: String {
108113
if isJSON {
@@ -112,16 +117,29 @@ private extension HTTPHandlerMacro {
112117
}
113118
}
114119

120+
var responseVar: String {
121+
"response\(idx)"
122+
}
123+
115124
var httpRouteSyntax: String {
116125
if funcDecl.returnType.isVoid {
117126
"""
118-
if await HTTPRoute("\(route)") ~= request { \(funcCallSyntax)
119-
return HTTPResponse(statusCode: \(statusCode), headers: \(headers))
127+
let \(responseVar) = try await RoutedHTTPHandler.handleMatchedRequest(request, to: HTTPRoute("\(route)")) { _ in
128+
\(funcCallSyntax)
129+
return HTTPResponse(statusCode: \(statusCode), headers: \(headers))
130+
}
131+
if let \(responseVar) {
132+
return \(responseVar)
120133
}
121134
"""
122135
} else {
123136
"""
124-
if await HTTPRoute("\(route)") ~= request { return \(funcCallSyntax) }
137+
let \(responseVar) = try await RoutedHTTPHandler.handleMatchedRequest(request, to: HTTPRoute("\(route)")) { _ in
138+
return \(funcCallSyntax)
139+
}
140+
if let \(responseVar) {
141+
return \(responseVar)
142+
}
125143
"""
126144
}
127145
}

0 commit comments

Comments
 (0)