Skip to content
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

Detect proto.Message types when failing to export a field #370

Merged
merged 1 commit into from
Jan 14, 2025

Conversation

dsnet
Copy link
Collaborator

@dsnet dsnet commented Jan 14, 2025

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.

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.
@dsnet dsnet requested a review from neild January 14, 2025 17:47
@dsnet
Copy link
Collaborator Author

dsnet commented Jan 14, 2025

Unfortunately, I can't add a test for this as doing so would require a dependency on google.golang.org/protobuf.

However, here's the code in action:

package main

import (
	"github.com/google/go-cmp/cmp"
	"google.golang.org/protobuf/types/descriptorpb"
)

func main() {
	m1 := &descriptorpb.DescriptorProto{}
	cmp.Equal(m1, m1)
}
panic: cannot handle unexported field at {*descriptorpb.DescriptorProto}.state:
		"google.golang.org/protobuf/types/descriptorpb".DescriptorProto
	consider using "google.golang.org/protobuf/testing/protocmp".Transform to compare proto.Message types

@dsnet
Copy link
Collaborator Author

dsnet commented Jan 14, 2025

Note that MethodByName could massively increase a binary size, but cmp is only used for testing, so this isn't a major detriment. Also, golang/go#62257 makes it such that string literals passed to MethodByName are specially handled such that only those particular methods must be linked in, while the entire universe of all methods can be mostly DCE'd.

@dsnet dsnet merged commit 9b12f36 into master Jan 14, 2025
7 checks passed
@dsnet dsnet deleted the warn-proto-message branch January 14, 2025 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants