Skip to content

Instantly share code, notes, and snippets.

@larryhou
Last active June 11, 2023 06:20
Show Gist options
  • Save larryhou/3e039ba1630b7592cc9a0df622e74691 to your computer and use it in GitHub Desktop.
Save larryhou/3e039ba1630b7592cc9a0df622e74691 to your computer and use it in GitHub Desktop.
Fix protobuf-gen-go repeated enum typename in each case

https://github.com/protocolbuffers/protobuf-go

diff --git a/compiler/protogen/protogen.go b/compiler/protogen/protogen.go
index 431e880..5e6b263 100644
--- a/compiler/protogen/protogen.go
+++ b/compiler/protogen/protogen.go
@@ -550,11 +550,14 @@ func newEnumValue(gen *Plugin, f *File, message *Message, enum *Enum, desc proto
 	// An enum value contained in a message is: MessageName_ValueName
 	//
 	// For historical reasons, enum value names are not camel-cased.
-	parentIdent := enum.GoIdent
+	var parentIdent *GoIdent
 	if message != nil {
-		parentIdent = message.GoIdent
+		parentIdent = &message.GoIdent
+	}
+	name := string(desc.Name())
+	if parentIdent != nil {
+		name = parentIdent.GoName + "_" + name
 	}
-	name := parentIdent.GoName + "_" + string(desc.Name())
 	loc := enum.Location.appendPath(genid.EnumDescriptorProto_Value_field_number, desc.Index())
 	return &EnumValue{
 		Desc:     desc,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment