Skip to content

Commit

Permalink
Use macro to generate authorization property (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hallee authored Feb 17, 2024
1 parent fe96c02 commit 9b8c570
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/EndpointBuilder/EndpointMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import RoutingKit
/// Generates a `path` string for the given path components,
/// along with a helper type for initializing any path parameters.
@attached(extension, conformances: Endpoint)
@attached(member, names: named(PathParameters), named(pathParameters), named(path))
@attached(member, names: named(authorization), named(PathParameters), named(pathParameters), named(path))
public macro Endpoint() = #externalMacro(module: "EndpointBuilderMacros", type: "EndpointMacro")
11 changes: 10 additions & 1 deletion Sources/EndpointBuilderMacros/EndpointMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ extension EndpointMacro: MemberMacro {
providingMembersOf declaration: D,
in context: C
) throws -> [DeclSyntax] {
let authorizationDefinition = [
VariableDeclSyntax(
modifiers: declaration.modifiers,
Keyword.var,
name: PatternSyntax(stringLiteral: "authorization"),
type: TypeAnnotationSyntax(type: TypeSyntax(stringLiteral: "Authorization?"))
).as(DeclSyntax.self)
].compactMap { $0 }

let (pathComponents, pathVariableSyntax) = try endpointPathParameters(declaration: declaration)
let pathParameters = pathComponents.filter { pathComponent in
guard case .parameter = pathComponent else {
Expand All @@ -57,7 +66,7 @@ extension EndpointMacro: MemberMacro {
return try endpointPathParametersStructDefinition(declaration: declaration, pathParameters)
}()

return pathDefinition + structDefinition
return authorizationDefinition + pathDefinition + structDefinition
}

@usableFromInline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final class EndpointBuilderMacrosTests: XCTestCase {
public static let path: [RoutingKit.PathComponent] = ["user", ":id"]
public static let httpMethod = HTTPRequest.Method.get
public static let responseType = User.self
public let authorization: Authorization?
}
"""
} expansion: {
Expand All @@ -29,7 +28,8 @@ final class EndpointBuilderMacrosTests: XCTestCase {
public static let path: [RoutingKit.PathComponent] = ["user", ":id"]
public static let httpMethod = HTTPRequest.Method.get
public static let responseType = User.self
public let authorization: Authorization?
public var authorization: Authorization?
public var path: String {
"/" + ["user", pathParameters.id].joined(separator: "/")
Expand Down Expand Up @@ -68,6 +68,8 @@ final class EndpointBuilderMacrosTests: XCTestCase {
public static let httpMethod = HTTPRequest.Method.get
public static let responseType = User.self
public var authorization: Authorization?
public var path: String {
"/" + ["team", pathParameters.teamId, "user", pathParameters.userId].joined(separator: "/")
}
Expand Down

0 comments on commit 9b8c570

Please sign in to comment.