Skip to content

proposal: Configurable Post Processors #1725

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

Closed
tinyzimmer opened this issue Jul 6, 2022 · 1 comment
Closed

proposal: Configurable Post Processors #1725

tinyzimmer opened this issue Jul 6, 2022 · 1 comment
Labels
enhancement New feature or request proposal triage New issues that hasn't been reviewed

Comments

@tinyzimmer
Copy link
Contributor

tinyzimmer commented Jul 6, 2022

What do you want to change?

This idea comes from various open and closed issues. Most recently #1485 (which I jumped in at the last minute to help push over the finish line).

The idea is simple - the implemetation probably not so much. So, I was hoping to open an Issue to put forth the proposal and flesh out the idea before venturing into implementation.


Proposal: An extension to the configuration that allows for running arbitrary post-processors against generated code.

This could take one of two forms in my head:

  • Simple stdin/stdout approach : Where essentially the input could be passed as stdin to some arbitrary command string and the output replaced with whatever it produces on its stdout.

This method would probably be pretty easy to implement (relatively speaking), but would come with limitations that I'll expand on in the second approach.

  • A plugin model built around a public interface in the code. Ideally with support for third-party plugins (perhaps via an RPC interface such as used in Hashicorp Vault), but that wouldn't be as crucial out of the gate. The interface could look something like (pseudo-code)
type PostProcessor interface {
    Render(Context) (io.ReadCloser, error)
}

Where the Context contains AST information and other metadata for the file being rendered. A built-in reference implementation could be a simple search/replace and/or type override. A custom plugin could have satisfied the NullableEnum issue until support was included upstream. This makes things much more flexible for the post-processor and runs into far fewer limitations.

If you are open to either of these proposals, I'm happy to help see them to reality. I feel like this would end up having considerable overlap with some of the existing functionality regarding the child directives in the gen config. But perhaps there is a way for it to fit into that paradigm as well, and then some of those directives could be implemented as going through a chain of "Post Processors".


Some additional context regarding this idea. One thing I've done for an internal project (though this may become open source) is a post-processor (that has to be invoked after sqlc) that generates most of a GraphQL schema from the generated code using
https://github.com/graphql-go/graphql.

One of the pain points in that generator, is it has to effectively rebuild its own Go AST by parsing the sqlc output. If a plugin interface like described above existed, the implementation would be much cleaner.

What database engines need to be changed?

PostgreSQL, MySQL

What programming language backends need to be changed?

Go, Python, Kotlin

@tinyzimmer tinyzimmer added enhancement New feature or request triage New issues that hasn't been reviewed labels Jul 6, 2022
@kyleconroy
Copy link
Collaborator

Support for codegen plugins (both WASM and process-based) was merged recently (#1684, #1578). As we build out documentation and examples for those, I have no plans to add additional plugins or post-processors.

The existing plugin system is designed after protoc in which plugins are additive; they can't change the output of other plugins. While that may sound limiting, it's too difficult to version the actual Go AST. sqlc reserves the right to change internal implementation details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request proposal triage New issues that hasn't been reviewed
Projects
None yet
Development

No branches or pull requests

2 participants