-
Notifications
You must be signed in to change notification settings - Fork 175
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 proto3 'optional' is coming in protobuf 3.12 #311
Comments
@haberman, how will this be represented in descriptors? I assume (hope) that the "label" field in Does that make sense? Luckily, I think this will be a minor change to support. Changing Do you know in what version of the Go protobuf runtime this is expected to be implemented? |
While that would be a clear way of expressing this in descriptors, it is unfortunately incompatible with the existing ecosystem on both the producing and consuming side:
Instead we put each proto3
@dsnet could answer this question. |
Oy. I guess I didn't read thoroughly enough. That is unfortunate. So, to recap, if the
It sounds like no new runtime support is necessary if the change is in how the descriptor is generated. The existing Go runtime would continue to work fine, but just expose these optional fields precisely as if they were one-field oneofs (since that is what the descriptor will look like that is fed to In that case, I was wrong about where the impact will be. I think the |
First we prepend just a
We don't want the generated code to emit a generated API for the synthetic oneof. The oneof is visible to reflection, but it should not be visible to generated code. For a user who is writing a |
I guess that means that the generated code will have to use a field mask to know which optional fields were set? More importantly, in order to distinguish synthetic oneofs from user-written ones, I think that means they must have a new option, to tell plugins/generators how to treat it, right? Or is this how all single-field oneofs are to be treated during code generation? (That seems like it would be an incompatible change in the generated code.) |
I see that there is indeed a new option: |
Out of curiosity, @haberman, do you know why it was chosen to be an attribute of the field instead, for example, a new field in |
Each language will do whatever it does for proto2
The void GenerateCodeForField(const FieldDescriptor* field) {
if (!field->is_repeated() &&
(field->file()->syntax() == SYNTAX_PROTO2) ||
field->options().proto3_optional())) {
// Field has presence (optional or oneof).
}
} That code is complicated and brittle, and will have to be updated if we ever created anything like a "proto4". Instead we want code generators to use functions that describe the semantic meaning of each field: void GenerateCodeForField(const FieldDescriptor* field) {
if (field->has_presence()) {
// Field has presence (optional or oneof).
}
} This code is more robust, because it is not directly paying attention to the difference between proto2 and proto3. Whoever owns the descriptor API can define the Also, field options are generally something that users can set directly in .proto files. But we don't want to let users write this: syntax = "proto3"
message Foo {
int32 bar [proto3_optional = true];
} |
Yeah, after I left that comment, I realized this was better and my comment/question was misguided. It just goes against the precedent of Anyhow, I think I've got enough to start. @dsnet, how often are generated Go files kept in sync with the proto sources in the main protobuf repo? In other words, how long do you think it will be before the Go file is updated to reflect latest changes to the proto source. |
I hope to get to it this week. Past few weeks have been notably distracting for me as a number of unexpected issues have required my investigation and time. |
https://go-review.googlesource.com/c/protobuf/+/230698 is the change that will add proto3 optional support to the |
Hi there, I wanted to give you a heads-up that protobuf 3.12 is adding experimental support for
optional
in proto3, with a goal of making it generally available in protobuf 3.13:protocolbuffers/protobuf#1606 (comment)
This may require some changes in protoreflect to fully support. Here is a guide to the changes that are generally required. I'm not super-familiar with protoreflect so I can't make any specific recommendations, but hopefully this doc will help:
https://github.com/protocolbuffers/protobuf/blob/master/docs/implementing_proto3_presence.md
Please let me know if I can answer any questions. Thanks!
The text was updated successfully, but these errors were encountered: