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

extension points for plugin #183

Closed
hugebdu opened this issue Oct 28, 2021 · 3 comments
Closed

extension points for plugin #183

hugebdu opened this issue Oct 28, 2021 · 3 comments

Comments

@hugebdu
Copy link
Contributor

hugebdu commented Oct 28, 2021

Do you have any plans to allow extending the existing plugin? Or maybe it is possible already, and I just couldn't find it...

For example we'd like for each and every proto message to generate builders, test builders (with test values) and probably jest matchers.

Of course we can take the plugin framework and write our own but that means duplicate codegen execution and plenty of code copy & paste from the plugin.

Thanks!

@timostamm
Copy link
Owner

I have never used extension points, but I've heard they might be unpleasant to use. Is it an option to do what you need in runtime?

For example, the existing reflectionCreate() takes a IMessageType<T> and returns an instance of message T, all via reflection ops:

export function reflectionCreate<T extends object>(type: IMessageType<T>): T {

I imagine it should be possible to create a jest matcher like isMessageOfType(received, type: IMessageType), for example.

Could you elaborate a bit what you want to do?

@hugebdu
Copy link
Contributor Author

hugebdu commented Oct 31, 2021

@timostamm thanks for your reply.
The runtime magic is an option indeed, however the devex isn't great (IDE support and compilation errors are cryptic). Thus I prefer to do a codegen. Currently having some post-codegen with ts-morph.

Another use case I have is message type refinement (due to custom options) or method signatures (think something like CallContext argument).

I was thinking about some decorators support for all those tasty generators you have there.

Since I couldn't find any programmatic API for the plugin, possible API could be another plugin parameter with absolute path to .js file with decorators factory:

exports = {
  enumGeneratorDecorator: {...},
  methodInfoGeneratorDecorator: {...},
  // etc
}

@timostamm
Copy link
Owner

The plan is to switch to the base types provided by protobuf-es. protobuf-es includes a much improved plugin framework with @bufbuild/protoplugin, which makes it a whole lot easier to write your own plugins.

I think this should be the best way forward for your use case as well (generating builder, test builders, jest matchers). It might not give you all the capability extension points give you, but they are a double edged sword for sure. You can see the code for a simple plugin here. I hope we can simplify it even more (not needing to type out .js, .ts and .d.ts code for example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants