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

Support for proto-loader "virtual" oneof field #1041

Open
jtimmons opened this issue May 14, 2024 · 0 comments
Open

Support for proto-loader "virtual" oneof field #1041

jtimmons opened this issue May 14, 2024 · 0 comments

Comments

@jtimmons
Copy link

The @grpc/proto-loader library (documentation) supports a oneofs config option which adds some additional information to a deserialized message to identify which field in a oneof (if any) the user specified. My team makes heavy use of this feature, so not having that information in our typedefs is a bit of a hinderance to us switching over to ts-proto.

Proposal

Introduce a new oneof=virtual_properties option which would behave the same as the existing properties option but add in the proto-loader "virtual" property as described below.

Example

Given the following proto message definition:

message ExampleMessage {
  oneof contact {
    string phoneNumber = 1;
    string email = 2;
  }
}

deserializing a message with the email field specified results in the following object. Note the contact field which proto-loader populates to let you know which field in the oneof the user actually provided:

{
  contact: "email",
  email: "[email protected]"
}

specifying this in the interface generated by ts-proto could look like the following:

export interface ExampleMessage {
  phoneNumber: string | undefined;
  email: string | undefined;
  contact: 'phoneNumber' | 'email'; // new
}

even better would be to use a discriminating union similar to the oneof=union option but that might be a good amount of work. If people agree with this as a proposal I'd be happy to take a stab at it

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

1 participant