jhump / protoreflect

Reflection (Rich Descriptors) for Go Protocol Buffers
Apache License 2.0
1.33k stars 170 forks source link

Optional fields are considered oneofs? #619

Closed zchenyu closed 1 month ago

zchenyu commented 1 month ago

If I have an optional field, the fieldDescriptor.GetOneOf() seems to be non-nil with name name:"_my_field". Is this expected?

zchenyu commented 1 month ago

I found this: https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md

To minimize this risk we chose a descriptor representation that is semantically compatible with existing proto3 reflection. Every proto3 optional field is placed into a one-field oneof. We call this a "synthetic" oneof, as it was not present in the source .proto file.

Is there some way to distinguish between these synthetic oneofs vs real ones? Basically I'm trying to loop over all FieldDescriptors in a MessageDescriptor, ignoring ones that are part of a oneof.

zchenyu commented 1 month ago

Oh I just found this: https://pkg.go.dev/github.com/jhump/protoreflect@v1.16.0/desc#OneOfDescriptor.IsSynthetic