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

Feature request: Allow prefixing generated type names #1017

Closed
csimmons0 opened this issue Nov 19, 2024 · 2 comments
Closed

Feature request: Allow prefixing generated type names #1017

csimmons0 opened this issue Nov 19, 2024 · 2 comments

Comments

@csimmons0
Copy link

csimmons0 commented Nov 19, 2024

It would be great if we could simply add a line to buf.gen.yaml that made the generator add a prefix to the generated types' names. For example, I would just say typesPrefix: "Pb" in the yaml file, and then for this proto enum:

enum Foo {
  ...
}

the generated TS code would be

export enum PbFoo {
  ...
}

Same for message types and their TS classes. The motivation for this request is to avoid name collisions between generated TS types and manually-written types that correspond to them. Without this feature I have to manually alias names when I import them, as in

import {Foo as PbFoo} from ...
@JosephusPaye
Copy link

This would be quite useful to have. In our case we're generating wrapper classes that provide an API similar to v1 and protobuf-js, and having the base types prefixed would be handy.

sbarfurth added a commit to sbarfurth/protobuf-es that referenced this issue Jan 1, 2025
Prefixing and suffixing the names of types enables cleaner operation when custom plugins are used that may generate multiple representations of the same underlying type.

Fixes bufbuild#1017
@timostamm
Copy link
Member

I understand the use case - I've maintained code bases before that had to use import aliases. But this feature isn't without drawbacks:

  • Plugins that reference protoc-gen-es generated code will have to support this option as well, and users will have to remember to set them.
  • We already suffix names - descriptors have the "Schema" suffix, and JSON types have the "Json" suffix. With a prefix, names become very wordy.
  • Well-known types are usually pre-generated and can't be prefixed, but it is possible to generate them locally. A prefix adds complexity.

The inconvenience of import aliases is real, but the feature doesn't enable new use cases. Because of the drawbacks and the added maintenance costs, we are not going to support this feature.

As an alternative, we suggest the following options:

  1. Use a wildcard type import (it won't affect bundle sizes): import type * as pb from "./example_pb".
  2. Write a plugin that generates type aliases with prefixed names. It should be relatively simple to implement (see the guide in the manual), and gives great control.

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

3 participants