protobuf-go/cmd/protoc-gen-go/testdata
Joe Tsai acaef6adb7 cmd/protoc-gen-go: avoid referencing remote enum values by name
The Go generator has historically always prefixed an enum value
with the name of the enum type, when it was unnecessary to do so.

For example:
	enum Status {
		STATUS_FAILED = 0;
		STATUS_PASSED = 1;
	}
would be generated as:
	type Status int32
	const (
		Status_STATUS_FAILED Status = 0
		Status_STATUS_PASSED Status = 1
	)

It is common for the enum values to be manually prefixed by the
enum type since protobuf enums use C++ namespace rules where
enum types and enum values are in the same namespace scope.
Thus, having the Go generator add a prefix is redundant.
See https://github.com/golang/protobuf/issues/513.

Some custom Go generators like protoc-gen-gogo allow removing
the prefix with the gogoproto.goproto_enum_prefix feature.
However, this leads to interoperability issues between
protoc-gen-go and protoc-gen-gogo, where the enum value names
cannot be accurately inferred.

Avoid this problem by just hard-coding the enum value number
for values declared in other packages. This provides benefits
in interoperability at the small cost of enum values possibly
being stale if their value were ever changed in a remote package.
However, this would only occur with use of proto2 enums and
default values, which seems to be an exceptionally rare situation.

Before:
	Default_MyMessage_MyField = remotepb.FooEnum_FOO_ENUM
After:
	Default_MyMessage_MyField = remotepb.FooEnum(4) // remotepb.FooEnum_FOO_ENUM

Before:
	func (x *MyMessage) GetField() remotepb.FooEnum {
		...
		return remotepb.FooEnum_FOO_ZERO
	}
After:
	func (x *MyMessage) GetField() remotepb.FooEnum {
		...
		return remotepb.FooEnum(0) // always 0 for proto3 and often 0 for proto2
	}

Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/319649
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-05-24 22:21:56 +00:00
..
annotations all: make .proto file names relative to module root 2020-02-04 23:20:20 +00:00
comments cmd/protoc-gen-go: remove generation of the ExtensionRangeArray method 2021-03-29 20:25:28 +00:00
extensions cmd/protoc-gen-go: remove generation of the ExtensionRangeArray method 2021-03-29 20:25:28 +00:00
fieldnames all: make .proto file names relative to module root 2020-02-04 23:20:20 +00:00
import_public cmd/protoc-gen-go: avoid referencing remote enum values by name 2021-05-24 22:21:56 +00:00
imports all: make .proto file names relative to module root 2020-02-04 23:20:20 +00:00
issue780_oneof_conflict internal/cmd/generate-protos: use module= generator option 2020-03-20 21:35:56 +00:00
nopackage all: make .proto file names relative to module root 2020-02-04 23:20:20 +00:00
proto2 all: make .proto file names relative to module root 2020-02-04 23:20:20 +00:00
proto3 all: make .proto file names relative to module root 2020-02-04 23:20:20 +00:00
gen_test.go all: remove stray "." from license headers 2020-02-20 18:54:38 +00:00
registry_test.go all: remove stray "." from license headers 2020-02-20 18:54:38 +00:00