Просмотр исходного кода

cmd/protoc-gen-go: refactor logic to be more compartmentalized

This CL makes no feature changes except to move code around.
The only change to the actual generated code is the placement of
the default constants and variables. They move because the new logic
generates all methods together, while previously the constants
were interspersed in-between.

Change-Id: I45932d5aeec5ba45180fb255ea17898beb6c3bd2
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/186878
Reviewed-by: Herbie Ong <herbie@google.com>
Joe Tsai 6 лет назад
Родитель
Сommit
fb3ff727f0

+ 35 - 0
cmd/protoc-gen-go/internal_gengo/api_flags.go

@@ -0,0 +1,35 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package internal_gengo
+
+import (
+	"sync"
+
+	"google.golang.org/protobuf/compiler/protogen"
+)
+
+// messageAPIFlags provides flags that control the generated API.
+type messageAPIFlags struct {
+	WeakMapField bool
+}
+
+var messageAPIFlagsCache sync.Map
+
+func loadMessageAPIFlags(message *protogen.Message) messageAPIFlags {
+	if flags, ok := messageAPIFlagsCache.Load(message); ok {
+		return flags.(messageAPIFlags)
+	}
+
+	var flags messageAPIFlags
+	for _, field := range message.Fields {
+		if field.Desc.IsWeak() {
+			flags.WeakMapField = true
+			break
+		}
+	}
+
+	messageAPIFlagsCache.Store(message, flags)
+	return flags
+}

+ 219 - 216
cmd/protoc-gen-go/internal_gengo/main.go

@@ -52,9 +52,9 @@ const (
 	// XXX_WellKnownType methods on well-known types.
 	generateWKTMarkerMethods = false
 
-	// generateMessateStateFields specifies whether to generate an unexported
+	// generateMessageStateFields specifies whether to generate an unexported
 	// protoimpl.MessageState as the first field.
-	generateMessateStateFields = true
+	generateMessageStateFields = true
 
 	// generateNoUnkeyedLiteralFields specifies whether to generate
 	// the XXX_NoUnkeyedLiteral field.
@@ -98,6 +98,28 @@ type goImportPath interface {
 	Ident(string) protogen.GoIdent
 }
 
+// Names of messages and enums for which we will generate XXX_WellKnownType methods.
+var wellKnownTypes = map[protoreflect.FullName]bool{
+	"google.protobuf.Any":       true,
+	"google.protobuf.Duration":  true,
+	"google.protobuf.Empty":     true,
+	"google.protobuf.Struct":    true,
+	"google.protobuf.Timestamp": true,
+
+	"google.protobuf.BoolValue":   true,
+	"google.protobuf.BytesValue":  true,
+	"google.protobuf.DoubleValue": true,
+	"google.protobuf.FloatValue":  true,
+	"google.protobuf.Int32Value":  true,
+	"google.protobuf.Int64Value":  true,
+	"google.protobuf.ListValue":   true,
+	"google.protobuf.NullValue":   true,
+	"google.protobuf.StringValue": true,
+	"google.protobuf.UInt32Value": true,
+	"google.protobuf.UInt64Value": true,
+	"google.protobuf.Value":       true,
+}
+
 type fileInfo struct {
 	*protogen.File
 
@@ -353,7 +375,7 @@ func genEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum
 	g.P("}")
 	g.P()
 
-	genReflectEnum(gen, g, f, enum)
+	genEnumReflectMethods(gen, g, f, enum)
 
 	// UnmarshalJSON method.
 	if generateEnumJSONMethods && enum.Desc.Syntax() == protoreflect.Proto2 {
@@ -382,7 +404,11 @@ func genEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum
 		g.P()
 	}
 
-	genWellKnownType(g, "", enum.GoIdent, enum.Desc)
+	// XXX_WellKnownType method.
+	if generateWKTMarkerMethods && wellKnownTypes[enum.Desc.FullName()] {
+		g.P("func (", enum.GoIdent, `) XXX_WellKnownType() string { return "`, enum.Desc.Name(), `" }`)
+		g.P()
+	}
 }
 
 // enumLegacyName returns the name used by the v1 proto package.
@@ -413,64 +439,78 @@ func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, me
 	}
 	g.Annotate(message.GoIdent.GoName, message.Location)
 	g.P("type ", message.GoIdent, " struct {")
+	genMessageFields(g, f, message)
+	g.P("}")
+	g.P()
+
+	genDefaultConsts(g, message)
+	genMessageMethods(gen, g, f, message)
+	genOneofWrapperTypes(gen, g, f, message)
+}
+
+func genMessageFields(g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
 	sf := f.allMessageFieldsByPtr[message]
-	if generateMessateStateFields {
+	if generateMessageStateFields {
 		g.P("state ", protoimplPackage.Ident("MessageState"))
 		sf.append("state")
 	}
-	hasWeak := false
 	for _, field := range message.Fields {
-		if field.Oneof != nil {
-			// It would be a bit simpler to iterate over the oneofs below,
-			// but generating the field here keeps the contents of the Go
-			// struct in the same order as the contents of the source
-			// .proto file.
-			oneof := field.Oneof
-			if field != oneof.Fields[0] {
-				continue // already generated oneof field for first entry
-			}
-			if g.PrintLeadingComments(oneof.Location) {
-				g.P("//")
-			}
-			g.P("// Types that are valid to be assigned to ", oneofFieldName(oneof), ":")
-			for _, field := range oneof.Fields {
-				g.PrintLeadingComments(field.Location)
-				g.P("//\t*", fieldOneofType(field))
-			}
-			g.Annotate(message.GoIdent.GoName+"."+oneofFieldName(oneof), oneof.Location)
-			g.P(oneofFieldName(oneof), " ", oneofInterfaceName(oneof), " `protobuf_oneof:\"", oneof.Desc.Name(), "\"`")
-			sf.append(oneofFieldName(oneof))
-			continue
-		}
-		g.PrintLeadingComments(field.Location)
-		goType, pointer := fieldGoType(g, field)
-		if pointer {
-			goType = "*" + goType
+		genMessageField(g, message, field, sf)
+	}
+	genMessageInternalFields(g, message, sf)
+}
+
+func genMessageField(g *protogen.GeneratedFile, message *protogen.Message, field *protogen.Field, sf *structFields) {
+	if oneof := field.Oneof; oneof != nil {
+		// It would be a bit simpler to iterate over the oneofs below,
+		// but generating the field here keeps the contents of the Go
+		// struct in the same order as the contents of the source
+		// .proto file.
+		if oneof.Fields[0] != field {
+			return // only generate for first appearance
 		}
-		tags := []string{
-			fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)),
-			fmt.Sprintf("json:%q", fieldJSONTag(field)),
+		if g.PrintLeadingComments(oneof.Location) {
+			g.P("//")
 		}
-		if field.Desc.IsMap() {
-			key := field.Message.Fields[0]
-			val := field.Message.Fields[1]
-			tags = append(tags,
-				fmt.Sprintf("protobuf_key:%q", fieldProtobufTag(key)),
-				fmt.Sprintf("protobuf_val:%q", fieldProtobufTag(val)),
-			)
+		g.P("// Types that are valid to be assigned to ", oneof.GoName, ":")
+		for _, field := range oneof.Fields {
+			g.PrintLeadingComments(field.Location)
+			g.P("//\t*", fieldOneofType(field))
 		}
+		g.Annotate(message.GoIdent.GoName+"."+oneof.GoName, oneof.Location)
+		g.P(oneof.GoName, " ", oneofInterfaceName(oneof), " `protobuf_oneof:\"", oneof.Desc.Name(), "\"`")
+		sf.append(oneof.GoName)
+		return
+	}
+	g.PrintLeadingComments(field.Location)
+	goType, pointer := fieldGoType(g, field)
+	if pointer {
+		goType = "*" + goType
+	}
+	tags := []string{
+		fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)),
+		fmt.Sprintf("json:%q", fieldJSONTag(field)),
+	}
+	if field.Desc.IsMap() {
+		key := field.Message.Fields[0]
+		val := field.Message.Fields[1]
+		tags = append(tags,
+			fmt.Sprintf("protobuf_key:%q", fieldProtobufTag(key)),
+			fmt.Sprintf("protobuf_val:%q", fieldProtobufTag(val)),
+		)
+	}
 
-		name := field.GoName
-		if field.Desc.IsWeak() {
-			hasWeak = true
-			name = "XXX_weak_" + name
-		}
-		g.Annotate(message.GoIdent.GoName+"."+name, field.Location)
-		g.P(name, " ", goType, " `", strings.Join(tags, " "), "`",
-			deprecationComment(field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated()))
-		sf.append(field.GoName)
+	name := field.GoName
+	if field.Desc.IsWeak() {
+		name = "XXX_weak_" + name
 	}
+	g.Annotate(message.GoIdent.GoName+"."+name, field.Location)
+	g.P(name, " ", goType, " `", strings.Join(tags, " "), "`",
+		deprecationComment(field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated()))
+	sf.append(field.GoName)
+}
 
+func genMessageInternalFields(g *protogen.GeneratedFile, message *protogen.Message, sf *structFields) {
 	if generateNoUnkeyedLiteralFields {
 		g.P("XXX_NoUnkeyedLiteral", " struct{} `json:\"-\"`")
 		sf.append("XXX_NoUnkeyedLiteral")
@@ -482,7 +522,7 @@ func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, me
 		g.P("sizeCache", " ", protoimplPackage.Ident("SizeCache"))
 		sf.append("sizeCache")
 	}
-	if hasWeak {
+	if loadMessageAPIFlags(message).WeakMapField {
 		g.P("XXX_weak", " ", protoimplPackage.Ident("WeakFields"), " `json:\"-\"`")
 		sf.append("XXX_weak")
 	}
@@ -502,24 +542,86 @@ func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, me
 			sf.append("extensionFields")
 		}
 	}
-	g.P("}")
+}
+
+// genDefaultConsts generates consts and vars holding the default
+// values of fields.
+func genDefaultConsts(g *protogen.GeneratedFile, message *protogen.Message) {
+	for _, field := range message.Fields {
+		if !field.Desc.HasDefault() {
+			continue
+		}
+		defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName
+		def := field.Desc.Default()
+		switch field.Desc.Kind() {
+		case protoreflect.StringKind:
+			g.P("const ", defVarName, " string = ", strconv.Quote(def.String()))
+		case protoreflect.BytesKind:
+			g.P("var ", defVarName, " []byte = []byte(", strconv.Quote(string(def.Bytes())), ")")
+		case protoreflect.EnumKind:
+			evalueDesc := field.Desc.DefaultEnumValue()
+			enum := field.Enum
+			evalue := enum.Values[evalueDesc.Index()]
+			g.P("const ", defVarName, " ", field.Enum.GoIdent, " = ", evalue.GoIdent)
+		case protoreflect.FloatKind, protoreflect.DoubleKind:
+			// Floating point numbers need extra handling for -Inf/Inf/NaN.
+			f := field.Desc.Default().Float()
+			goType := "float64"
+			if field.Desc.Kind() == protoreflect.FloatKind {
+				goType = "float32"
+			}
+			// funcCall returns a call to a function in the math package,
+			// possibly converting the result to float32.
+			funcCall := func(fn, param string) string {
+				s := g.QualifiedGoIdent(mathPackage.Ident(fn)) + param
+				if goType != "float64" {
+					s = goType + "(" + s + ")"
+				}
+				return s
+			}
+			switch {
+			case math.IsInf(f, -1):
+				g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(-1)"))
+			case math.IsInf(f, 1):
+				g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(1)"))
+			case math.IsNaN(f):
+				g.P("var ", defVarName, " ", goType, " = ", funcCall("NaN", "()"))
+			default:
+				g.P("const ", defVarName, " ", goType, " = ", field.Desc.Default().Interface())
+			}
+		default:
+			goType, _ := fieldGoType(g, field)
+			g.P("const ", defVarName, " ", goType, " = ", def.Interface())
+		}
+	}
 	g.P()
+}
+
+func genMessageMethods(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
+	genMessageBaseMethods(gen, g, f, message)
+	genMessageGetterMethods(gen, g, f, message)
+	genMessageSetterMethods(gen, g, f, message)
+}
 
+func genMessageBaseMethods(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
 	// Reset method.
 	g.P("func (x *", message.GoIdent, ") Reset() {")
 	g.P("*x = ", message.GoIdent, "{}")
 	g.P("}")
 	g.P()
+
 	// String method.
 	g.P("func (x *", message.GoIdent, ") String() string {")
 	g.P("return ", protoimplPackage.Ident("X"), ".MessageStringOf(x)")
 	g.P("}")
 	g.P()
+
 	// ProtoMessage method.
 	g.P("func (*", message.GoIdent, ") ProtoMessage() {}")
 	g.P()
 
-	genReflectMessage(gen, g, f, message)
+	// ProtoReflect method.
+	genMessageReflectMethods(gen, g, f, message)
 
 	// Descriptor method.
 	if generateRawDescMethods {
@@ -554,70 +656,47 @@ func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, me
 		}
 	}
 
-	genWellKnownType(g, "*", message.GoIdent, message.Desc)
+	// XXX_OneofWrappers method.
+	if generateOneofWrapperMethods && len(message.Oneofs) > 0 {
+		idx := f.allMessagesByPtr[message]
+		typesVar := messageTypesVarName(f)
+		g.P("// XXX_OneofWrappers is for the internal use of the proto package.")
+		g.P("func (*", message.GoIdent.GoName, ") XXX_OneofWrappers() []interface{} {")
+		g.P("return ", typesVar, "[", idx, "].OneofWrappers")
+		g.P("}")
+		g.P()
+	}
 
-	// Constants and vars holding the default values of fields.
-	for _, field := range message.Fields {
-		if !field.Desc.HasDefault() {
-			continue
-		}
-		defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName
-		def := field.Desc.Default()
-		switch field.Desc.Kind() {
-		case protoreflect.StringKind:
-			g.P("const ", defVarName, " string = ", strconv.Quote(def.String()))
-		case protoreflect.BytesKind:
-			g.P("var ", defVarName, " []byte = []byte(", strconv.Quote(string(def.Bytes())), ")")
-		case protoreflect.EnumKind:
-			evalueDesc := field.Desc.DefaultEnumValue()
-			enum := field.Enum
-			evalue := enum.Values[evalueDesc.Index()]
-			g.P("const ", defVarName, " ", field.Enum.GoIdent, " = ", evalue.GoIdent)
-		case protoreflect.FloatKind, protoreflect.DoubleKind:
-			// Floating point numbers need extra handling for -Inf/Inf/NaN.
-			f := field.Desc.Default().Float()
-			goType := "float64"
-			if field.Desc.Kind() == protoreflect.FloatKind {
-				goType = "float32"
-			}
-			// funcCall returns a call to a function in the math package,
-			// possibly converting the result to float32.
-			funcCall := func(fn, param string) string {
-				s := g.QualifiedGoIdent(mathPackage.Ident(fn)) + param
-				if goType != "float64" {
-					s = goType + "(" + s + ")"
-				}
-				return s
-			}
-			switch {
-			case math.IsInf(f, -1):
-				g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(-1)"))
-			case math.IsInf(f, 1):
-				g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(1)"))
-			case math.IsNaN(f):
-				g.P("var ", defVarName, " ", goType, " = ", funcCall("NaN", "()"))
-			default:
-				g.P("const ", defVarName, " ", goType, " = ", field.Desc.Default().Interface())
-			}
-		default:
-			goType, _ := fieldGoType(g, field)
-			g.P("const ", defVarName, " ", goType, " = ", def.Interface())
-		}
+	// XXX_WellKnownType method.
+	if generateWKTMarkerMethods && wellKnownTypes[message.Desc.FullName()] {
+		g.P("func (*", message.GoIdent, `) XXX_WellKnownType() string { return "`, message.Desc.Name(), `" }`)
+		g.P()
 	}
-	g.P()
+}
 
-	// Getter methods.
+func genMessageGetterMethods(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
 	for _, field := range message.Fields {
-		if isFirstOneofField(field) {
-			genOneofGetter(gen, g, f, message, field.Oneof)
+		// Getter for parent oneof.
+		if oneof := field.Oneof; oneof != nil && oneof.Fields[0] == field {
+			g.Annotate(message.GoIdent.GoName+".Get"+oneof.GoName, oneof.Location)
+			g.P("func (m *", message.GoIdent.GoName, ") Get", oneof.GoName, "() ", oneofInterfaceName(oneof), " {")
+			g.P("if m != nil {")
+			g.P("return m.", oneof.GoName)
+			g.P("}")
+			g.P("return nil")
+			g.P("}")
+			g.P()
 		}
+
+		// Getter for message field.
 		goType, pointer := fieldGoType(g, field)
 		defaultValue := fieldDefaultValue(g, message, field)
 		if field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated() {
 			g.P(deprecationComment(true))
 		}
 		g.Annotate(message.GoIdent.GoName+".Get"+field.GoName, field.Location)
-		if field.Desc.IsWeak() {
+		switch {
+		case field.Desc.IsWeak():
 			g.P("func (x *", message.GoIdent, ") Get", field.GoName, "() ", protoifacePackage.Ident("MessageV1"), "{")
 			g.P("if x != nil {")
 			g.P("v := x.XXX_weak[", field.Desc.Number(), "]")
@@ -628,14 +707,15 @@ func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, me
 			g.P("}")
 			g.P("return ", protoimplPackage.Ident("X"), ".WeakNil(", strconv.Quote(string(field.Message.Desc.FullName())), ")")
 			g.P("}")
-			continue
-		}
-		g.P("func (x *", message.GoIdent, ") Get", field.GoName, "() ", goType, " {")
-		if field.Oneof != nil {
+		case field.Oneof != nil:
+			g.P("func (x *", message.GoIdent, ") Get", field.GoName, "() ", goType, " {")
 			g.P("if x, ok := x.Get", field.Oneof.GoName, "().(*", fieldOneofType(field), "); ok {")
 			g.P("return x.", field.GoName)
 			g.P("}")
-		} else {
+			g.P("return ", defaultValue)
+			g.P("}")
+		default:
+			g.P("func (x *", message.GoIdent, ") Get", field.GoName, "() ", goType, " {")
 			if field.Desc.Syntax() == protoreflect.Proto3 || defaultValue == "nil" {
 				g.P("if x != nil {")
 			} else {
@@ -647,13 +727,14 @@ func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, me
 			}
 			g.P("return ", star, " x.", field.GoName)
 			g.P("}")
+			g.P("return ", defaultValue)
+			g.P("}")
 		}
-		g.P("return ", defaultValue)
-		g.P("}")
 		g.P()
 	}
+}
 
-	// Setter methods.
+func genMessageSetterMethods(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
 	for _, field := range message.Fields {
 		if field.Desc.IsWeak() {
 			g.Annotate(message.GoIdent.GoName+".Set"+field.GoName, field.Location)
@@ -671,11 +752,6 @@ func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, me
 			g.P()
 		}
 	}
-
-	// Oneof wrapper types.
-	if len(message.Oneofs) > 0 {
-		genOneofWrappers(gen, g, f, message)
-	}
 }
 
 // fieldGoType returns the Go type used for a field.
@@ -826,106 +902,33 @@ func deprecationComment(deprecated bool) string {
 	return "// Deprecated: Do not use."
 }
 
-func genWellKnownType(g *protogen.GeneratedFile, ptr string, ident protogen.GoIdent, desc protoreflect.Descriptor) {
-	if generateWKTMarkerMethods && wellKnownTypes[desc.FullName()] {
-		g.P("func (", ptr, ident, `) XXX_WellKnownType() string { return "`, desc.Name(), `" }`)
-		g.P()
-	}
-}
-
-// Names of messages and enums for which we will generate XXX_WellKnownType methods.
-var wellKnownTypes = map[protoreflect.FullName]bool{
-	"google.protobuf.Any":       true,
-	"google.protobuf.Duration":  true,
-	"google.protobuf.Empty":     true,
-	"google.protobuf.Struct":    true,
-	"google.protobuf.Timestamp": true,
-
-	"google.protobuf.BoolValue":   true,
-	"google.protobuf.BytesValue":  true,
-	"google.protobuf.DoubleValue": true,
-	"google.protobuf.FloatValue":  true,
-	"google.protobuf.Int32Value":  true,
-	"google.protobuf.Int64Value":  true,
-	"google.protobuf.ListValue":   true,
-	"google.protobuf.NullValue":   true,
-	"google.protobuf.StringValue": true,
-	"google.protobuf.UInt32Value": true,
-	"google.protobuf.UInt64Value": true,
-	"google.protobuf.Value":       true,
-}
-
-// genOneofGetter generate a Get method for a oneof.
-func genOneofGetter(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) {
-	g.Annotate(message.GoIdent.GoName+".Get"+oneof.GoName, oneof.Location)
-	g.P("func (m *", message.GoIdent.GoName, ") Get", oneof.GoName, "() ", oneofInterfaceName(oneof), " {")
-	g.P("if m != nil {")
-	g.P("return m.", oneofFieldName(oneof))
-	g.P("}")
-	g.P("return nil")
-	g.P("}")
-	g.P()
-}
-
-// genOneofWrappers generates the oneof wrapper types and associates the types
-// with the parent message type.
-func genOneofWrappers(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
-	idx := f.allMessagesByPtr[message]
-	typesVar := messageTypesVarName(f)
-
-	// Associate the wrapper types through a XXX_OneofWrappers method.
-	if generateOneofWrapperMethods {
-		g.P("// XXX_OneofWrappers is for the internal use of the proto package.")
-		g.P("func (*", message.GoIdent.GoName, ") XXX_OneofWrappers() []interface{} {")
-		g.P("return ", typesVar, "[", idx, "].OneofWrappers")
-		g.P("}")
-		g.P()
-	}
-
-	// Generate the oneof wrapper types.
+// genOneofWrapperTypes generates the oneof wrapper types and
+// associates the types with the parent message type.
+func genOneofWrapperTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
 	for _, oneof := range message.Oneofs {
-		genOneofTypes(gen, g, f, message, oneof)
-	}
-}
-
-// genOneofTypes generates the interface type used for a oneof field,
-// and the wrapper types that satisfy that interface.
-func genOneofTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) {
-	ifName := oneofInterfaceName(oneof)
-	g.P("type ", ifName, " interface {")
-	g.P(ifName, "()")
-	g.P("}")
-	g.P()
-	for _, field := range oneof.Fields {
-		name := fieldOneofType(field)
-		g.Annotate(name.GoName, field.Location)
-		g.Annotate(name.GoName+"."+field.GoName, field.Location)
-		g.P("type ", name, " struct {")
-		goType, _ := fieldGoType(g, field)
-		tags := []string{
-			fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)),
-		}
-		g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`")
+		ifName := oneofInterfaceName(oneof)
+		g.P("type ", ifName, " interface {")
+		g.P(ifName, "()")
 		g.P("}")
 		g.P()
+		for _, field := range oneof.Fields {
+			name := fieldOneofType(field)
+			g.Annotate(name.GoName, field.Location)
+			g.Annotate(name.GoName+"."+field.GoName, field.Location)
+			g.P("type ", name, " struct {")
+			goType, _ := fieldGoType(g, field)
+			tags := []string{
+				fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)),
+			}
+			g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`")
+			g.P("}")
+			g.P()
+		}
+		for _, field := range oneof.Fields {
+			g.P("func (*", fieldOneofType(field), ") ", ifName, "() {}")
+			g.P()
+		}
 	}
-	for _, field := range oneof.Fields {
-		g.P("func (*", fieldOneofType(field), ") ", ifName, "() {}")
-		g.P()
-	}
-}
-
-// isFirstOneofField reports whether this is the first field in a oneof.
-func isFirstOneofField(field *protogen.Field) bool {
-	return field.Oneof != nil && field.Oneof.Fields[0] == field
-}
-
-// oneofFieldName returns the name of the struct field holding the oneof value.
-//
-// This function is trivial, but pulling out the name like this makes it easier
-// to experiment with alternative oneof implementations.
-func oneofFieldName(oneof *protogen.Oneof) string {
-	return oneof.GoName
 }
 
 // oneofInterfaceName returns the name of the interface type implemented by

+ 3 - 3
cmd/protoc-gen-go/internal_gengo/reflect.go

@@ -272,7 +272,7 @@ func genFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileI
 	}
 }
 
-func genReflectEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum *protogen.Enum) {
+func genEnumReflectMethods(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum *protogen.Enum) {
 	idx := f.allEnumsByPtr[enum]
 	typesVar := enumTypesVarName(f)
 
@@ -295,14 +295,14 @@ func genReflectEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo
 	g.P()
 }
 
-func genReflectMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
+func genMessageReflectMethods(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
 	idx := f.allMessagesByPtr[message]
 	typesVar := messageTypesVarName(f)
 
 	// ProtoReflect method.
 	g.P("func (x *", message.GoIdent, ") ProtoReflect() ", protoreflectPackage.Ident("Message"), " {")
 	g.P("mi := &", typesVar, "[", idx, "]")
-	if generateMessateStateFields {
+	if generateMessageStateFields {
 		g.P("if ", protoimplPackage.Ident("UnsafeEnabled"), " && x != nil {")
 		g.P("ms := ", protoimplPackage.Ident("X"), ".MessageStateOf(", protoimplPackage.Ident("Pointer"), "(x))")
 		g.P("if ms.LoadMessageInfo() == nil {")

+ 5 - 5
cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go

@@ -194,6 +194,11 @@ type M struct {
 	extensionFields protoimpl.ExtensionFields
 }
 
+const Default_M_S string = "default"
+
+var Default_M_B []byte = []byte("default")
+var Default_M_F float64 = math.NaN()
+
 func (x *M) Reset() {
 	*x = M{}
 }
@@ -230,11 +235,6 @@ func (*M) ExtensionRangeArray() []protoiface.ExtensionRangeV1 {
 	return extRange_M
 }
 
-const Default_M_S string = "default"
-
-var Default_M_B []byte = []byte("default")
-var Default_M_F float64 = math.NaN()
-
 func (x *M) GetM2() *M2 {
 	if x != nil {
 		return x.M2

+ 2 - 2
cmd/protoc-gen-go/testdata/nopackage/nopackage.pb.go

@@ -77,6 +77,8 @@ type Message struct {
 	unknownFields protoimpl.UnknownFields
 }
 
+const Default_Message_EnumField Enum = Enum_ZERO
+
 func (x *Message) Reset() {
 	*x = Message{}
 }
@@ -104,8 +106,6 @@ func (*Message) Descriptor() ([]byte, []int) {
 	return file_nopackage_nopackage_proto_rawDescGZIP(), []int{0}
 }
 
-const Default_Message_EnumField Enum = Enum_ZERO
-
 func (x *Message) GetStringField() string {
 	if x != nil && x.StringField != nil {
 		return *x.StringField

+ 3 - 3
cmd/protoc-gen-go/testdata/proto2/enum.pb.go

@@ -345,6 +345,9 @@ type EnumContainerMessage1 struct {
 	unknownFields     protoimpl.UnknownFields
 }
 
+const Default_EnumContainerMessage1_DefaultDuplicate1 EnumType2 = EnumType2_duplicate1
+const Default_EnumContainerMessage1_DefaultDuplicate2 EnumType2 = EnumType2_duplicate2
+
 func (x *EnumContainerMessage1) Reset() {
 	*x = EnumContainerMessage1{}
 }
@@ -372,9 +375,6 @@ func (*EnumContainerMessage1) Descriptor() ([]byte, []int) {
 	return file_proto2_enum_proto_rawDescGZIP(), []int{0}
 }
 
-const Default_EnumContainerMessage1_DefaultDuplicate1 EnumType2 = EnumType2_duplicate1
-const Default_EnumContainerMessage1_DefaultDuplicate2 EnumType2 = EnumType2_duplicate2
-
 func (x *EnumContainerMessage1) GetDefaultDuplicate1() EnumType2 {
 	if x != nil && x.DefaultDuplicate1 != nil {
 		return *x.DefaultDuplicate1

+ 27 - 27
cmd/protoc-gen-go/testdata/proto2/fields.pb.go

@@ -185,33 +185,6 @@ type FieldTestMessage struct {
 	unknownFields protoimpl.UnknownFields
 }
 
-func (x *FieldTestMessage) Reset() {
-	*x = FieldTestMessage{}
-}
-
-func (x *FieldTestMessage) String() string {
-	return protoimpl.X.MessageStringOf(x)
-}
-
-func (*FieldTestMessage) ProtoMessage() {}
-
-func (x *FieldTestMessage) ProtoReflect() protoreflect.Message {
-	mi := &file_proto2_fields_proto_msgTypes[0]
-	if protoimpl.UnsafeEnabled && x != nil {
-		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
-		if ms.LoadMessageInfo() == nil {
-			ms.StoreMessageInfo(mi)
-		}
-		return ms
-	}
-	return mi.MessageOf(x)
-}
-
-// Deprecated: Use FieldTestMessage.ProtoReflect.Descriptor instead.
-func (*FieldTestMessage) Descriptor() ([]byte, []int) {
-	return file_proto2_fields_proto_rawDescGZIP(), []int{0}
-}
-
 const Default_FieldTestMessage_DefaultBool bool = true
 const Default_FieldTestMessage_DefaultEnum FieldTestMessage_Enum = FieldTestMessage_ONE
 const Default_FieldTestMessage_DefaultInt32 int32 = 1
@@ -240,6 +213,33 @@ var Default_FieldTestMessage_DefaultDoubleNeginf float64 = math.Inf(-1)
 var Default_FieldTestMessage_DefaultDoublePosinf float64 = math.Inf(1)
 var Default_FieldTestMessage_DefaultDoubleNan float64 = math.NaN()
 
+func (x *FieldTestMessage) Reset() {
+	*x = FieldTestMessage{}
+}
+
+func (x *FieldTestMessage) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*FieldTestMessage) ProtoMessage() {}
+
+func (x *FieldTestMessage) ProtoReflect() protoreflect.Message {
+	mi := &file_proto2_fields_proto_msgTypes[0]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use FieldTestMessage.ProtoReflect.Descriptor instead.
+func (*FieldTestMessage) Descriptor() ([]byte, []int) {
+	return file_proto2_fields_proto_rawDescGZIP(), []int{0}
+}
+
 func (x *FieldTestMessage) GetOptionalBool() bool {
 	if x != nil && x.OptionalBool != nil {
 		return *x.OptionalBool

+ 35 - 35
internal/testprotos/benchmarks/datasets/google_message1/proto2/benchmark_message1_proto2.pb.go

@@ -64,6 +64,31 @@ type GoogleMessage1 struct {
 	unknownFields protoimpl.UnknownFields
 }
 
+const Default_GoogleMessage1_Field80 bool = false
+const Default_GoogleMessage1_Field81 bool = true
+const Default_GoogleMessage1_Field6 int32 = 0
+const Default_GoogleMessage1_Field59 bool = false
+const Default_GoogleMessage1_Field130 int32 = 0
+const Default_GoogleMessage1_Field12 bool = true
+const Default_GoogleMessage1_Field17 bool = true
+const Default_GoogleMessage1_Field13 bool = true
+const Default_GoogleMessage1_Field14 bool = true
+const Default_GoogleMessage1_Field104 int32 = 0
+const Default_GoogleMessage1_Field100 int32 = 0
+const Default_GoogleMessage1_Field101 int32 = 0
+const Default_GoogleMessage1_Field29 int32 = 0
+const Default_GoogleMessage1_Field30 bool = false
+const Default_GoogleMessage1_Field60 int32 = -1
+const Default_GoogleMessage1_Field271 int32 = -1
+const Default_GoogleMessage1_Field272 int32 = -1
+const Default_GoogleMessage1_Field23 int32 = 0
+const Default_GoogleMessage1_Field24 bool = false
+const Default_GoogleMessage1_Field25 int32 = 0
+const Default_GoogleMessage1_Field67 int32 = 0
+const Default_GoogleMessage1_Field128 int32 = 0
+const Default_GoogleMessage1_Field129 string = "xxxxxxxxxxxxxxxxxxxxx"
+const Default_GoogleMessage1_Field131 int32 = 0
+
 func (x *GoogleMessage1) Reset() {
 	*x = GoogleMessage1{}
 }
@@ -91,31 +116,6 @@ func (*GoogleMessage1) Descriptor() ([]byte, []int) {
 	return file_datasets_google_message1_proto2_benchmark_message1_proto2_proto_rawDescGZIP(), []int{0}
 }
 
-const Default_GoogleMessage1_Field80 bool = false
-const Default_GoogleMessage1_Field81 bool = true
-const Default_GoogleMessage1_Field6 int32 = 0
-const Default_GoogleMessage1_Field59 bool = false
-const Default_GoogleMessage1_Field130 int32 = 0
-const Default_GoogleMessage1_Field12 bool = true
-const Default_GoogleMessage1_Field17 bool = true
-const Default_GoogleMessage1_Field13 bool = true
-const Default_GoogleMessage1_Field14 bool = true
-const Default_GoogleMessage1_Field104 int32 = 0
-const Default_GoogleMessage1_Field100 int32 = 0
-const Default_GoogleMessage1_Field101 int32 = 0
-const Default_GoogleMessage1_Field29 int32 = 0
-const Default_GoogleMessage1_Field30 bool = false
-const Default_GoogleMessage1_Field60 int32 = -1
-const Default_GoogleMessage1_Field271 int32 = -1
-const Default_GoogleMessage1_Field272 int32 = -1
-const Default_GoogleMessage1_Field23 int32 = 0
-const Default_GoogleMessage1_Field24 bool = false
-const Default_GoogleMessage1_Field25 int32 = 0
-const Default_GoogleMessage1_Field67 int32 = 0
-const Default_GoogleMessage1_Field128 int32 = 0
-const Default_GoogleMessage1_Field129 string = "xxxxxxxxxxxxxxxxxxxxx"
-const Default_GoogleMessage1_Field131 int32 = 0
-
 func (x *GoogleMessage1) GetField1() string {
 	if x != nil && x.Field1 != nil {
 		return *x.Field1
@@ -429,6 +429,16 @@ type GoogleMessage1SubMessage struct {
 	unknownFields protoimpl.UnknownFields
 }
 
+const Default_GoogleMessage1SubMessage_Field1 int32 = 0
+const Default_GoogleMessage1SubMessage_Field2 int32 = 0
+const Default_GoogleMessage1SubMessage_Field3 int32 = 0
+const Default_GoogleMessage1SubMessage_Field12 bool = true
+const Default_GoogleMessage1SubMessage_Field19 int32 = 2
+const Default_GoogleMessage1SubMessage_Field20 bool = true
+const Default_GoogleMessage1SubMessage_Field28 bool = true
+const Default_GoogleMessage1SubMessage_Field23 bool = false
+const Default_GoogleMessage1SubMessage_Field206 bool = false
+
 func (x *GoogleMessage1SubMessage) Reset() {
 	*x = GoogleMessage1SubMessage{}
 }
@@ -456,16 +466,6 @@ func (*GoogleMessage1SubMessage) Descriptor() ([]byte, []int) {
 	return file_datasets_google_message1_proto2_benchmark_message1_proto2_proto_rawDescGZIP(), []int{1}
 }
 
-const Default_GoogleMessage1SubMessage_Field1 int32 = 0
-const Default_GoogleMessage1SubMessage_Field2 int32 = 0
-const Default_GoogleMessage1SubMessage_Field3 int32 = 0
-const Default_GoogleMessage1SubMessage_Field12 bool = true
-const Default_GoogleMessage1SubMessage_Field19 int32 = 2
-const Default_GoogleMessage1SubMessage_Field20 bool = true
-const Default_GoogleMessage1SubMessage_Field28 bool = true
-const Default_GoogleMessage1SubMessage_Field23 bool = false
-const Default_GoogleMessage1SubMessage_Field206 bool = false
-
 func (x *GoogleMessage1SubMessage) GetField1() int32 {
 	if x != nil && x.Field1 != nil {
 		return *x.Field1

+ 22 - 22
internal/testprotos/benchmarks/datasets/google_message2/benchmark_message2.pb.go

@@ -53,6 +53,22 @@ type GoogleMessage2 struct {
 	unknownFields protoimpl.UnknownFields
 }
 
+const Default_GoogleMessage2_Field75 bool = false
+const Default_GoogleMessage2_Field21 int32 = 0
+const Default_GoogleMessage2_Field109 int32 = 0
+const Default_GoogleMessage2_Field210 int32 = 0
+const Default_GoogleMessage2_Field211 int32 = 0
+const Default_GoogleMessage2_Field212 int32 = 0
+const Default_GoogleMessage2_Field213 int32 = 0
+const Default_GoogleMessage2_Field216 int32 = 0
+const Default_GoogleMessage2_Field217 int32 = 0
+const Default_GoogleMessage2_Field218 int32 = 0
+const Default_GoogleMessage2_Field220 int32 = 0
+const Default_GoogleMessage2_Field221 int32 = 0
+const Default_GoogleMessage2_Field222 float32 = 0
+const Default_GoogleMessage2_Field205 bool = false
+const Default_GoogleMessage2_Field206 bool = false
+
 func (x *GoogleMessage2) Reset() {
 	*x = GoogleMessage2{}
 }
@@ -80,22 +96,6 @@ func (*GoogleMessage2) Descriptor() ([]byte, []int) {
 	return file_datasets_google_message2_benchmark_message2_proto_rawDescGZIP(), []int{0}
 }
 
-const Default_GoogleMessage2_Field75 bool = false
-const Default_GoogleMessage2_Field21 int32 = 0
-const Default_GoogleMessage2_Field109 int32 = 0
-const Default_GoogleMessage2_Field210 int32 = 0
-const Default_GoogleMessage2_Field211 int32 = 0
-const Default_GoogleMessage2_Field212 int32 = 0
-const Default_GoogleMessage2_Field213 int32 = 0
-const Default_GoogleMessage2_Field216 int32 = 0
-const Default_GoogleMessage2_Field217 int32 = 0
-const Default_GoogleMessage2_Field218 int32 = 0
-const Default_GoogleMessage2_Field220 int32 = 0
-const Default_GoogleMessage2_Field221 int32 = 0
-const Default_GoogleMessage2_Field222 float32 = 0
-const Default_GoogleMessage2_Field205 bool = false
-const Default_GoogleMessage2_Field206 bool = false
-
 func (x *GoogleMessage2) GetField1() string {
 	if x != nil && x.Field1 != nil {
 		return *x.Field1
@@ -323,6 +323,10 @@ type GoogleMessage2GroupedMessage struct {
 	unknownFields protoimpl.UnknownFields
 }
 
+const Default_GoogleMessage2GroupedMessage_Field3 float32 = 0
+const Default_GoogleMessage2GroupedMessage_Field6 bool = true
+const Default_GoogleMessage2GroupedMessage_Field7 bool = false
+
 func (x *GoogleMessage2GroupedMessage) Reset() {
 	*x = GoogleMessage2GroupedMessage{}
 }
@@ -350,10 +354,6 @@ func (*GoogleMessage2GroupedMessage) Descriptor() ([]byte, []int) {
 	return file_datasets_google_message2_benchmark_message2_proto_rawDescGZIP(), []int{1}
 }
 
-const Default_GoogleMessage2GroupedMessage_Field3 float32 = 0
-const Default_GoogleMessage2GroupedMessage_Field6 bool = true
-const Default_GoogleMessage2GroupedMessage_Field7 bool = false
-
 func (x *GoogleMessage2GroupedMessage) GetField1() float32 {
 	if x != nil && x.Field1 != nil {
 		return *x.Field1
@@ -453,6 +453,8 @@ type GoogleMessage2_Group1 struct {
 	unknownFields protoimpl.UnknownFields
 }
 
+const Default_GoogleMessage2_Group1_Field20 int32 = 0
+
 func (x *GoogleMessage2_Group1) Reset() {
 	*x = GoogleMessage2_Group1{}
 }
@@ -480,8 +482,6 @@ func (*GoogleMessage2_Group1) Descriptor() ([]byte, []int) {
 	return file_datasets_google_message2_benchmark_message2_proto_rawDescGZIP(), []int{0, 0}
 }
 
-const Default_GoogleMessage2_Group1_Field20 int32 = 0
-
 func (x *GoogleMessage2_Group1) GetField11() float32 {
 	if x != nil && x.Field11 != nil {
 		return *x.Field11

+ 22 - 20
internal/testprotos/test/test.pb.go

@@ -333,6 +333,26 @@ type TestAllTypes struct {
 	unknownFields protoimpl.UnknownFields
 }
 
+const Default_TestAllTypes_DefaultInt32 int32 = 81
+const Default_TestAllTypes_DefaultInt64 int64 = 82
+const Default_TestAllTypes_DefaultUint32 uint32 = 83
+const Default_TestAllTypes_DefaultUint64 uint64 = 84
+const Default_TestAllTypes_DefaultSint32 int32 = -85
+const Default_TestAllTypes_DefaultSint64 int64 = 86
+const Default_TestAllTypes_DefaultFixed32 uint32 = 87
+const Default_TestAllTypes_DefaultFixed64 uint64 = 88
+const Default_TestAllTypes_DefaultSfixed32 int32 = 89
+const Default_TestAllTypes_DefaultSfixed64 int64 = -90
+const Default_TestAllTypes_DefaultFloat float32 = 91.5
+const Default_TestAllTypes_DefaultDouble float64 = 92000
+const Default_TestAllTypes_DefaultBool bool = true
+const Default_TestAllTypes_DefaultString string = "hello"
+
+var Default_TestAllTypes_DefaultBytes []byte = []byte("world")
+
+const Default_TestAllTypes_DefaultNestedEnum TestAllTypes_NestedEnum = TestAllTypes_BAR
+const Default_TestAllTypes_DefaultForeignEnum ForeignEnum = ForeignEnum_FOREIGN_BAR
+
 func (x *TestAllTypes) Reset() {
 	*x = TestAllTypes{}
 }
@@ -360,26 +380,6 @@ func (*TestAllTypes) Descriptor() ([]byte, []int) {
 	return file_test_test_proto_rawDescGZIP(), []int{0}
 }
 
-const Default_TestAllTypes_DefaultInt32 int32 = 81
-const Default_TestAllTypes_DefaultInt64 int64 = 82
-const Default_TestAllTypes_DefaultUint32 uint32 = 83
-const Default_TestAllTypes_DefaultUint64 uint64 = 84
-const Default_TestAllTypes_DefaultSint32 int32 = -85
-const Default_TestAllTypes_DefaultSint64 int64 = 86
-const Default_TestAllTypes_DefaultFixed32 uint32 = 87
-const Default_TestAllTypes_DefaultFixed64 uint64 = 88
-const Default_TestAllTypes_DefaultSfixed32 int32 = 89
-const Default_TestAllTypes_DefaultSfixed64 int64 = -90
-const Default_TestAllTypes_DefaultFloat float32 = 91.5
-const Default_TestAllTypes_DefaultDouble float64 = 92000
-const Default_TestAllTypes_DefaultBool bool = true
-const Default_TestAllTypes_DefaultString string = "hello"
-
-var Default_TestAllTypes_DefaultBytes []byte = []byte("world")
-
-const Default_TestAllTypes_DefaultNestedEnum TestAllTypes_NestedEnum = TestAllTypes_BAR
-const Default_TestAllTypes_DefaultForeignEnum ForeignEnum = ForeignEnum_FOREIGN_BAR
-
 func (x *TestAllTypes) GetOptionalInt32() int32 {
 	if x != nil && x.OptionalInt32 != nil {
 		return *x.OptionalInt32
@@ -1585,6 +1585,7 @@ func (x *TestWeak) GetWeakMessage1() protoiface.MessageV1 {
 	}
 	return protoimpl.X.WeakNil("goproto.proto.test.weak.WeakImportMessage1")
 }
+
 func (x *TestWeak) GetWeakMessage2() protoiface.MessageV1 {
 	if x != nil {
 		v := x.XXX_weak[2]
@@ -1595,6 +1596,7 @@ func (x *TestWeak) GetWeakMessage2() protoiface.MessageV1 {
 	}
 	return protoimpl.X.WeakNil("goproto.proto.test.weak.WeakImportMessage2")
 }
+
 func (x *TestWeak) SetWeakMessage1(v protoiface.MessageV1) {
 	if x.XXX_weak == nil {
 		x.XXX_weak = make(protoimpl.WeakFields)

+ 32 - 32
types/descriptorpb/descriptor.pb.go

@@ -1175,6 +1175,9 @@ type MethodDescriptorProto struct {
 	unknownFields   protoimpl.UnknownFields
 }
 
+const Default_MethodDescriptorProto_ClientStreaming bool = false
+const Default_MethodDescriptorProto_ServerStreaming bool = false
+
 func (x *MethodDescriptorProto) Reset() {
 	*x = MethodDescriptorProto{}
 }
@@ -1202,9 +1205,6 @@ func (*MethodDescriptorProto) Descriptor() ([]byte, []int) {
 	return file_google_protobuf_descriptor_proto_rawDescGZIP(), []int{9}
 }
 
-const Default_MethodDescriptorProto_ClientStreaming bool = false
-const Default_MethodDescriptorProto_ServerStreaming bool = false
-
 func (x *MethodDescriptorProto) GetName() string {
 	if x != nil && x.Name != nil {
 		return *x.Name
@@ -1338,6 +1338,16 @@ type FileOptions struct {
 	extensionFields     protoimpl.ExtensionFields
 }
 
+const Default_FileOptions_JavaMultipleFiles bool = false
+const Default_FileOptions_JavaStringCheckUtf8 bool = false
+const Default_FileOptions_OptimizeFor FileOptions_OptimizeMode = FileOptions_SPEED
+const Default_FileOptions_CcGenericServices bool = false
+const Default_FileOptions_JavaGenericServices bool = false
+const Default_FileOptions_PyGenericServices bool = false
+const Default_FileOptions_PhpGenericServices bool = false
+const Default_FileOptions_Deprecated bool = false
+const Default_FileOptions_CcEnableArenas bool = false
+
 func (x *FileOptions) Reset() {
 	*x = FileOptions{}
 }
@@ -1374,16 +1384,6 @@ func (*FileOptions) ExtensionRangeArray() []protoiface.ExtensionRangeV1 {
 	return extRange_FileOptions
 }
 
-const Default_FileOptions_JavaMultipleFiles bool = false
-const Default_FileOptions_JavaStringCheckUtf8 bool = false
-const Default_FileOptions_OptimizeFor FileOptions_OptimizeMode = FileOptions_SPEED
-const Default_FileOptions_CcGenericServices bool = false
-const Default_FileOptions_JavaGenericServices bool = false
-const Default_FileOptions_PyGenericServices bool = false
-const Default_FileOptions_PhpGenericServices bool = false
-const Default_FileOptions_Deprecated bool = false
-const Default_FileOptions_CcEnableArenas bool = false
-
 func (x *FileOptions) GetJavaPackage() string {
 	if x != nil && x.JavaPackage != nil {
 		return *x.JavaPackage
@@ -1591,6 +1591,10 @@ type MessageOptions struct {
 	extensionFields     protoimpl.ExtensionFields
 }
 
+const Default_MessageOptions_MessageSetWireFormat bool = false
+const Default_MessageOptions_NoStandardDescriptorAccessor bool = false
+const Default_MessageOptions_Deprecated bool = false
+
 func (x *MessageOptions) Reset() {
 	*x = MessageOptions{}
 }
@@ -1627,10 +1631,6 @@ func (*MessageOptions) ExtensionRangeArray() []protoiface.ExtensionRangeV1 {
 	return extRange_MessageOptions
 }
 
-const Default_MessageOptions_MessageSetWireFormat bool = false
-const Default_MessageOptions_NoStandardDescriptorAccessor bool = false
-const Default_MessageOptions_Deprecated bool = false
-
 func (x *MessageOptions) GetMessageSetWireFormat() bool {
 	if x != nil && x.MessageSetWireFormat != nil {
 		return *x.MessageSetWireFormat
@@ -1734,6 +1734,12 @@ type FieldOptions struct {
 	extensionFields     protoimpl.ExtensionFields
 }
 
+const Default_FieldOptions_Ctype FieldOptions_CType = FieldOptions_STRING
+const Default_FieldOptions_Jstype FieldOptions_JSType = FieldOptions_JS_NORMAL
+const Default_FieldOptions_Lazy bool = false
+const Default_FieldOptions_Deprecated bool = false
+const Default_FieldOptions_Weak bool = false
+
 func (x *FieldOptions) Reset() {
 	*x = FieldOptions{}
 }
@@ -1770,12 +1776,6 @@ func (*FieldOptions) ExtensionRangeArray() []protoiface.ExtensionRangeV1 {
 	return extRange_FieldOptions
 }
 
-const Default_FieldOptions_Ctype FieldOptions_CType = FieldOptions_STRING
-const Default_FieldOptions_Jstype FieldOptions_JSType = FieldOptions_JS_NORMAL
-const Default_FieldOptions_Lazy bool = false
-const Default_FieldOptions_Deprecated bool = false
-const Default_FieldOptions_Weak bool = false
-
 func (x *FieldOptions) GetCtype() FieldOptions_CType {
 	if x != nil && x.Ctype != nil {
 		return *x.Ctype
@@ -1894,6 +1894,8 @@ type EnumOptions struct {
 	extensionFields     protoimpl.ExtensionFields
 }
 
+const Default_EnumOptions_Deprecated bool = false
+
 func (x *EnumOptions) Reset() {
 	*x = EnumOptions{}
 }
@@ -1930,8 +1932,6 @@ func (*EnumOptions) ExtensionRangeArray() []protoiface.ExtensionRangeV1 {
 	return extRange_EnumOptions
 }
 
-const Default_EnumOptions_Deprecated bool = false
-
 func (x *EnumOptions) GetAllowAlias() bool {
 	if x != nil && x.AllowAlias != nil {
 		return *x.AllowAlias
@@ -1967,6 +1967,8 @@ type EnumValueOptions struct {
 	extensionFields     protoimpl.ExtensionFields
 }
 
+const Default_EnumValueOptions_Deprecated bool = false
+
 func (x *EnumValueOptions) Reset() {
 	*x = EnumValueOptions{}
 }
@@ -2003,8 +2005,6 @@ func (*EnumValueOptions) ExtensionRangeArray() []protoiface.ExtensionRangeV1 {
 	return extRange_EnumValueOptions
 }
 
-const Default_EnumValueOptions_Deprecated bool = false
-
 func (x *EnumValueOptions) GetDeprecated() bool {
 	if x != nil && x.Deprecated != nil {
 		return *x.Deprecated
@@ -2033,6 +2033,8 @@ type ServiceOptions struct {
 	extensionFields     protoimpl.ExtensionFields
 }
 
+const Default_ServiceOptions_Deprecated bool = false
+
 func (x *ServiceOptions) Reset() {
 	*x = ServiceOptions{}
 }
@@ -2069,8 +2071,6 @@ func (*ServiceOptions) ExtensionRangeArray() []protoiface.ExtensionRangeV1 {
 	return extRange_ServiceOptions
 }
 
-const Default_ServiceOptions_Deprecated bool = false
-
 func (x *ServiceOptions) GetDeprecated() bool {
 	if x != nil && x.Deprecated != nil {
 		return *x.Deprecated
@@ -2100,6 +2100,9 @@ type MethodOptions struct {
 	extensionFields     protoimpl.ExtensionFields
 }
 
+const Default_MethodOptions_Deprecated bool = false
+const Default_MethodOptions_IdempotencyLevel MethodOptions_IdempotencyLevel = MethodOptions_IDEMPOTENCY_UNKNOWN
+
 func (x *MethodOptions) Reset() {
 	*x = MethodOptions{}
 }
@@ -2136,9 +2139,6 @@ func (*MethodOptions) ExtensionRangeArray() []protoiface.ExtensionRangeV1 {
 	return extRange_MethodOptions
 }
 
-const Default_MethodOptions_Deprecated bool = false
-const Default_MethodOptions_IdempotencyLevel MethodOptions_IdempotencyLevel = MethodOptions_IDEMPOTENCY_UNKNOWN
-
 func (x *MethodOptions) GetDeprecated() bool {
 	if x != nil && x.Deprecated != nil {
 		return *x.Deprecated