-
Notifications
You must be signed in to change notification settings - Fork 125
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
Ability to attach macros to generated types and methods #383
Comments
@HonQii could you elaborate a little on what you'd like to see? You can currently write extensions on the types generated and conform them to protocols that you define. Are you asking for the protocol conformance to be automatically added for you by the generator, potentially by specifying a macro you'd like to run in the generator config? Maybe you could provide a quick snippet of how you'd like the generated code to be different. |
Maybe @HonQii means that they could configure some macro name to be automatically attached to all generated types, similar to how we allow adding additional imports? But the user would still need to provide the macro implementation. (Which is an interesting idea that would indeed provide quite a lot of flexibility for sufficiently motivated adopters.) |
Right, what I mean is that I can configure macros to the generated Schema as well as the Client so that I can do some wrapping. An example: // Each of my request response structures will have an error field with a message and code if there is an error
struct BaseResponse {
var error: Error? // empty if there is no error.
} Normally I would need to define a protocol for all structures to follow, and then I could handle these errors consistently. But in the case of autogeneration, I think it's more appropriate to use a macro for this purpose Similarly, I need a layer of encapsulation for each request in the client to handle exceptions, which can also be accomplished using a macro |
Can you be even more specific here, for example use a simple OpenAPI document, generate code with it, and then outline what exact changes you'd like to see in the generated code? I'm still not entirely sure I understand. |
OOI, are you the author of this document? Because, if so, for this example, you could have the generated code make use of shared types by writing this in the OpenAPI document itself. For example, you can make use of of a common type in Concretely, if you make use of references in the OpenAPI document, then the generator will use a single type. This allows for you to write extensions on this common type once. Footnotes |
For example, I'm using the Combine framework to encapsulate a lot of UI components, so I'd like to be able to return web requests through the Publisher, and I'm now generating my own batch of code using the Plugin, which is formatted as follows: func getMyInfo(_ query: Components.Schemas.GetMyInfoReq) -> AnyPublisher<Components.Schemas.GetMyInfoResp, Error> {
Future { promise in
Task {
do {
let resp = try await self.filterSuccess(await self.client.getMyInfo(.init(body: .json(query))))
promise(.success(resp))
} catch {
promise(.failure(error))
}
}
}
.eraseToAnyPublisher()
} If I could support macros when generating swift-openapi-generator, I could generate the methods directly from the macros, and instead of having to convert the results using the |
Sorry, I didn't write it. |
If I understand correctly, you'd be interested in being able to use a macro to generate a variant of every client method that uses Combine, like you outlined above? If so, that seems very similar to the "add async variant" macro here: https://github.com/apple/swift-syntax/blob/main/Examples/Sources/MacroExamples/Implementation/Peer/AddAsyncMacro.swift The question is where exactly the generator would add the |
Okay, renaming this issue to track the general desire to attach macros to generated types and functions. |
It would be nice to be able to add macros to the generated Schema and API requests in order to extend the functionality, e.g. all of my request return structures have an optional error field, so if I could add a macro that would allow me to extend the support for a protocol for them, I could easily handle the error uniformly
The text was updated successfully, but these errors were encountered: