Skip to content

Commit

Permalink
1.5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Apr 7, 2024
1 parent 2497058 commit c931a01
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ var package = Package(
.product(name: "SwiftAPIClient", package: "swift-api-client"),
]
),
.testTarget(
name: "PetStoreTests",
dependencies: [
.target(name: "PetStore")
]
),
.target(
name: "PetStoreWithMacros",
dependencies: [
Expand Down
11 changes: 11 additions & 0 deletions Example/TEsts/PetStoreTests/PetStoreTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation
@testable import PetStore
import SwiftAPIClient
import XCTest

final class PetStoreTests: XCTestCase {

func testRequests() async throws {
try await print(api().pet.findBy(status: .available))
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.5.6")
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.5.7")
],
targets: [
.target(
Expand Down
26 changes: 18 additions & 8 deletions Sources/SwiftAPIClientMacros/SwiftAPIClientMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,30 @@ public struct SwiftAPIClientCallMacro: PeerMacro {
}
var queryParams: ([String], [(String, String)]) = ([], [])
var bodyParams: ([String], [(String, String)]) = ([], [])
for param in funcDecl.signature.parameterClause.parameters {

var fileIDName = "fileID"
var lineName = "line"
for var param in funcDecl.signature.parameterClause.parameters {
let name = (param.secondName ?? param.firstName).trimmed.text
if param.attributes.contains("Body") || name == "body", attribute.method == ".get" {
throw MacroError("Body parameter is not allowed with GET method")
}
params += try scanParameters(name: name, type: "Query", param: param, into: &queryParams)
params += try scanParameters(name: name, type: "Body", param: param, into: &bodyParams)
if param.defaultValue?.value.description == "#fileID" {
fileIDName = name
} else if param.defaultValue?.value.description == "#line" {
lineName = name
} else {
let count = params.count
params += try scanParameters(name: name, type: "Query", param: &param, into: &queryParams)
params += try scanParameters(name: name, type: "Body", param: &param, into: &bodyParams)
if count == params.count {
params.append(param)
}
}
}

params += [
"\nfileID: String = #fileID,",
"\nline: UInt = #line",
"\n\(raw: fileIDName): String = #fileID,",
"\n\(raw: lineName): UInt = #line",
]
funcDecl.signature.parameterClause.rightParen.leadingTrivia = .newline
funcDecl.signature.parameterClause.parameters = params
Expand Down Expand Up @@ -391,10 +402,9 @@ private func pathArguments(
private func scanParameters(
name: String,
type: String,
param: FunctionParameterListSyntax.Element,
param: inout FunctionParameterListSyntax.Element,
into list: inout ([String], [(String, String)])
) throws -> [FunctionParameterListSyntax.Element] {
var param = param
param.trailingComma = .commaToken()
param.leadingTrivia = .newline
if param.attributes.remove(type.firstUppercased) {
Expand Down
31 changes: 31 additions & 0 deletions Tests/SwiftAPIClientMacrosTests/CallMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,35 @@ final class CallMacroTests: XCTestCase {
indentationWidth: .spaces(2)
)
}

func testCallMacroFuncWithArguments() {
assertMacroExpansion(
"""
@GET
func login(username: String, password: String) {
client.auth(.basic(username: username, password: password))
}
""",
expandedSource: """
func login(username: String, password: String) {
client.auth(.basic(username: username, password: password))
}
func login(
username: String,
password: String,
fileID: String = #fileID,
line: UInt = #line
) async throws {
try await
client.auth(.basic(username: username, password: password))
.path("login")
.method(.get)
.call(.http, as: .void, fileID: fileID, line: line)
}
""",
macros: macros,
indentationWidth: .spaces(2)
)
}
}

0 comments on commit c931a01

Please sign in to comment.