Skip to content

Commit 861ddc9

Browse files
committed
fix return type
1 parent 50241a0 commit 861ddc9

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

Macros/Tests/HTTPHandlerMacroTests.swift

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929
// SOFTWARE.
3030
//
3131

32+
import Foundation
3233
import FlyingFox
3334
import FlyingFoxMacros
34-
import XCTest
35+
import Testing
3536

36-
final class HTTPHandlerMacroTests: XCTestCase {
37+
struct HTTPHandlerMacroTests {
3738

38-
func testHandler() async throws {
39+
@Test
40+
func handler() async throws {
3941
let handler = MacroHandler()
4042

4143
await AsyncAssertEqual(
@@ -55,6 +57,24 @@ final class HTTPHandlerMacroTests: XCTestCase {
5557
try await handler.handleRequest(.make(path: "/fish")).jsonDictionaryBody,
5658
["name": "Pickles"]
5759
)
60+
61+
await AsyncAssertEqual(
62+
try await handler.handleRequest(.make(path: "/chips")).jsonDictionaryBody,
63+
["name": "🍟"]
64+
)
65+
66+
await AsyncAssertEqual(
67+
try await handler.handleRequest(.make(path: "/shrimp")).jsonDictionaryBody,
68+
["name": "🦐"]
69+
)
70+
71+
await AsyncAssertEqual(
72+
try await handler.handleRequest(.make(path: "/all")).jsonArrayBody,
73+
[
74+
["name": "Tyger Tyger"],
75+
["name": "Burning Bright"]
76+
]
77+
)
5878
}
5979
}
6080

@@ -79,9 +99,29 @@ private struct MacroHandler {
7999
Fish(name: "Pickles")
80100
}
81101

102+
@JSONRoute("/chips")
103+
func getFoo() -> MacroHandler.Chips {
104+
MacroHandler.Chips(name: "🍟")
105+
}
106+
107+
@JSONRoute("/shrimp")
108+
func getShrimp() -> some Encodable {
109+
MacroHandler.Chips(name: "🦐")
110+
}
111+
112+
@JSONRoute("/all")
113+
func getAll() -> [Fish] {
114+
[
115+
Fish(name: "Tyger Tyger"),
116+
Fish(name: "Burning Bright")
117+
]
118+
}
119+
82120
struct Fish: Encodable {
83121
var name: String
84122
}
123+
124+
typealias Chips = Fish
85125
}
86126

87127
private extension HTTPResponse {
@@ -95,4 +135,14 @@ private extension HTTPResponse {
95135
return object as? NSDictionary
96136
}
97137
}
138+
139+
var jsonArrayBody: NSArray? {
140+
get async {
141+
guard let data = try? await bodyData,
142+
let object = try? JSONSerialization.jsonObject(with: data, options: []) else {
143+
return nil
144+
}
145+
return object as? NSArray
146+
}
147+
}
98148
}

Plugins/Sources/FunctionDecl.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extension FunctionDecl {
9090
parameters: [],
9191
effects: [],
9292
attributes: [],
93-
returnType: .init(syntax.signature.returnClause?.type.as(IdentifierTypeSyntax.self)?.name.text)
93+
returnType: .init(syntax.signature.returnClause?.type.trimmedDescription)
9494
)
9595

9696
decl.attributes = syntax.attributes
@@ -115,7 +115,7 @@ extension FunctionDecl {
115115
decl.effects.insert(.async)
116116
}
117117

118-
if syntax.signature.effectSpecifiers?.throwsSpecifier != nil {
118+
if syntax.signature.effectSpecifiers?.throwsClause?.throwsSpecifier != nil {
119119
decl.effects.insert(.throws)
120120
}
121121

Plugins/Sources/HTTPHandlerMacro.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ import SwiftSyntaxBuilder
3434
import SwiftSyntaxMacros
3535

3636
public enum HTTPHandlerMacro: MemberMacro {
37+
3738
public static func expansion(
3839
of node: AttributeSyntax,
3940
providingMembersOf declaration: some DeclGroupSyntax,
41+
conformingTo protocols: [TypeSyntax],
4042
in context: some MacroExpansionContext
4143
) throws -> [DeclSyntax] {
4244
let memberList = declaration.memberBlock.members

0 commit comments

Comments
 (0)