Skip to content

Commit

Permalink
Add support for inout parameters to @DependencyEndpointMacro (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex293 committed Nov 29, 2023
1 parent 878d582 commit 778db38
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
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"
}
}
"""
}
}
}

0 comments on commit 778db38

Please sign in to comment.