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

protobuf3 optional: configurable "optional_" prefix #257

Open
iruno opened this issue Dec 5, 2023 · 3 comments
Open

protobuf3 optional: configurable "optional_" prefix #257

iruno opened this issue Dec 5, 2023 · 3 comments

Comments

@iruno
Copy link

iruno commented Dec 5, 2023

We are currently migrating from 0.3.3 and 0.5.0 and see optional label behavior is changed.

scenario: decode binary data and serialize it using json

version 0.3.3:
{"eventId":"175d252a-fd72-4e01-8260-88929a9b2aa9","eventTime":{"timestampUTC":1413525601} ...

version 0.5.0
{"eventId":"175d252a-fd72-4e01-8260-88929a9b2aa9","eventTime":{"timestampUTC":1413525601,"optional_timestampUTC":"timestampUTC"},"optional_eventTime":"eventTime"} ...

Is it possble to configure module to avoid "optional_" fields generation?

Thanks!

@starwing
Copy link
Owner

starwing commented Dec 6, 2023

to check with official protoc implement, the "optional oneof name" is "_field", so change name to fit it.

@iruno
Copy link
Author

iruno commented Dec 6, 2023

I see prefix is changed:

{"eventId":"175d252a-fd72-4e01-8260-88929a9b2aa9","_eventId":"eventId", "eventTime":{"timestampUTC":1413525601,"_timestampUTC":"timestampUTC"},"_eventTime":"eventTime"}

But I want to get the same result I had in 0.3.3. I do not want to have "_"-prefixed extra fields at all.
When I serialize the same object to json in java using com.google.protobuf.util.JsonFormat.Printer, I have no "_"-prefixed fileds generated.

Is it possible to implement it some way?
May be new option like "enable_proto3_optional_fields_generation=false"?


UPD: 'oneof' is not used in my proro file at all.

...

message ZonedTime {

  // Seconds since 1970-01-01 midnight UTC.  For \"2022-03-22 16:47:37 (MSK)\":
  optional int32 timestampUTC = 516260016;

  // Optional.  TZ name
  optional string zone = 3744684;

}

message Event {

  // The identifier of the notification.
  optional string eventId = 302760621;

  optional ZonedTime eventTime = 31415431;
}

@starwing
Copy link
Owner

starwing commented Dec 6, 2023

Unfortunately it may difficult to do. The optional in proto3 is just a syntax sugar of single field oneof, and there is no way to distinguish it with single field oneof in FileDescriptor. In luapb, all oneof has a field set to indicate which field is setting. I cannot just ignore it for optional field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants