stephenh / ts-proto

An idiomatic protobuf generator for TypeScript
Apache License 2.0
2.15k stars 346 forks source link

Feature Request: add options to limit generating encode and decode methods to only specific message types #1084

Open akrennmair opened 2 months ago

akrennmair commented 2 months ago

I'm facing the specific problem that a significant amount of the generated script size of a project mine consists of scripts generated by ts-proto. I was looking into limiting what is being generated, and found the outputEncodeMethods option to be too limited: generally speaking, I have two top-level types, and for one (the request type) I only want to generate an encode method, while for the other (the response type), I only want to generate a decode method.

I would therefore like to have options to limit the generation of encode resp. decode methods to only specific types. A similar suggestion has also been made as a comment to #773: https://github.com/stephenh/ts-proto/issues/773#issuecomment-1422208778

I also have a branch in which I implemented a possible solution that simply regular expressions. I'm not sure though whether this is sufficient as a general solution. I will submit a PR for it.

stephenh commented 2 months ago

Hi @akrennmair ; thanks for the PR! I guess that makes sense...

Out of curiosity, for your use case, is each type only primitives? Or does it have other messages?

I.e. I could see something like:

Such that to "encode Person", you actually end up ending encode methods for ~4-5 different messages, which seems like it would make keeping the regex updated kind of tedious? Or do you think that wouldn't be a problem?

akrennmair commented 2 months ago

@stephenh the way we structure our messages is through nested message types, so we do have more complex structures in there, but they're all underneath our request message type, so they'd be automatically covered by a regex just matching for a prefix. I updated one of the integration tests to specifically demonstrate that: https://github.com/stephenh/ts-proto/pull/1085/files#diff-1039be2c30dc2bd6576bdf8e7acd59bee9e3bf408cf023373f9bf2241d675a69

I have been thinking as well whether it could become too tedious to manually manage the regular expression, so I wonder whether it would also makes sense to add inverted options, i.e. output{Encode,Decode}ExcludeTypes where any types matching that regex would not have their encode resp. decode methods generated.