cmd/protoc-gen-go: treat extensions fields as proto2 in some places
The v1 generator doesn't include a "proto3" tag on extension fields,
even when the field is defined in a proto3 file. Match that behavior for
consistency. We can probably change this later if we want to; it's
unlikely anyone is depending on this behavior.
The v1 generator uses pointer types for extension fields, even when the
field is defined in a proto3 file. (e.g., *FooEnum instead of FooEnum.)
Match this behavior. We can't change this without breaking compatibility
in the generated code.
Change-Id: I4072f3dd1c915bf9ab89f1d5198e0144cb4de20f
Reviewed-on: https://go-review.googlesource.com/c/144282
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index b5b0f5a..8e57ebd 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -620,7 +620,8 @@
goType = "[]" + goType
pointer = false
}
- if field.Desc.Syntax() == protoreflect.Proto3 {
+ // Extension fields always have pointer type, even when defined in a proto3 file.
+ if field.Desc.Syntax() == protoreflect.Proto3 && field.Desc.ExtendedType() == nil {
pointer = false
}
return goType, pointer
@@ -659,7 +660,10 @@
tag = append(tag, "json="+jsonName)
}
// proto3
- if field.Desc.Syntax() == protoreflect.Proto3 {
+ // The previous implementation does not tag extension fields as proto3,
+ // even when the field is defined in a proto3 file. Match that behavior
+ // for consistency.
+ if field.Desc.Syntax() == protoreflect.Proto3 && field.Desc.ExtendedType() == nil {
tag = append(tag, "proto3")
}
// enum