internal/cmd/generate-protos: generate internal descfield package
Generate a list of descriptor fields from the descriptor.proto itself
as an internal package. Use these fields for the internal implementation.
Change-Id: Ib1ab0c5c6deb332ba6c8018ef55136b7e5974944
Reviewed-on: https://go-review.googlesource.com/c/164864
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/protogen/protogen.go b/protogen/protogen.go
index f945690..a61baa0 100644
--- a/protogen/protogen.go
+++ b/protogen/protogen.go
@@ -29,6 +29,7 @@
"strings"
"github.com/golang/protobuf/proto"
+ "github.com/golang/protobuf/v2/internal/descfield"
"github.com/golang/protobuf/v2/internal/scalar"
"github.com/golang/protobuf/v2/reflect/protodesc"
"github.com/golang/protobuf/v2/reflect/protoreflect"
@@ -519,9 +520,9 @@
func newMessage(gen *Plugin, f *File, parent *Message, desc protoreflect.MessageDescriptor) *Message {
var loc Location
if parent != nil {
- loc = parent.Location.appendPath(messageMessageField, int32(desc.Index()))
+ loc = parent.Location.appendPath(descfield.DescriptorProto_NestedType, int32(desc.Index()))
} else {
- loc = f.location(fileMessageField, int32(desc.Index()))
+ loc = f.location(descfield.FileDescriptorProto_MessageType, int32(desc.Index()))
}
message := &Message{
Desc: desc,
@@ -637,11 +638,11 @@
var loc Location
switch {
case desc.ExtendedType() != nil && message == nil:
- loc = f.location(fileExtensionField, int32(desc.Index()))
+ loc = f.location(descfield.FileDescriptorProto_Extension, int32(desc.Index()))
case desc.ExtendedType() != nil && message != nil:
- loc = message.Location.appendPath(messageExtensionField, int32(desc.Index()))
+ loc = message.Location.appendPath(descfield.DescriptorProto_Extension, int32(desc.Index()))
default:
- loc = message.Location.appendPath(messageFieldField, int32(desc.Index()))
+ loc = message.Location.appendPath(descfield.DescriptorProto_Field, int32(desc.Index()))
}
field := &Field{
Desc: desc,
@@ -702,7 +703,7 @@
Desc: desc,
ParentMessage: message,
GoName: camelCase(string(desc.Name())),
- Location: message.Location.appendPath(messageOneofField, int32(desc.Index())),
+ Location: message.Location.appendPath(descfield.DescriptorProto_OneofDecl, int32(desc.Index())),
}
}
@@ -724,9 +725,9 @@
func newEnum(gen *Plugin, f *File, parent *Message, desc protoreflect.EnumDescriptor) *Enum {
var loc Location
if parent != nil {
- loc = parent.Location.appendPath(messageEnumField, int32(desc.Index()))
+ loc = parent.Location.appendPath(descfield.DescriptorProto_EnumType, int32(desc.Index()))
} else {
- loc = f.location(fileEnumField, int32(desc.Index()))
+ loc = f.location(descfield.FileDescriptorProto_EnumType, int32(desc.Index()))
}
enum := &Enum{
Desc: desc,
@@ -761,7 +762,7 @@
return &EnumValue{
Desc: desc,
GoIdent: f.GoImportPath.Ident(name),
- Location: enum.Location.appendPath(enumValueField, int32(desc.Index())),
+ Location: enum.Location.appendPath(descfield.EnumDescriptorProto_Value, int32(desc.Index())),
}
}
@@ -778,7 +779,7 @@
service := &Service{
Desc: desc,
GoName: camelCase(string(desc.Name())),
- Location: f.location(fileServiceField, int32(desc.Index())),
+ Location: f.location(descfield.FileDescriptorProto_Service, int32(desc.Index())),
}
for i, mdescs := 0, desc.Methods(); i < mdescs.Len(); i++ {
service.Methods = append(service.Methods, newMethod(gen, f, service, mdescs.Get(i)))
@@ -802,7 +803,7 @@
Desc: desc,
GoName: camelCase(string(desc.Name())),
ParentService: service,
- Location: service.Location.appendPath(serviceMethodField, int32(desc.Index())),
+ Location: service.Location.appendPath(descfield.ServiceDescriptorProto_Method, int32(desc.Index())),
}
return method
}
@@ -1081,34 +1082,6 @@
pathTypeSourceRelative
)
-// The SourceCodeInfo message describes the location of elements of a parsed
-// .proto file by way of a "path", which is a sequence of integers that
-// describe the route from a FileDescriptorProto to the relevant submessage.
-// The path alternates between a field number of a repeated field, and an index
-// into that repeated field. The constants below define the field numbers that
-// are used.
-//
-// See descriptor.proto for more information about this.
-const (
- // field numbers in FileDescriptorProto
- filePackageField = 2 // package
- fileMessageField = 4 // message_type
- fileEnumField = 5 // enum_type
- fileServiceField = 6 // service
- fileExtensionField = 7 // extension
- // field numbers in DescriptorProto
- messageFieldField = 2 // field
- messageMessageField = 3 // nested_type
- messageEnumField = 4 // enum_type
- messageExtensionField = 6 // extension
- messageOneofField = 8 // oneof_decl
- // field numbers in EnumDescriptorProto
- enumValueField = 2 // value
- // field numbers in ServiceDescriptorProto
- serviceMethodField = 2 // method
- serviceStreamField = 4 // stream
-)
-
// A Location is a location in a .proto source file.
//
// See the google.protobuf.SourceCodeInfo documentation in descriptor.proto