Skip to content

Commit 9b12f36

Browse files
authored
Detect proto.Message types when failing to export a field (#370)
While we do not provide 1st class handling of protobuf messages, we can at least detect whether the type is a proto.Message and refer the user to the correct package to use to handle protobuf messages with cmp.Equal.
1 parent 4dd3d63 commit 9b12f36

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

cmp/options.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,15 @@ func (validator) apply(s *state, vx, vy reflect.Value) {
232232
if t := s.curPath.Index(-2).Type(); t.Name() != "" {
233233
// Named type with unexported fields.
234234
name = fmt.Sprintf("%q.%v", t.PkgPath(), t.Name()) // e.g., "path/to/package".MyType
235-
if _, ok := reflect.New(t).Interface().(error); ok {
235+
isProtoMessage := func(t reflect.Type) bool {
236+
m, ok := reflect.PointerTo(t).MethodByName("ProtoReflect")
237+
return ok && m.Type.NumIn() == 1 && m.Type.NumOut() == 1 &&
238+
m.Type.Out(0).PkgPath() == "google.golang.org/protobuf/reflect/protoreflect" &&
239+
m.Type.Out(0).Name() == "Message"
240+
}
241+
if isProtoMessage(t) {
242+
help = `consider using "google.golang.org/protobuf/testing/protocmp".Transform to compare proto.Message types`
243+
} else if _, ok := reflect.New(t).Interface().(error); ok {
236244
help = "consider using cmpopts.EquateErrors to compare error values"
237245
} else if t.Comparable() {
238246
help = "consider using cmpopts.EquateComparable to compare comparable Go types"

0 commit comments

Comments
 (0)