starwing / lua-protobuf

A Lua module to work with Google protobuf
MIT License
1.7k stars 386 forks source link

protobuf3 optional: configurable "optional_" prefix #257

Open iruno opened 7 months ago

iruno commented 7 months ago

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 commented 7 months ago

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

iruno commented 7 months ago

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 commented 7 months ago

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.