Skip to content

Commit

Permalink
Fix JSON multiline issue
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawright11 committed Dec 11, 2023
1 parent 8f49dec commit 6f42d93
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion PapyrusPlugin/Sources/APIAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum APIAttribute {
if case let .argumentList(list) = syntax.arguments {
for argument in list {
if let label = argument.label {
labeledArguments[label.description] = argument.expression.description
labeledArguments[label.text] = argument.expression.description
}
}

Expand Down
72 changes: 55 additions & 17 deletions PapyrusPlugin/Tests/APIMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,30 +287,21 @@ final class APIMacroTests: XCTestCase {
func testJSON() {
assertMacro(["API": APIMacro.self]) {
"""
struct User {
let id: Int
let name: String
}
@API
@KeyMapping(.snakeCase)
@JSON(encoder: .foo, decoder: .bar)
protocol MyService {
@POST("users")
func getUser(name: String) async throws -> User
func getUser() async throws
}
"""
} expansion: {
"""
struct User {
let id: Int
let name: String
}
@KeyMapping(.snakeCase)
@JSON(encoder: .foo, decoder: .bar)
protocol MyService {
@POST("users")
func getUser(name: String) async throws -> User
func getUser() async throws
}
struct MyServiceAPI: MyService {
Expand All @@ -320,12 +311,59 @@ final class APIMacroTests: XCTestCase {
self.provider = provider
}
func getUser(name: String) async throws -> User {
var req = builder(method: "POST", path: "users")
req.addField("name", value: name)
let res = try await provider.request(req)
try res.validate()
return try res.decode(User.self, using: req.responseDecoder)
func getUser() async throws {
let req = builder(method: "POST", path: "users")
try await provider.request(req).validate()
}
private func builder(method: String, path: String) -> RequestBuilder {
var req = provider.newBuilder(method: method, path: path)
req.keyMapping = .snakeCase
req.requestEncoder = .json(.foo)
req.responseDecoder = .json(.bar)
return req
}
}
"""
}
}

func testJSONMultiline() {
assertMacro(["API": APIMacro.self]) {
"""
@API
@KeyMapping(.snakeCase)
@JSON(
encoder: .foo,
decoder: .bar
)
protocol MyService {
@POST("users")
func getUser() async throws
}
"""
} expansion: {
"""
@KeyMapping(.snakeCase)
@JSON(
encoder: .foo,
decoder: .bar
)
protocol MyService {
@POST("users")
func getUser() async throws
}
struct MyServiceAPI: MyService {
private let provider: PapyrusCore.Provider
init(provider: PapyrusCore.Provider) {
self.provider = provider
}
func getUser() async throws {
let req = builder(method: "POST", path: "users")
try await provider.request(req).validate()
}
private func builder(method: String, path: String) -> RequestBuilder {
Expand Down

0 comments on commit 6f42d93

Please sign in to comment.