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

Autogenerate create${MessageName} functions #1068

Closed
mikeki opened this issue Feb 11, 2025 · 2 comments
Closed

Autogenerate create${MessageName} functions #1068

mikeki opened this issue Feb 11, 2025 · 2 comments

Comments

@mikeki
Copy link

mikeki commented Feb 11, 2025

would it be possible to include the autogeneration of the create function for each message?

e.g. for a

message User {
  string name = 1;
}

We would end up with:

export type User = Message<"User"> & {
  name: string;
}

export const UserSchema: GenMessage<User> =
  manageDesc(file_user, 0);

Could we include the following in the code generation?

export const createUser = (init?: Omit<User, "$typeName" | "$unknown">) =>
  create(UserSchema, init);
@timostamm
Copy link
Member

Of course it is technically possible to generate such a function, but it needs to provide a significant benefit. We purposefully generate as little as possible because it keeps the API easy to understand, maintain, and support in other packages like @bufbuild/protoplugin.

My first questions would be:

  • What's the advantage of createUser({}) over create(UserSchema, {}) ? Fewer characters is nice, but what about all other functions tied to a schema, for example toJson, mergeFromBinary, clone, and future functions (e.g. merge)?

  • Why take Omit<T, "$typeName" | "$unknown"> as the initializer (unlike create), and require the user to provide values for proto2 optionals?

@mikeki
Copy link
Author

mikeki commented Feb 12, 2025

Thank you very much for the detailed response.

There's definitely so much I hadn't thought about this program, and it's good to hear all your rationale.

Thanks

@mikeki mikeki closed this as completed Feb 12, 2025
@mikeki mikeki closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2025
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