Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for inout parameters to @DependencyEndpointMacro #149

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/DependenciesMacrosPlugin/DependencyEndpointMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,15 @@ public enum DependencyEndpointMacro: AccessorMacro, PeerMacro {
parameters[i].secondName = TokenSyntax(stringLiteral: "p\(offset)")
parameters[i].colon = parameters[i].colon ?? .colonToken(trailingTrivia: .space)
}
let appliedParameters = (0..<parameters.count).map { "p\($0)" }.joined(separator: ", ")
let appliedParameters = parameters
.map {
guard let typed = $0.type.as(AttributedTypeSyntax.self), typed.specifier?.tokenKind == .keyword(.inout)
else { return false }
return true
}
.enumerated()
.map { $1 ? "&p\($0)" : "p\($0)" }
.joined(separator: ", ")
decls.append(
"""
\(raw: attributes.map { "@\($0) " }.joined())\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,4 +755,45 @@ final class DependencyEndpointMacroTests: BaseTestCase {
"""
}
}

func testInout() {
assertMacro {
"""
struct Blah {
@DependencyEndpoint
public var doAThing: (_ a: inout Int, _ b: Int, _ c: inout Bool) -> String = { _ in
"Hello, world"
}
}
"""
} expansion: {
"""
struct Blah {
public var doAThing: (_ a: inout Int, _ b: Int, _ c: inout Bool) -> String = { _ in
"Hello, world"
} {
@storageRestrictions(initializes: _doAThing)
init(initialValue) {
_doAThing = initialValue
}
get {
_doAThing
}
set {
_doAThing = newValue
}
}

public func doAThing(a p0: inout Int, b p1: Int, c p2: inout Bool) -> String {
self.doAThing(&p0, p1, &p2)
}

private var _doAThing: (_ a: inout Int, _ b: Int, _ c: inout Bool) -> String = { _ in
XCTestDynamicOverlay.XCTFail("Unimplemented: 'doAThing'")
return "Hello, world"
}
}
"""
}
}
}