-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
protocmp: Panic on embed message field comparison #1638
protocmp: Panic on embed message field comparison #1638
Comments
Embedding a proto message like this type embedMessage struct {
*testpb.TestAllTypes
} Generates a lot of methods that forward to the generated
The problem is that protocmp is passing this type to All in all, I don't think this is a problem that can be fixed without severely breaking backwards compatibility. If you must embed a proto, and I don't think embetting is good idea for any type you don't fully control, then you are responsible for making sure that it really behaves like a proto message. |
I think we can implement this feature without losing compatibility. |
Thanks for the PR. IIUC, the PR assumes that there are no implementations of proto.Message that rely on embedding another proto.Message. That's unlikely to exist, but there are a lot of proto users out there and therefore even unlikely things like that happen regularly. I am also not convinced that the additional complexity is worth it: There are number of functions that take a proto.Message, handling this differently in protocmp is bound to cause confusion. I don't think this solution should be accepted. |
I think protocmp is totally broken for proto.Message embed structs and no one is happy with this behavior at the point of comparing of go-cmp's one. |
Keep in mind that your type is implementing proto.Message if you embed a proto message. If you do that and pass it to a function that accepts a proto.Message it's your responsibility to implement it correctly. Granted, it would be nice if that happened automatically, but it's not. I think a fix should be on that level. If that's not possible, so be it. Either way, as I said before, embedding proto messages is not a good idea. I would be very surprised if protocmp is the only sharp corner you'll run into with that approach. |
A protobuf message is any type that implements The For example, you could avoid panicing: type embedMessage struct {
*testpb.TestAllTypes
}
func (m *embedMessage) ProtoReflect() protoreflect.Message {
var p *testpb.TestAllTypes
if m != nil {
p = m.TestAllTypes
}
return p.ProtoReflect()
} Or you could avoid implementing proto.Message: type doNotImplementMessage struct{}
func (doNotImplementMessage) ProtoReflect() protoreflect.Message
type embedMessage struct {
*testpb.TestAllTypes
doNotImplementMessage
} Or, as @znkr suggests, you could avoid embedding altogether, which is probably the simplest solution. |
Yes, I know I can avoid panic by defining |
What version of protobuf and what language are you using?
Version: 1.34.2
What did you do?
Compare embed message field.
What did you expect to see?
No panic.
What did you see instead?
Panic at https://github.com/protocolbuffers/protobuf-go/blob/master/internal/impl/api_export.go#L144 by call method for nil pointer.
Anything else we should know about your project / environment?
The text was updated successfully, but these errors were encountered: