Skip to content

Commit

Permalink
1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Mar 28, 2024
1 parent 75afb33 commit 5728a93
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.2.1")
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.2.2")
],
targets: [
.target(
Expand Down
30 changes: 19 additions & 11 deletions Sources/SwiftAPIClientMacros/SwiftAPIClientMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,22 @@ public struct SwiftAPIClientPathMacro: MemberMacro, MemberAttributeMacro, PeerMa
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some SwiftSyntaxMacros.MacroExpansionContext
) throws -> [DeclSyntax] {
guard let structDecl = declaration.as(StructDeclSyntax.self) else {
return []
}
let accessControl = structDecl.modifiers.first(as: { $0.as(AccessorDeclSyntax.self) }).map {
let structDecl = declaration.as(StructDeclSyntax.self)
guard structDecl != nil || declaration.is(ExtensionDeclSyntax.self) else {
throw MacroError("Path macro can be applied only to struct or extension")
}
guard let declName = structDecl?.name, node.description.contains("Path") else {
return []
}
let accessControl = structDecl?.modifiers.first(as: { $0.as(AccessorDeclSyntax.self) }).map {
"\($0) "
} ?? ""

let path = path(node: node, name: structDecl.name)


let path = path(node: node, name: declName)
let pathArguments = pathArguments(path: path)
let isVar = pathArguments.isEmpty
let pathName = structDecl.name.trimmed.text.firstLowercased
let pathName = declName.trimmed.text.firstLowercased
let name = path.count > pathArguments.count ? pathName : "callAsFunction"
let args = pathArguments.enumerated()
.map { "\($0.offset == 0 ? "_ " : "")\($0.element.0): \($0.element.1)" }
Expand All @@ -176,8 +181,8 @@ public struct SwiftAPIClientPathMacro: MemberMacro, MemberAttributeMacro, PeerMa
return [
"""
/// /\(raw: path.joined(separator: "/"))
\(raw: accessControl)\(raw: isVar ? "var" : "func") \(raw: name)\(raw: isVar ? ":" : "(\(args)) ->") \(raw: structDecl.name) {
\(raw: structDecl.name)(client: \(raw: client))
\(raw: accessControl)\(raw: isVar ? "var" : "func") \(raw: name)\(raw: isVar ? ":" : "(\(args)) ->") \(raw: declName) {
\(raw: declName)(client: \(raw: client))
}
""",
]
Expand All @@ -188,10 +193,13 @@ public struct SwiftAPIClientPathMacro: MemberMacro, MemberAttributeMacro, PeerMa
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard !declaration.is(ExtensionDeclSyntax.self) else {
return []
}
if let structDecl = declaration.as(StructDeclSyntax.self) {
return try structExpansion(of: node, providingMembersOf: structDecl, in: context)
} else {
throw MacroError("\(node.attributeName.description) macro can only be attached to a struct")
throw MacroError("\(node.attributeName.description) macro can only be attached to a struct or extension")
}
}

Expand All @@ -205,7 +213,7 @@ public struct SwiftAPIClientPathMacro: MemberMacro, MemberAttributeMacro, PeerMa
var result: [DeclSyntax] = [
"public typealias Body<Value> = _APIParameterWrapper<Value>",
"public typealias Query<Value> = _APIParameterWrapper<Value>",
"\(raw: isPath ? "private let" : "public var") client: APIClient",
"public var client: APIClient",
]
var hasRightInit = false
var hasOtherInits = false
Expand Down
65 changes: 65 additions & 0 deletions Tests/SwiftAPIClientMacrosTests/APIMacroTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import SwiftAPIClientMacros
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest

final class APIMacroTests: XCTestCase {

private let macros: [String: Macro.Type] = [
"API": SwiftAPIClientPathMacro.self
]

func testExpansionAPI() {
assertMacroExpansion(
"""
@API
struct Pets {
}
""",
expandedSource: """
struct Pets {
public typealias Body<Value> = _APIParameterWrapper<Value>
public typealias Query<Value> = _APIParameterWrapper<Value>
public var client: APIClient
public init(client: APIClient) {
self.client = client
}
}
""",
macros: macros,
indentationWidth: .spaces(2)
)
}

func testExpansionAPIWithInit() {
assertMacroExpansion(
"""
@API
struct Pets {
init(client: APIClient) {
self.client = client
}
}
""",
expandedSource: """
struct Pets {
init(client: APIClient) {
self.client = client
}
public typealias Body<Value> = _APIParameterWrapper<Value>
public typealias Query<Value> = _APIParameterWrapper<Value>
public var client: APIClient
}
""",
macros: macros,
indentationWidth: .spaces(2)
)
}
}
8 changes: 4 additions & 4 deletions Tests/SwiftAPIClientMacrosTests/PathMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class PathMacroTests: XCTestCase {
public typealias Query<Value> = _APIParameterWrapper<Value>
private var client: APIClient
public var client: APIClient
fileprivate init(client: APIClient) {
self.client = client
Expand Down Expand Up @@ -54,7 +54,7 @@ final class PathMacroTests: XCTestCase {
public typealias Query<Value> = _APIParameterWrapper<Value>
private var client: APIClient
public var client: APIClient
fileprivate init(client: APIClient) {
self.client = client
Expand Down Expand Up @@ -85,7 +85,7 @@ final class PathMacroTests: XCTestCase {
public typealias Query<Value> = _APIParameterWrapper<Value>
private var client: APIClient
public var client: APIClient
fileprivate init(client: APIClient) {
self.client = client
Expand Down Expand Up @@ -121,7 +121,7 @@ final class PathMacroTests: XCTestCase {
public typealias Query<Value> = _APIParameterWrapper<Value>
private var client: APIClient
public var client: APIClient
fileprivate init(client: APIClient) {
self.client = client
Expand Down

0 comments on commit 5728a93

Please sign in to comment.