Open tamayika opened 3 months ago
Embedding a proto message like this
type embedMessage struct {
*testpb.TestAllTypes
}
Generates a lot of methods that forward to the generated testpb.TestAllTypes
type. protocmp therefore thinks that this is a proto message (it implements the correct interface). In this case, something equivalent to
func (e *embedMessage) ProtoReflect() protoreflect.ProtoMessage {
return e.TestAllTypes.ProtoReflect()
}
The problem is that protocmp is passing this type to MessageOf
in the first place. The documentation is pretty clear that the type must be a "message type generated by protoc-gen-go and must be a pointer to a named struct type". OTOH, protocmp
has no way of knowing that *embedMessage
is not a proto type, after all *embedMessage
implements the correct interface.
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. Please check the PR. If PR is likely accepted, I will send it to google's gerrit too.
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. I keep this issue open for other users facing the same issue.
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 "google.golang.org/protobuf/proto".Message
.
The *embedMessage
type implements proto.Message
, therefore it's a message. However, a nil *embedMessage
panics when its ProtoReflect
method is called, therefore the panic as observed here. You either need to take care not to pass a nil *embedMessage
to something that expects a message, change it to not panic in this case, or change it to not implement proto.Message
.
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 ProtoReflect()
and works well if and only if there is no other fields.
Embeding proto.Message is useful to extend Message with fields which is not defined in Message but it behaves the same for the fields defined in Message.
Tests in the PR show the problem in current implementatioin.
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?