I'm trying to add a macro that generates a type alongside a user defined protocol like so:
@Mocked
protocol MyService {
func getTodos() -> [Todo]
}
// This type would be generated by the `@Mocked` macro.
struct MyServiceMock: MyService {
// Default implementations
}
I defined @Mocked as a PeerMacro
however I get the following error in the above example.
@Mocked // ๐ Macro doesn't conform to required macro role
protocol MyService {
...
Here is the implementation of MockedMacro
:
struct MockedMacro: PeerMacro {
static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return [
"""
struct \(node.attributeName)Mock {
func test() -> String {
return "Testing Macros!"
}
}
"""
]
}
}
Is this possible right now? Can a peer macro (or any macro type) generate a type alongside the type it is applied to? If not is this something that's a possibility for the future? I'd use a nested type but those aren't allowed in a protocol
.